示例#1
0
文件: analyzer.c 项目: notozeki/mysh
Node* analyze_line(String* str)
{
	Node* node;

	st_token_buf = NULL;
	lexer_init(str);
	parser_init();

	node = parse_acceptable();
	switch ( parser_state() ) {
	case PS_ACCEPT:
		if ( node == NULL ) {
			fprintf(stderr, "parse error\n");
		}
		break;
	case PS_ERROR:
		parser_print_error();
		if ( node != NULL ) {
			delete_tree(node);
			node = NULL;
		}
		break;
	}
	
	if ( st_token_buf != NULL) {
		delete_token(st_token_buf);
		st_token_buf = NULL;
	}

	return node;
}
示例#2
0
文件: tags.c 项目: dacap/htmlex
static char *iftag_fi (int argc, char *argv[])
{
  /* back to the old if's state */
  delete_token ();
  /* update the if's states */
  update_state ();
  return NULL;
}
示例#3
0
/**
 * Deletes a text.
 * */
void Classifier::delete_text(TextToClassify* text) {
	TextToClassify* next_text;

	while(text){
		if(text->sText) delete[] text->sText;
		if(text->pToken) delete_token(text->pToken);
		next_text = text->pNext;
		delete text;
		text = next_text;
	}
}
示例#4
0
/* Compare v to w.  Return
   -1 if v <  w or exception (PyErr_Occurred() true in latter case).
    0 if v == w.
    1 if v > w.
   XXX The docs (C API manual) say the return value is undefined in case
   XXX of error.
*/
int
PyObject_Compare(PyObject *v, PyObject *w)
{
	PyTypeObject *vtp;
	int result;

#if defined(USE_STACKCHECK)
	if (PyOS_CheckStack()) {
		PyErr_SetString(PyExc_MemoryError, "Stack overflow");
		return -1;
	}
#endif
	if (v == NULL || w == NULL) {
		PyErr_BadInternalCall();
		return -1;
	}
	if (v == w)
		return 0;
	vtp = v->ob_type;
	compare_nesting++;
	if (compare_nesting > NESTING_LIMIT &&
		(vtp->tp_as_mapping
		 || (vtp->tp_as_sequence
		     && !PyString_Check(v)
		     && !PyTuple_Check(v)))) {
		/* try to detect circular data structures */
		PyObject *token = check_recursion(v, w, -1);

		if (token == NULL) {
			result = -1;
		}
		else if (token == Py_None) {
			/* already comparing these objects.  assume
			   they're equal until shown otherwise */
                        result = 0;
		}
		else {
			result = do_cmp(v, w);
			delete_token(token);
		}
	}
	else {
		result = do_cmp(v, w);
	}
	compare_nesting--;
	return result < 0 ? -1 : result;
}
示例#5
0
token next_token() {
	extern int line_number;
	extern int yylex();
	extern char *yytext;

	if (did_back_token) {
        did_back_token = 0;
		return *current_token;
    }
	else {
		token *t;
		delete_token(previous_token);
		previous_token = current_token;
		t = (token*)malloc(sizeof(token));
		t->type = yylex();
		t->text = strdup(yytext);
		t->line = line_number;
		current_token = t;
		return *t;
	}
}
示例#6
0
int run_parser( TTreeView *TreeView1 , char *file_name )
{
    Config_set *config_ptr;
    GRAMMAR *G_ptr; 
    STATE *state_ptr;
    TOKEN *T_ptr , *T_tmp;
    FILE *input ,*out;
    char grammar_rule[1024];
    char *ptr , *ptr_string;
    char *ptr_lhs , *ptr_rhs;
    SET *S_ptr;
    int i , j;
    int rule_num; 

    strncpy( lambda , "£f" , strlen("£f")+1 );
	G_start = NULL;
    G_end = NULL;
	terminal_start = NULL;
    terminal_end = NULL;
    nonterminal_start = NULL;
    nonterminal_end = NULL;
    first_set_start = NULL;
    first_set_end = NULL;
    follow_set_start = NULL;
    follow_set_end = NULL;
    predict_set_start = NULL;
    predict_set_end = NULL;
    config_start = NULL;
    config_end = NULL;
    state_start = NULL;
    state_end = NULL;
    state_num = 0;
    
    
    //scanner();
	input = fopen( file_name ,"r");
	scan = fopen("out.txt","r");

    
    if ( input == NULL )
	{
    	Application->MessageBoxA("grammar file error","error",0);
        return 0;     
    }
     
//    out = fopen("table.csv","w");
     
    for ( i = 0 ; fgets( grammar_rule , 1024 , input ) ;  )
    {
        ptr = grammar_rule;
        // remove \n
        ptr = strstr(grammar_rule , "\n");
        if(ptr != NULL)
            *ptr = '\0';
            
        // remove rule num
        ptr = strstr( grammar_rule , "." );
        // get left handside
        ptr_lhs = ptr + 2;
        ptr = strstr( grammar_rule , " " );
        *ptr = '\0';
        insert_token( &nonterminal_start , &nonterminal_end , ptr_lhs );    // make nonterminal table
        insert_token( &first_set_start , &first_set_end , ptr_lhs );        // make first set table with nonterminal 
        insert_token( &follow_set_start , &follow_set_end , ptr_lhs );      // make follow set table
        insert_token( &predict_set_start , &predict_set_end , ptr_lhs );    // make predict set table
		// get right handside
		ptr = strstr( ++ptr , "¡÷" );
        do
        {
            ptr = strstr( ++ptr , " " );
            while(isspace(*ptr))
                ptr++;
            ptr_rhs = ptr;
            ptr = strstr( ptr_rhs , "|" );
            if ( ptr != NULL &&  *(ptr+1) != '|' )
            {
                for ( j = 1 ; isspace(*(ptr-j)) ; j++ )
                *(ptr-j) = '\0';
            }
            
            insert_grammar( ++i , ptr_lhs , ptr_rhs );
            
        } while ( ptr != NULL &&  *(ptr+1) != '|' ); 
    }
    
    
    // remove nonterminal in termainal table
    T_ptr = nonterminal_start;
    i = 0;
    while ( T_ptr != NULL )
    {
        T_tmp = search_token( terminal_start , T_ptr->string );
        delete_token( &terminal_start , T_tmp );
        T_ptr = T_ptr->next;
        i++;
    }
    num_of_nonterm = i;

    for ( T_ptr = terminal_start , i =0 ; T_ptr != NULL ; T_ptr = T_ptr->next , i++ )
    {
        insert_token( &first_set_start , &first_set_end , T_ptr->string );        // make first set table
    }
    num_of_term = ++i;
    
    fill_frist_set();
    fill_follow_set();
    i = 0;
    view = fopen("state.txt","w");
    //view = stdout;
    out = fopen("go_to_table.csv","w");
    
    build_CFSM();
    

    
    // build goto table
    parser_table = (PARSER**) malloc( sizeof(PARSER) * state_num );
    for( i = 0 ; i < state_num ; i++ )
    {
        parser_table[i] = (PARSER*) malloc( sizeof(PARSER) * ( num_of_term  + num_of_nonterm ));
        for( j = 0 ; j < num_of_term  + num_of_nonterm ; j++ )
        {
            parser_table[i][j].go_to = go_to_state(i,j+1);
            if( j+1 != make_id("$") )
                parser_table[i][j].action = SHIFT;
            else
                parser_table[i][j].action = ACCEPT;
                
            if( parser_table[i][j].go_to == 0 )
                parser_table[i][j].action = ERROR;
        }
    }
    
    // comput lalr lookahead
    build_LALR_lookahead();

    fprintf(out,"\t,");
    for( j = 0 ; j < num_of_term  + num_of_nonterm ; j++ )
    {
        if( j+1 != make_id(",") )
            fprintf(out," %s,",idtostr(j+1));
        else
           fprintf(out," ' ,");
    }
    fprintf( out ,"\n");
    
    // build action table
    for( i = 0 , state_ptr = state_start ; i < state_num ; i++ , state_ptr = state_ptr->next )
    {
        fprintf(out,"state%d,",i); 

        for( j = 0 ; j < num_of_term  + num_of_nonterm ; j++ )
        { 
            for( config_ptr = state_ptr->config_set ; config_ptr != NULL ; config_ptr = config_ptr->set_next )
            {
                if( config_ptr->dot == NULL && search_set( config_ptr->lookahead , idtostr(j+1) ) == TRUE )
                {
                    if(parser_table[i][j].go_to == 0)
                    {
                        //printf("hit\n");
                        parser_table[i][j].go_to = config_ptr->rule->rule;
                        parser_table[i][j].action = REDUCE;
                    }
                    //fprintf(out,"R%02d,",parser_table[i][j].go_to);
                }
            }
            if(parser_table[i][j].go_to > 0)
            {
                if( parser_table[i][j].action == SHIFT )                        
                    fprintf( out , "S%02d,",parser_table[i][j].go_to);
                else
                    fprintf( out , "R%02d,",parser_table[i][j].go_to);
            }
            else
                fprintf( out , ",");                
        }
        fprintf( out ,"\n"); 
    }
    fclose(out);
    
    
    //printf( "%d\n", go_to_state(4,5) );
    for ( i = 0 ; i < state_num ; i++ )
    {
        view_state(i);
        fprintf(view,"\n");
        //getch();
        //system("cls");
	}
    shift_reduce_driver( TreeView1 );

    free_token(&terminal_start);
    free_token(&nonterminal_start);
    free_grammar();
    free_state(&state_start);
    //free_config(&config_start);
    
	//system("pause");
	return 0;
}
示例#7
0
文件: htmlex.c 项目: dacap/htmlex
/* process the file `in' and output the result to `out' file */
void process_file (STREAM *in, STREAM *out)
{
  STREAM *old_i_stream = _i_stream;
  STREAM *old_o_stream = _o_stream;
  char *old_current_line = current_line;
  char *old_current_col = current_col;
  char *s, buf[MAX_BYTES];

  _i_stream = in;
  _o_stream = out;

  old_current_line = current_line;
  old_current_col = current_col;

  new_token (TOK_SPACE);
  update_state ();

  while (stgets (buf, MAX_BYTES, in)) {
    for (s = buf; *s; s++) {
      /* tag beginning */
      if ((*s == '<') && (s[1] == '!')) {
	int c, i, used = FALSE;
	int restore = TRUE;
	char *tag = s + 1;

	/* jump the comment? */
	if ((s[2] == '-') && (s[3] == '-')) {
	  if (!kill_comments)
	    stputs ("<!", out);
	  s += 2;
	  for (;;) {
	    if (strncmp (s, "-->", 3) == 0) {
	      if (!kill_comments)
		stputs ("-->", out);
	      s += 2;
	      break;
	    }
	    else if (*s == 0) {
	      if (!stgets (buf, MAX_BYTES, in))
		break;
	      s = buf;
	    }
	    else {
	      if (!kill_comments)
		stputc (*s, out);
	      s++;
	    }
	  }
	  continue;
	}

	/* jump nested tags */
	for (c = 0;; s++) {
	  if (*s == '<')
	    c++;
	  else if (*s == '>') {
	    c--;
	    if (c == 0)
	      break;
	  }
	  else if (*s == 0) {
	    if (!stgets (buf + strlen (buf), MAX_BYTES - strlen (buf), in))
	      break;
	    s--;
	  }
	}

	c = *s;
	*s = 0;

	log_printf (2, "tag found: \"%s\"\n", tag + 1);

	/* check for <!arg...> */
	if (strncmp (tag + 1, "arg", 3) == 0) {
	  if (can_attach) {
	    /* <!args...> */
	    if (tag[4] == 's') {
	      char temp[32];
	      sprintf (temp, "%d", nargs);
	      stputs (temp, out);
	    }
	    /* <!arg[1-9][0-9]*...> */
	    else {
	      int arg = strtol (tag + 4, NULL, 10);
	      if ((arg > 0) && (arg <= nargs) && (args[arg - 1]))
		stputs (args[arg - 1], out);
	    }
	  }
	  used = TRUE;
	}
	/* check for built-ins functions <!...> */
	if (!used) {
	  for (i = 0; i < ntags; i++) {
	    if (strncmp (tag + 1, tags[i].name, strlen (tags[i].name)) == 0) {
	      int x = tag[1 + strlen (tags[i].name)];
	      if (IS_BLANK (x) || (!x)) {
		char *tok, *argv[MAX_ARGS];
		char *replacement;
		char *holder = NULL;
		int argc = 0;

		for (tok = own_strtok (tag + 2, &holder),
		     tok = own_strtok (NULL, &holder); tok;
		     tok = own_strtok (NULL, &holder))
		  argv[argc++] = tok;

		if ((tags[i].if_tag) || (can_attach)) {
		  current_line = buf;
		  current_col = s + 1;

		  /* call the tag procedure */
		  replacement = (*tags[i].proc) (argc, argv);

		  if (s != current_col - 1) {
		    s = current_col - 1;
		    restore = FALSE;
		  }

		  /* text to replace */
		  if (replacement) {
		    stputs (replacement, out);
		    free (replacement);
		  }

		  log_printf (2, "tag \"%s\" was processed\n", tags[i].name);
		}
		else
		  log_printf (2, "tag \"%s\" wasn't processed\n",
			      tags[i].name);

		used = TRUE;
		break;
	      }
	    }
	  }
	}
	/* check for user functional macros <!...> */
	if (!used && can_attach) {
	  char *replacement = function_macro (macros_space[nmacros_space-1],
					      tag);
	  if (replacement) {
	    stputs (replacement, out);
	    free (replacement);
	    used = TRUE;
	  }
	}
	/* well, this is an unknown tag */
	if (!used) {
	  char *ptag = process_text (tag);

	  if (can_attach)
	    stputc ('<', out);

	  if (ptag) {
	    if (can_attach)
	      stputs (ptag, out);

	    free (ptag);
	  }

	  if (can_attach)
	    stputc ('>', out);
	}

	if (restore) {
	  if (!c)
	    s--;
	  else
	    *s = c;
	}
      }
      /* put a character in the output file */
      else if (can_attach) {
	char *replacement = NULL;
	int c, length = 0;

	/* check for macros */
	for (c = 0; c < nmacros_space; c++) {
	  replacement = replace_by_macro (macros_space[c], s, &length);
	  if (replacement)
	    break;
	}

	/* just put the character */
	if (!replacement) {
	  stputc (*s, out);
	}
	/* put the value of the macro */
	else {
	  stputs (replacement, out);
	  s += length - 1;
	  free (replacement);
	}
      }
    }
  }

  delete_token ();
  update_state ();

  _i_stream = old_i_stream;
  _o_stream = old_o_stream;

  current_line = old_current_line;
  current_col = old_current_col;
}
示例#8
0
/* Return:
   NULL for exception;
   some object not equal to NotImplemented if it is implemented
     (this latter object may not be a Boolean).
*/
PyObject *
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
{
	PyObject *res;

	assert(Py_LT <= op && op <= Py_GE);

	compare_nesting++;
	if (compare_nesting > NESTING_LIMIT &&
		(v->ob_type->tp_as_mapping
		 || (v->ob_type->tp_as_sequence
		     && !PyString_Check(v)
		     && !PyTuple_Check(v)))) {

		/* try to detect circular data structures */
		PyObject *token = check_recursion(v, w, op);
		if (token == NULL) {
			res = NULL;
			goto Done;
		}
		else if (token == Py_None) {
			/* already comparing these objects with this operator.
			   assume they're equal until shown otherwise */
			if (op == Py_EQ)
				res = Py_True;
			else if (op == Py_NE)
				res = Py_False;
			else {
				PyErr_SetString(PyExc_ValueError,
					"can't order recursive values");
				res = NULL;
			}
			Py_XINCREF(res);
		}
		else {
			res = do_richcmp(v, w, op);
			delete_token(token);
		}
		goto Done;
	}

	/* No nesting extremism.
	   If the types are equal, and not old-style instances, try to
	   get out cheap (don't bother with coercions etc.). */
	if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
		cmpfunc fcmp;
		richcmpfunc frich = RICHCOMPARE(v->ob_type);
		/* If the type has richcmp, try it first.  try_rich_compare
		   tries it two-sided, which is not needed since we've a
		   single type only. */
		if (frich != NULL) {
			res = (*frich)(v, w, op);
			if (res != Py_NotImplemented)
				goto Done;
			Py_DECREF(res);
		}
		/* No richcmp, or this particular richmp not implemented.
		   Try 3-way cmp. */
		fcmp = v->ob_type->tp_compare;
		if (fcmp != NULL) {
			int c = (*fcmp)(v, w);
			if (c < 0 && PyErr_Occurred()) {
				res = NULL;
				goto Done;
			}
			res = convert_3way_to_object(op, c);
			goto Done;
		}
	}

	/* Fast path not taken, or couldn't deliver a useful result. */
	res = do_richcmp(v, w, op);
Done:
	compare_nesting--;
	return res;
}