/* Global directive code */
 void printTreeRec(rdp_tree_node_data *node, int level) {
        for (int i = 0; i < level; i++) text_printf(" ");
        text_printf("%s\n", node->id);
        for (void *edge = graph_next_out_edge(node); edge != NULL;
                                        edge = graph_next_out_edge(edge))
                printTreeRec((rdp_tree_node_data*) graph_destination(edge), level+1);
} 
Esempio n. 2
0
static int rdp_check_nullable(void * base)
{
  int bad = 0; 
  rdp_data * temp =(rdp_data *) symbol_next_symbol_in_scope(base); 
  set_ work = SET_NULL; 
  
  while (temp != NULL)
  {
    if (temp->contains_null &&(temp->kind != K_CODE))
    {
      set_assign_set(& work, & temp->first); 
      set_intersect_set(& work, & temp->follow); 
      
      if (set_cardinality(& work)!= 0)
      {
        text_message(TEXT_ERROR, "LL(1) violation - rule\n %s ::= ", temp->id); 
        rdp_print_sub_item(temp, 1); 
        text_printf(".\n contains null but first and follow sets both include: "); 
        
        set_print_set(& work, rdp_token_string, 78); 
        text_printf("\n"); 
        temp->ll1_violation = 1; 
        bad = 1; 
      }
    }
    temp =(rdp_data *) symbol_next_symbol_in_scope(temp); 
  }
  return bad; 
}
Esempio n. 3
0
/* Check if we have a nullable alternate inside a nullable iterator */
static int rdp_check_nested_nullable(void * base)
{
  int bad = 0; 
  rdp_data * temp =(rdp_data *) symbol_next_symbol_in_scope(base); 
  
  while (temp != NULL)
  {
    if (set_includes_element(& rdp_production_set, temp->kind)&& temp->kind != K_SEQUENCE)
    {
      rdp_list * inner = temp->list; 
      
      while (inner != NULL)
      {
        
        if (temp->lo == 0 && inner->production->contains_null)
        {
          text_message(TEXT_ERROR, "LL(1) violation - rule \'%s\'\n is nullable but contains the nullable subrule\n", temp->id); 
          text_printf(" %s ::= ", inner->production->id); 
          rdp_print_sub_item(inner->production, 1); 
          text_printf(".\n"); 
          bad = 1; 
          temp->ll1_violation = 1; 
          inner->production->ll1_violation = 1; 
        }
        inner = inner->next; 
      }
    }
    temp =(rdp_data *) symbol_next_symbol_in_scope(temp); 
  }
  return bad; 
}
Esempio n. 4
0
void text_dump(void)          /* debugging routine to dump text space */
{
  char * p = text_bot - 1;

  while (p <= text_top)
  {
    while (++p <= text_top && * p != 0)
      text_printf("%c", isprint(* p)? * p: '.');

    text_printf("\n");
  }
}
Esempio n. 5
0
void scan_vcg_print_edge(const void * edge)
{
  static unsigned long edge_count = 0;
  /* Set user inserted edges to red */
  if (*((int*) edge) == 0)
    text_printf("color:red");

#if defined(show_vcg_atom_numbers)
  text_printf("label:\"%lu\" ", graph_atom_number(edge));
#endif
  text_printf(" horizontal_order:%lu", 1000 - ++edge_count);
}
Esempio n. 6
0
void text_print_time(void)
{
  char line[80]; 
  time_t timer = time(NULL); 
  
  strftime(line, 80, "%b %d %Y %H:%M:%S", localtime(& timer)); 
  text_printf("%s", line); 
}
Esempio n. 7
0
static int rdp_check_disjoint(void * base)
{
  int bad = 0; 
  set_ work = SET_NULL; 
  
  rdp_data * temp =(rdp_data *) symbol_next_symbol_in_scope(base); 
  
  while (temp != NULL)
  {
    if (set_includes_element(& rdp_production_set, temp->kind)&& temp->kind != K_SEQUENCE)
    {
      rdp_list * left = temp->list; 
      
      while (left != NULL)
      {
        rdp_list * right = left->next; 
        
        while (right != NULL)
        {
          /* First check for disjoint on epsilon */
          if (left->production->contains_null && right->production->contains_null)
          {
            text_message(TEXT_ERROR, "LL(1) violation - rule \'%s\'\n", temp->id); 
            text_printf(" productions %s ::= ", left->production->id); 
            rdp_print_sub_item(left->production, 1); 
            text_printf(".\n and %s ::= ", right->production->id); 
            rdp_print_sub_item(right->production, 1); 
            text_printf(".\n are both nullable \n"); 
            left->production->ll1_violation = 1; 
            right->production->ll1_violation = 1; 
            bad = 1; 
          }
          
          set_assign_set(& work, & left->production->first); 
          set_intersect_set(& work, & right->production->first); 
          
          if (set_cardinality(& work)!= 0)
          {
            text_message(TEXT_ERROR, "LL(1) violation - rule \'%s\'\n", temp->id); 
            text_printf(" productions %s ::= ", left->production->id); 
            rdp_print_sub_item(left->production, 1); 
            text_printf(".\n and %s ::= ", right->production->id); 
            rdp_print_sub_item(right->production, 1); 
            text_printf(".\n share these start tokens: "); 
            set_print_set(& work, rdp_token_string, 78); 
            text_printf("\n"); 
            left->production->ll1_violation = 1; 
            right->production->ll1_violation = 1; 
            bad = 1; 
          }
          right = right->next; 
        }
        left = left->next; 
      }
    }
    temp =(rdp_data *) symbol_next_symbol_in_scope(temp); 
  }
  return bad; 
}
Esempio n. 8
0
void hist_print(histogram_node *this_hist)
{
  histogram_node *this_histogram_node = this_hist;

  while (this_histogram_node->next != NULL)
  {
    if (this_histogram_node->value != 0)
      text_printf("%lu: %lu\n", this_histogram_node->bucket, this_histogram_node->value);

    this_histogram_node = this_histogram_node->next;
  }
}
Esempio n. 9
0
int scan_test_set(const char * production, set_ * valid, set_ * stop)
{
  if (!set_includes_element(valid, SCAN_CAST->token))
  {
    if (stop != NULL)
    {
      if (production != NULL)
        text_message(TEXT_ERROR_ECHO, "In rule \'%s\', scanned ", production); 
      else
        text_message(TEXT_ERROR_ECHO, "Scanned ");
      set_print_element(SCAN_CAST->token, scan_token_names); 
      text_printf(" whilst expecting %s", set_cardinality(valid)== 1 ? "": "one of ");
      set_print_set(valid, scan_token_names, 60); 
      text_printf("\n"); 
      scan_skip(stop); 
    }
    return 0;
  }
  else
    return 1;
}
Esempio n. 10
0
int scan_test(const char * production, const int valid, set_ * stop)
{
  if (valid != SCAN_CAST->token)
  {
    if (stop != NULL)
    {
      if (production != NULL)
        text_message(TEXT_ERROR_ECHO, "In rule \'%s\', scanned ", production); 
      else
        text_message(TEXT_ERROR_ECHO, "Scanned ");
      set_print_element(SCAN_CAST->token, scan_token_names); 
      text_printf(" whilst expecting "); 

      set_print_element(valid, scan_token_names); 
      text_printf("\n");
      scan_skip(stop); 
    }
    return 0; 
  }
  else
    return 1; 
}
Esempio n. 11
0
void hist_report_differences(histogram_node *hista, histogram_node *histb)
{
  bool same = true;
  bool go_on = true;

  while (go_on)
  {
    while(hista->bucket < histb->bucket)
    {
      text_printf("\n%lu:\t%lu\t-\n", hista->bucket, hista->value);
      same = false;
      hista = hista->next;
    }

    while(histb->bucket < hista->bucket)
    {
      text_printf("\n%lu:\t-\t%lu\n", histb->bucket, histb->value);
      same = false;
      histb = histb->next;
    }

    if (histb->bucket == hista->bucket)
      if (hista->bucket != ULONG_MAX)
      {
        if (hista->value != histb->value)
        {
          text_printf("\n%lu:\t%lu\t%lu\n", hista->bucket, hista->value, histb->value);
          same = false;
        }
        hista = hista->next;
        histb = histb->next;
      }
      else
        go_on = false;
  }
  text_printf("%s\n", same ? "--- same ----" :
                             "- different -");
}
Esempio n. 12
0
void pretty_close(char * sourcefilename, char * outputfilename)
{
  unsigned long useful_lexeme_count = lexeme_count - comment_count - eoln_count; 
  char * backup_filename = text_force_filetype(sourcefilename, "bak"); 
  
  fclose(outputfile); 
  
  remove(backup_filename); 
  
  if (rename(sourcefilename, backup_filename)!= 0)
    text_message(TEXT_FATAL, "unable to rename \'%s\' to \'%s\'\n", sourcefilename, backup_filename); 
  
  if (rename(outputfilename, sourcefilename)!= 0)
    text_message(TEXT_FATAL, "unable to rename \'%s\' to \'%s\'\n", outputfilename, sourcefilename); 
  
  text_printf("%s,%lu,%lu,%.2lf\n", sourcefilename, 
  last_line, 
  useful_lexeme_count, 
  (double) useful_lexeme_count /(double) last_line); 
}
Esempio n. 13
0
int main(int argc, char *argv[])
{
  clock_t rdp_finish_time, rdp_start_time = clock();
  int
    rdp_symbol_statistics = 0,    /* show symbol_ table statistics flag */
    rdp_line_echo_all = 0,        /* make a listing on all passes flag */
    rdp_filter = 0,               /* filter flag */
    rdp_line_echo = 0,            /* make listing flag */

    rdp_lexicalise = 0;            /* print lexicalised output flag */

  unsigned long rdp_textsize = 35000l;   /* size of scanner text array */

  unsigned long rdp_tabwidth = 8l;   /* tab expansion width */

  char* rdp_vcg_filename = NULL;      /* filename for -V option */

  rdp_tree_node_data* rdp_tree = (rdp_tree_node_data*) graph_insert_graph("RDP derivation tree");  /* hook for derivation tree */
  rdp_tree_node_data* rdp_tree_root;

  arg_message("Minitree compiler V1.50 (c) Adrian Johnstone 1997\n" RDP_STAMP "\n\n""Usage: minitree [options] source[.m]");

  arg_message("");
  arg_boolean('f', "Filter mode (read from stdin and write to stdout)", &rdp_filter);
  arg_boolean('l', "Make a listing", &rdp_line_echo);
  arg_boolean('L', "Print lexicalised source file", &rdp_lexicalise);
  arg_string ('o', "Write output to filename", &rdp_outputfilename);
  arg_boolean('s', "Echo each scanner symbol as it is read", &rdp_symbol_echo);
  arg_boolean('S', "Print summary symbol table statistics", &rdp_symbol_statistics);
  arg_numeric('t', "Tab expansion width (default 8)", &rdp_tabwidth);
  arg_numeric('T', "Text buffer size in bytes for scanner (default 20000)", &rdp_textsize);
  arg_boolean('v', "Set verbose mode", &rdp_verbose);
  arg_string ('V', "Write derivation tree to filename in VCG format", &rdp_vcg_filename);

  rdp_sourcefilenames = arg_process(argc, argv);

  /* Fix up filetypes */
  for (rdp_sourcefilenumber = 0; rdp_sourcefilenames[rdp_sourcefilenumber] != NULL; rdp_sourcefilenumber++)
    rdp_sourcefilenames[rdp_sourcefilenumber] = text_default_filetype(rdp_sourcefilenames[rdp_sourcefilenumber], "m");

  if (rdp_filter)
  {
    rdp_sourcefilenames[0] = "-";
    rdp_outputfilename = "-";
    rdp_sourcefilenames[1] = NULL;     /* make sure no further filenames are taken from the array */

  }
  if ((rdp_sourcefilename = rdp_sourcefilenames[0]) == NULL)
     arg_help("no source files specified");

  if (rdp_sourcefilenames[1] != NULL)
    text_message(TEXT_FATAL, "multiple source files not allowed\n");
  text_init(rdp_textsize, 25, 100, (int) rdp_tabwidth);
  scan_init(0, 0, 0, rdp_symbol_echo, rdp_tokens);
  if (rdp_lexicalise)
    scan_lexicalise();
  mini = symbol_new_table("mini", 101, 31, symbol_compare_string, symbol_hash_string, symbol_print_string);
  rdp_set_initialise();
  rdp_load_keywords();
  if (rdp_verbose)
     text_printf("\nMinitree compiler V1.50 (c) Adrian Johnstone 1997\n" RDP_STAMP "\n\n");
  for (rdp_pass = 1; rdp_pass <= RDP_PASSES; rdp_pass++)
  {
    rdp_tree_update = rdp_pass == RDP_PASSES;
    text_echo(rdp_line_echo_all || (rdp_line_echo && rdp_pass == RDP_PASSES));

    for (rdp_sourcefilenumber = 0; (rdp_sourcefilename = rdp_sourcefilenames[rdp_sourcefilenumber]) != NULL; rdp_sourcefilenumber++)
    {
      if (text_open(rdp_sourcefilename) == NULL)
        arg_help("unable to open source file");

      text_get_char();
      scan_();

      program(rdp_tree_root = rdp_add_node("program", rdp_tree));            /* call parser at top level */
      if (text_total_errors() != 0)
        text_message(TEXT_FATAL, "error%s detected in source file ''\n", text_total_errors() == 1 ? "" : "s", rdp_sourcefilename);   /* crash quietly */ 
      graph_epsilon_prune_rdp_tree(rdp_tree_root, sizeof(rdp_tree_edge_data));
    }
  }

  rdp_sourcefilename = rdp_sourcefilenames[0];     /* Reset filename to first file in the list */

  graph_set_root(rdp_tree, rdp_tree_root);
  if (rdp_vcg_filename != NULL)
  {
    FILE *rdp_vcg_file;

    if (*rdp_vcg_filename == '\0')   /* No filename supplied */
      rdp_vcg_filename = "rdparser";
    rdp_vcg_file = fopen((rdp_vcg_filename = text_default_filetype(rdp_vcg_filename, "vcg")), "w");

    if (rdp_vcg_file == NULL)
      text_message(TEXT_FATAL, "unable to open VCG file '%s' for write\n", rdp_vcg_filename);

    if (rdp_verbose)
      text_message(TEXT_INFO, "Dumping derivation tree to VCG file '%s'\n", rdp_vcg_filename);

    text_redirect(rdp_vcg_file);
    graph_vcg(rdp_tree, NULL, scan_vcg_print_node, scan_vcg_print_edge);
    text_redirect(stdout);
    fclose(rdp_vcg_file);
  }

   code_generate(rdp_sourcefilename, rdp_outputfilename, rdp_tree); 
  if (rdp_symbol_statistics)
  {
    symbol_print_all_table_statistics(11);
    symbol_print_all_table();

  }
  text_print_total_errors();
  if (rdp_verbose)
  {
    rdp_finish_time = clock();
    text_message(TEXT_INFO, "%.3f CPU seconds used\n", ((double) (rdp_finish_time-rdp_start_time)) / CLOCKS_PER_SEC);
  }
  return rdp_error_return;
}
Esempio n. 14
0
int main(int argc, char *argv[])
{
  clock_t rdp_finish_time, rdp_start_time = clock();
  int
    rdp_symbol_statistics = 0,    /* show symbol_ table statistics flag */
    rdp_line_echo_all = 0,        /* make a listing on all passes flag */
    rdp_filter = 0,               /* filter flag */
    rdp_line_echo = 0,            /* make listing flag */

    rdp_lexicalise = 0;            /* print lexicalised output flag */

  unsigned long rdp_textsize = 35000l;   /* size of scanner text array */

  unsigned long rdp_tabwidth = 8l;   /* tab expansion width */

  char* rdp_vcg_filename = NULL;      /* filename for -V option */

  arg_message("rdparser\n" RDP_STAMP "\n\n""Usage: regex [options] source");

  arg_message("");
  arg_boolean('f', "Filter mode (read from stdin and write to stdout)", &rdp_filter);
  arg_boolean('l', "Make a listing", &rdp_line_echo);
  arg_boolean('L', "Print lexicalised source file", &rdp_lexicalise);
  arg_string ('o', "Write output to filename", &rdp_outputfilename);
  arg_boolean('s', "Echo each scanner symbol as it is read", &rdp_symbol_echo);
  arg_boolean('S', "Print summary symbol table statistics", &rdp_symbol_statistics);
  arg_numeric('t', "Tab expansion width (default 8)", &rdp_tabwidth);
  arg_numeric('T', "Text buffer size in bytes for scanner (default 20000)", &rdp_textsize);
  arg_boolean('v', "Set verbose mode", &rdp_verbose);
  arg_string ('V', "Write derivation tree to filename in VCG format", &rdp_vcg_filename);

  rdp_sourcefilenames = arg_process(argc, argv);

  /* Fix up filetypes */
  for (rdp_sourcefilenumber = 0; rdp_sourcefilenames[rdp_sourcefilenumber] != NULL; rdp_sourcefilenumber++)
    rdp_sourcefilenames[rdp_sourcefilenumber] = text_default_filetype(rdp_sourcefilenames[rdp_sourcefilenumber], "");

  if (rdp_filter)
  {
    rdp_sourcefilenames[0] = "-";
    rdp_outputfilename = "-";
    rdp_sourcefilenames[1] = NULL;     /* make sure no further filenames are taken from the array */

  }
  if ((rdp_sourcefilename = rdp_sourcefilenames[0]) == NULL)
     arg_help("no source files specified");

  if (rdp_sourcefilenames[1] != NULL)
    text_message(TEXT_FATAL, "multiple source files not allowed\n");
  text_init(rdp_textsize, 25, 100, (int) rdp_tabwidth);
  scan_init(0, 0, 0, rdp_symbol_echo, rdp_tokens);
  if (rdp_lexicalise)
    scan_lexicalise();
  rdp_set_initialise();
  rdp_load_keywords();
  if (rdp_verbose)
     text_printf("\nrdparser\n" RDP_STAMP "\n\n");
  for (rdp_pass = 1; rdp_pass <= RDP_PASSES; rdp_pass++)
  {
    text_echo(rdp_line_echo_all || (rdp_line_echo && rdp_pass == RDP_PASSES));

    for (rdp_sourcefilenumber = 0; (rdp_sourcefilename = rdp_sourcefilenames[rdp_sourcefilenumber]) != NULL; rdp_sourcefilenumber++)
    {
      if (text_open(rdp_sourcefilename) == NULL)
        arg_help("unable to open source file");

      text_get_char();
      scan_();

      reg();            /* call parser at top level */
      if (text_total_errors() != 0)
        text_message(TEXT_FATAL, "error%s detected in source file ''\n", text_total_errors() == 1 ? "" : "s", rdp_sourcefilename);   /* crash quietly */ 
    }
  }

  rdp_sourcefilename = rdp_sourcefilenames[0];     /* Reset filename to first file in the list */

  if (rdp_symbol_statistics)
  {
    symbol_print_all_table_statistics(11);
    symbol_print_all_table();

  }
  text_print_total_errors();
  if (rdp_verbose)
  {
    rdp_finish_time = clock();
    text_message(TEXT_INFO, "%.3f CPU seconds used\n", ((double) (rdp_finish_time-rdp_start_time)) / CLOCKS_PER_SEC);
  }
  return rdp_error_return;
}
Esempio n. 15
0
void scan_vcg_print_node(const void * node)
{
  text_printf("label:\"");

#if defined(show_vcg_atom_numbers)
  text_printf("%lu: ", graph_atom_number(node));
#endif

  switch (((scan_data *) node)->token)
  {
    case SCAN_P_IGNORE:
    case SCAN_P_ID: text_print_C_string(((scan_data *) node)->id); break;
    case SCAN_P_INTEGER:
    text_printf("INTEGER: %lu", ((scan_data *) node)->data.i); break;
    case SCAN_P_REAL: text_printf("REAL: %lf", ((scan_data *) node)->data.r); break;

    case SCAN_P_STRING:
    case SCAN_P_STRING_ESC:
    case SCAN_P_COMMENT:
    case SCAN_P_COMMENT_VISIBLE:
    case SCAN_P_COMMENT_NEST:
    case SCAN_P_COMMENT_NEST_VISIBLE:
    case SCAN_P_COMMENT_LINE:
    case SCAN_P_COMMENT_LINE_VISIBLE: text_printf("\\\""); text_print_C_string(((scan_data *) node)->id); text_printf("\\\""); break;

    case SCAN_P_EOF: text_printf("EOF"); break;
    case SCAN_P_EOLN: text_printf("EOLN"); break;
    default: text_printf("\'");
    text_print_C_string(((scan_data *) node)->id);
    text_printf("\'");
    break;
  }

  text_printf("\" horizontal_order:%lu", graph_atom_number(node));

  if (((scan_data *) node)->token == SCAN_P_IGNORE) /* make nonterminals round */
    text_printf(" shape:ellipse ");
}