Exemplo n.º 1
0
int main(int argc, char *argv[]) {
  char *buf;
  D_ParseNode *pn;
  /* any number greater than sizeof(D_ParseNode_User) will do;
     below 1024 is used */
  D_Parser *p = new_D_Parser(&parser_tables_gram, 1024); 
  p->save_parse_tree = 1;

  if (argc!=2) {
    fprintf(stderr,"U¿ycie: %s FILE_to_parse\n",argv[0]);
    return -1;
  } else {
    buf = sbuf_read(argv[1]);
    if (buf == NULL) {
      fprintf(stderr, "error: empty buf\n");
      return -2;
    }
  }
  printf("file: %s\n", argv[1]);
  printf("----------\n%s", buf);
  printf("----------\n");

  if ((pn=dparse(p, buf, strlen(buf))) && !p->syntax_errors) {
    printf("\nparse tree\n");
    printf("----------\n");
    print_parsetree(parser_tables_gram, pn, NULL, NULL);
    printf("\n");
  } else {
    printf("\nfailure\n");
  }
  return 0;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[]) {
  int i, len = 0;
  char *buf = NULL, *fn;
  D_Parser *p;
  D_ParseNode *pn = NULL;

  process_args(&arg_state, argv);
  if (!arg_state.nfile_arguments)
    help(&arg_state, NULL);
  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;
  p->dont_use_greediness_for_disambiguation = dont_use_greediness_for_disambiguation; 
  p->dont_use_height_for_disambiguation = dont_use_height_for_disambiguation;
  for (i = 0; i < arg_state.nfile_arguments; i++) {
    p->loc.pathname = arg_state.file_argument[i];
    p->loc.line = 1;
    p->loc.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);
}
Exemplo n.º 3
0
D_Parser *
make_parser(long int idpt,
	    PyObject *self,
	    PyObject *reject,
	    PyObject *make_token,
	    PyObject *loc_type,
	    PyObject *node_info_type,
	    PyObject *actions,
	    PyObject *initial_white_space_fn,
	    PyObject *syntax_error_fn,
	    PyObject *ambiguity_fn,
	    int dont_fixup_internal_productions,
            int fixup_EBNF_productions, 
	    int dont_merge_epsilon_trees,
	    int commit_actions_interval,
	    int error_recovery,
	    int print_debug_info,
            int partial_parses,
            int dont_compare_stacks,
            int dont_use_greediness_for_disambiguation,
            int dont_use_height_for_disambiguation,
	    char *start_symbol,
	    int takes_strings,
	    int takes_globals) {
  BinaryTables *dpt = (BinaryTables*) idpt;
  D_ParserPyInterface *ppi;
  D_Parser *p = new_D_Parser(dpt->parser_tables_gram, sizeof(D_ParseNode_User));
  p->fixup_EBNF_productions = fixup_EBNF_productions;
  p->save_parse_tree = 1;
  p->initial_scope = NULL; 
  p->dont_fixup_internal_productions = dont_fixup_internal_productions;
  p->dont_merge_epsilon_trees = dont_merge_epsilon_trees;
  p->commit_actions_interval = commit_actions_interval;
  p->partial_parses = partial_parses;
  p->dont_compare_stacks = dont_compare_stacks;
  p->dont_use_greediness_for_disambiguation = dont_use_greediness_for_disambiguation;
  p->dont_use_height_for_disambiguation = dont_use_height_for_disambiguation;
  p->error_recovery = error_recovery;
  p->free_node_fn = free_node_fn;
  ppi = malloc(sizeof(D_ParserPyInterface)); 
  memset(ppi, 0, sizeof(D_ParserPyInterface));
  ((Parser*)p)->pinterface1 = ppi;
  /* d_interface(p) = ppi; */
  if (initial_white_space_fn != Py_None)
    p->initial_white_space_fn = my_initial_white_space_fn;
  if (syntax_error_fn != Py_None)
    p->syntax_error_fn = my_syntax_error_fn;
  if (ambiguity_fn != Py_None)
    p->ambiguity_fn = my_ambiguity_fn;
  Py_INCREF(Py_None);
  p->initial_globals = Py_None;
  Py_INCREF(self);
  ppi->self = self;
  Py_INCREF(make_token);
  ppi->make_token = make_token;
  Py_INCREF(actions);
  ppi->actions = actions;
  Py_INCREF(initial_white_space_fn);
  ppi->initial_white_space_fn = initial_white_space_fn;
  Py_INCREF(syntax_error_fn);
  ppi->syntax_error_fn = syntax_error_fn;
  Py_INCREF(ambiguity_fn);
  ppi->ambiguity_fn = ambiguity_fn;
  Py_INCREF(loc_type);
  ppi->loc_type = loc_type;
  Py_INCREF(reject);
  ppi->reject = reject;
  Py_INCREF(node_info_type);
  ppi->node_info_type = node_info_type;
  ppi->print_debug_info = print_debug_info;
  ppi->takes_strings = takes_strings;
  ppi->takes_globals = takes_globals;
  ppi->symbol_list = NULL;
  if (start_symbol[0]) {
    int i;
    for (i = 0; i < dpt->parser_tables_gram->nsymbols; i++) {
      if (dpt->parser_tables_gram->symbols[i].kind == D_SYMBOL_NTERM && strcmp(dpt->parser_tables_gram->symbols[i].name, start_symbol) == 0) {
	p->start_state = dpt->parser_tables_gram->symbols[i].start_symbol;
	break;
      }
    }
    if (i == dpt->parser_tables_gram->nsymbols) {
      fprintf(stderr, "invalid start symbol: %s\n", start_symbol);
      i = 0;
    }
  }

  return p;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: 01org/fMBT
int
main(int argc, char *argv[]) {

	char 	*filebuf;
	int 	size;
	int	ch, arg;
	char	incdirsbuf[2048];

	int	dump_pp = 0;

	D_Parser *p = new_D_Parser(&parser_tables_gram,
				   sizeof(D_ParseNode_User));
	p->ambiguity_fn = my_ambiguity_fn;

	while ((ch = getopt(argc, argv, "vphI:")) != -1)
		switch (ch) {
		case 'v': d_verbose_level++; 	break;
		case 'p': dump_pp++;		break;
		case 'I':
			if ( ! v_incdirs ) {
				v_incdirs = incdirsbuf;
				v_incdirs[0] = '\0';
			}
			if ( strlen( v_incdirs ) + strlen( optarg ) + 2 >
			     sizeof(incdirsbuf) )
				exit(1);
			strcat( v_incdirs, optarg );
			strcat( v_incdirs, ":" );
			break;
		case '?':
		case 'h':
		default:
			fprintf( stderr, usage, argv[0] );
			exit(1);
		}

	for (arg = optind; arg < argc; arg++) {

		if ( v_getfile( argv[ arg ], &filebuf, &size ) < 0 ) {
			fprintf( stderr, "Trouble getting '%s', skipping.\n",
				 argv[ arg ] );
			continue;
		}

		p->loc.pathname = argv[ arg ];
		p->loc.line = 1;
		p->loc.col = 0;

		if ( dump_pp ) {
			int i;
			for (i=0; i<size; i++)
				putchar( filebuf[i] );
		}

		v_parse_init();
		if ( dparse( p, filebuf, size ) && !p->syntax_errors )
			fprintf( stderr, "%s succeeded\n", argv[ arg ] );
		else
			fprintf( stderr, "%s failed\n", argv[ arg ] );

		free( filebuf );

		arg++;
	}

	return 0;
}
Exemplo n.º 5
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);
}