Exemple #1
0
int
main(int argc, char *argv[]) {
  char *grammar_pathname;
  Grammar *g;
  (void)argc;

  process_args(&arg_state, argv);
  if (arg_state.nfile_arguments != 1)
    help(&arg_state, NULL);
  grammar_pathname = arg_state.file_argument[0];
  g = new_D_Grammar(grammar_pathname);
  /* grammar construction options */
  g->set_op_priority_from_rule = set_op_priority_from_rule;
  g->right_recursive_BNF = right_recursive_BNF;
  g->states_for_whitespace = states_for_whitespace;
  g->states_for_all_nterms = states_for_all_nterms;
  g->tokenizer = tokenizer;
  g->longest_match = longest_match;
  /* grammar writing options */
  strcpy(g->grammar_ident, grammar_ident);
  if (ident_from_filename) {
    char *n = strrchr(grammar_pathname, '/'), *e;
    n = n ? n : grammar_pathname;
    e = strchr(n, '.');
    e = e ? e : n + strlen(n);
    memcpy(g->grammar_ident, n, e-n);
    g->grammar_ident[e-n] = 0;
  }
  g->scanner_blocks = scanner_blocks;
  g->scanner_block_size = scanner_block_size;
  g->write_line_directives = write_line_directives;
  g->write_header = write_header;
  g->token_type = token_type;
  strcpy(g->write_extension, write_extension);

  if (!output_file[0]) {
    strncpy(output_file, grammar_pathname, sizeof(output_file)-1);
    strncat(output_file, ".d_parser.", sizeof(output_file)-strlen(output_file)-1);
    strncat(output_file, g->write_extension, sizeof(output_file)-strlen(output_file)-1);
  }
  g->write_pathname = output_file;

  /* don't print anything to stdout, when the grammar is printed there */
  if (d_rdebug_grammar_level > 0)
    d_verbose_level = 0;

  mkdparse(g, grammar_pathname);

  if (d_rdebug_grammar_level == 0) {
    if (write_c_tables(g) < 0)
      d_fail("unable to write C tables '%s'", grammar_pathname);
  } else
    print_rdebug_grammar(g, grammar_pathname);

  free_args(&arg_state);
  free_D_Grammar(g);
  g = 0;
  return 0;
}
Exemple #2
0
int
main(int argc, char *argv[]) {
  int i, len = 0;
  char *buf = NULL, *fn, *grammar_pathname;
  D_Parser *p;
  D_ParseNode *pn = NULL;
  unsigned char *str = NULL;
  unsigned int str_len;
  Grammar *g;
  D_ParserTables *parser_tables_gram;

  process_args(&arg_state, argv);
  if (arg_state.nfile_arguments < 2)
    help(&arg_state, NULL);

  /* build grammar */
  grammar_pathname = arg_state.file_argument[0];
  g = new_D_Grammar(grammar_pathname);
  g->set_op_priority_from_rule = set_op_priority_from_rule;
  g->right_recursive_BNF = right_recursive_BNF;
  g->states_for_whitespace = states_for_whitespace;
  g->states_for_all_nterms = states_for_all_nterms;
  g->tokenizer = tokenizer;
  g->longest_match = longest_match;
  g->scanner_blocks = scanner_blocks;
  g->scanner_block_size = scanner_block_size;
  
  if (!(str = (unsigned char*)sbuf_read(grammar_pathname)))
    d_fail("unable to read grammar file '%s'", grammar_pathname);
  mkdparse_from_string(g, (char*)str);
  str = 0;
  if (write_binary_tables_to_string(g, &str, &str_len) < 0)
    d_fail("unable to write tables to string '%s'", grammar_pathname);
  free_D_Grammar(g);

  /* execute parser */
  parser_tables_gram = read_binary_tables_from_string(str, spec_code, final_code);
  p = new_D_Parser(parser_tables_gram, SIZEOF_MY_PARSE_NODE);
  p->save_parse_tree = save_parse_tree;
  p->ambiguity_fn = ambiguity_count_fn;
  p->partial_parses = partial_parses;
  p->dont_fixup_internal_productions = !fixup;
  p->fixup_EBNF_productions = fixup_ebnf;
  p->dont_compare_stacks = !compare_stacks;
  p->commit_actions_interval = commit_actions_interval;
  p->start_state = start_state;
  for (i = 1; i < arg_state.nfile_arguments; i++) {
    p->loc.pathname = arg_state.file_argument[i];
    p->loc.line = 1;
    p->loc.col = p->loc.previous_col = 0;
    if (buf_read(arg_state.file_argument[i], &buf, &len) > 0)
      pn = dparse(p, buf, len);
    else 
      d_fail("unable to read file '%s'", arg_state.file_argument[i]);
    if (pn) {
      free_D_ParseNode(p, pn);
      pn = 0;
    } else {
      fn = d_dup_pathname_str(p->loc.pathname);
      fprintf(stderr, "fatal error, '%s' line %d\n", fn, p->loc.line);
      FREE(fn);
    }
    if (buf)
      FREE(buf);
  }
  free_D_Parser(p);
  free_args(&arg_state);
  exit(0);
}