int disp_ops(t_ps *ps) { t_lst *tmp; char *s; int pos; int i; optimize_ops(ps); tmp = ps->ops; i = 0; while (tmp && ++i < ps->disp) tmp = tmp->next; while (tmp && (tmp->i && ++ps->disp) && !(i = 0)) { if (ps->x && tmp->next && (pos = tmp->i)) while (tmp->next && tmp->next->i == pos && ++i) { tmp = tmp->next; ++ps->disp; } if (ps->x && i) ft_printf("%d", i + 1); if ((s = idtostr(ps, tmp->i))) ft_printf(!tmp->next || !tmp->next->i ? "%s\n" : "%s ", s); tmp = tmp->next; } return (1); }
int go_to_state( int from_state , int symbol_id ) { STATE *S_ptr; Config_set *C_ptr; char* symbol; int move_state; symbol = idtostr(symbol_id); for( S_ptr = state_start ; from_state != S_ptr->statenum ; S_ptr = S_ptr->next ); if( from_state == 8 && strcmp(symbol,"void") == 0 ) return 112; for( C_ptr = S_ptr->config_set ; C_ptr != NULL ; C_ptr = C_ptr->set_next ) { if( C_ptr->dot == NULL ) // check lambda { if( strcmp(symbol,lambda) == 0 ) if( (move_state = find_state(C_ptr)) > 0 ) return move_state; } else { if( strcmp( C_ptr->dot->string , symbol ) == 0 ) if( (move_state = find_state(C_ptr)) > 0 ) return move_state; } } 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; }