int ape_consume_token() { int machine_id; int token_id = ape_get_token_id(); Machine current_machine = ape_parser.current_machine; int next_state = current_machine.token_transitions[current_machine.current_state][token_id]; printf("%d: (%d,%d) -> %d\t\t(Token ID: %d)\n", current_machine.machine_id, current_machine.current_state, token_id, next_state, ape_get_token_id()); if (next_state == APE_INVALID_STATE) { // could not read the token, let's check if we can change the machine machine_id = machine_id_consume_token(token_id); next_state = current_machine.machine_transitions[current_machine.current_state][machine_id]; if (machine_id != APE_INVALID_STATE) { // change the current machine change_ape_machine(machine_id, ape_parser.machines[machine_id].initial_state); current_machine.current_state = next_state; // enqueue the current machine push_apestack(current_machine); return ape_consume_token(); } else if (is_ape_in_final_state() && !is_apestack_empty()) { // check if there's a machine in the queue // retrieve the machine in its original state Machine m = pop_apestack(); change_ape_machine_pop(m.machine_id, m.current_state); return ape_consume_token(); } else { // could not consume the token in any machine return 0; } } else { go_to_state(next_state); return 1; } return 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; }
void cql_escape_sequences_remover_t::push_character(const char c) { switch (_state) { case ESC_SEQ_STATE_OUTSIDE: if(c == ESCAPE) go_to_state(ESC_SEQ_STATE_AFTER_ESCAPE); else if((unsigned char)c == CSI) go_to_state(ESC_SEQ_STATE_AFTER_ESCAPE_BRACKET); else if(is_control_character(c)) go_to_state(ESC_SEQ_STATE_OUTSIDE); else { _buffer.push_back(c); } break; case ESC_SEQ_STATE_AFTER_ESCAPE: if(c == '[') go_to_state(ESC_SEQ_STATE_AFTER_ESCAPE_BRACKET); else if(c == ']') go_to_state(ESC_SEQ_STATE_SKIP_TO_SEQ_END); else if(strchr("%#()", c) != 0) go_to_state(ESC_SEQ_STATE_SKIP_NEXT); else { /* current character is skipped */ go_to_state(ESC_SEQ_STATE_OUTSIDE); } break; case ESC_SEQ_STATE_AFTER_ESCAPE_BRACKET: if(c == '[') go_to_state(ESC_SEQ_STATE_SKIP_NEXT); else go_to_state(ESC_SEQ_STATE_SKIP_TO_SEQ_END); break; case ESC_SEQ_STATE_SKIP_NEXT: /* current character is skipped */ go_to_state(ESC_SEQ_STATE_OUTSIDE); break; case ESC_SEQ_STATE_SKIP_NEXT_2: /* current character is skipped */ go_to_state(ESC_SEQ_STATE_SKIP_NEXT); break; case ESC_SEQ_STATE_SKIP_TO_SEQ_END: if(strchr("ABCDEFGHIJKLMPXacdefghlmnqrsu`];", c) != 0) go_to_state(ESC_SEQ_STATE_OUTSIDE); /* current character is skipped */ break; } }