void fill_predict_set(void) { GRAMMAR *G_ptr; TOKEN *ptr , *first , *follow , *predict; SET *set_ptr , *set_srtat , *set_end; predict = (TOKEN*) malloc(sizeof(TOKEN)); predict->set = NULL; predict->next = NULL; strncpy( predict->string , "predict" , strlen("predict")+1 ); for ( G_ptr = G_start ; G_ptr != NULL ; G_ptr = G_ptr->next ) { set_srtat = NULL; set_end = NULL; //predict = search_token( predict_set_start , G_ptr->lhs ); for ( ptr = G_ptr->rhs_string ; ptr != NULL ; ptr = ptr->next ) { first = search_token( first_set_start , ptr->string ); if ( first == NULL ) { continue; } else if ( search_set( first , "£f" ) == TRUE ) { add_set( predict , first ); delete_set( &(predict->set) , "£f" ); continue; } else { add_set( predict , first ); break; } } if ( ptr == NULL ) { follow = search_token( follow_set_start , G_ptr->lhs ); add_set( predict , follow ); } for( set_ptr = predict->set ; set_ptr != NULL ; set_ptr = set_ptr->next ) { insert_predict( &set_srtat , &set_end , set_ptr->string ); //printf("%s ",set_ptr->string); } G_ptr->set = set_srtat; //printf("\n"); //view_token(predict); free_set(&(predict->set)); } }
void fill_frist_set(void) { TOKEN *term , *nonterm , *ptr , *ptr2 , *ptr3 , *ptr_end; GRAMMAR *gram , *G_ptr; term = terminal_start; nonterm = nonterminal_start; gram = G_start; int change; // nonterminal for ( G_ptr = G_start ; G_ptr != NULL ; G_ptr = G_ptr->next ) { //printf("%s\n",gram->rhs); if ( strcmp( G_ptr->rhs_string->string , "£f" ) == 0 ) { ptr = search_token( first_set_start , G_ptr->lhs ); insert_set( ptr , "£f" ); // add £finto first set } } // terminal for ( term = terminal_start ; term != NULL ; term = term->next ) { ptr = search_token( first_set_start , term->string ); insert_set( ptr , term->string ); for ( gram = G_start ; gram != NULL ; gram = gram->next ) { if( strcmp( gram->rhs_string->string , term->string ) == 0 ) { ptr = search_token( first_set_start , gram->lhs ); insert_set( ptr , term->string ); // add terminal into first set } } } // change do { change = FALSE; for ( gram = G_start ; gram != NULL ; gram = gram->next ) { if ( comput_first( gram->rhs_string , first_set_start , gram->lhs ) == TRUE ) change = TRUE; } } while (change); }
/** @brief Implement the param_list method. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Provide a list of bind variables for a specified query, along with their various attributes. Method parameters: - query token, as previously returned by the .prepare method. Returns: A (possibly empty) JSON_HASH, keyed on the names of the bind variables. The data for each is another level of JSON_HASH with a fixed set of tags: - "label" - "type" - "description" - "default_value" (as a jsonObject) - "actual_value" (as a jsonObject) Any non-existent values are represented as JSON_NULLs. */ int doParamList( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token from a method parameter const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Returning list of bind variables for token %s", token ); osrfAppRespondComplete( ctx, oilsBindVarList( query->state->bindvar_list ) ); return 0; }
/** @brief Execute an SQL query and return a result set. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Method parameters: - query token, as previously returned by the .prepare method. Returns: A series of responses, each of them a row represented as an array of column values. */ int doExecute( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Executing query for token \"%s\"", token ); if( query->state->error ) { osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state, "No valid prepared query available for query id # %d", query->query->id )); osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "No valid prepared query available" ); return -1; } else if( buildSQL( query->state, query->query )) { osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state, "Unable to build SQL statement for query id # %d", query->query->id )); osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Unable to build SQL statement" ); return -1; } jsonObject* row = oilsFirstRow( query->state ); while( row ) { osrfAppRespond( ctx, row ); row = oilsNextRow( query->state ); } if( query->state->error ) { osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state, "Unable to execute SQL statement for query id # %d", query->query->id )); osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Unable to execute SQL statement" ); if( query->state->panic ) { osrfLogError( OSRF_LOG_MARK, sqlAddMsg( query->state, "Database connection isn't working" )); osrfAppSessionPanic( ctx->session ); } return -1; } osrfAppRespondComplete( ctx, NULL ); return 0; }
/** @brief Implement the bind_param method. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Apply values to bind variables, overriding the defaults, if any. Method parameters: - query token, as previously returned by the .prepare method. - hash of bind variable values, keyed on bind variable names. Returns: Nothing. */ int doBindParam( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token from a method parameter const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Binding parameter(s) for token %s", token ); jsonObject* bindings = jsonObjectGetIndex( ctx->params, 1 ); if( !bindings ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "No parameter provided for bind variable values" ); return -1; } else if( bindings->type != JSON_HASH ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter for bind variable values: not a hash" ); return -1; } if( 0 == bindings->size ) { // No values to assign; we're done. osrfAppRespondComplete( ctx, NULL ); return 0; } osrfHash* bindvar_list = query->state->bindvar_list; if( !bindvar_list || osrfHashGetCount( bindvar_list ) == 0 ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "There are no bind variables to which to assign values" ); return -1; } if( oilsApplyBindValues( query->state, bindings )) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Unable to apply values to bind variables" ); return -1; } else { osrfAppRespondComplete( ctx, NULL ); return 0; } }
void fill_follow_set(void) { TOKEN *term , *nonterm , *ptr , *ptr2 , *ptr3 , *ptr_end; TOKEN *test; GRAMMAR *gram , *G_ptr; int change; int lambda; test = (TOKEN*) malloc(sizeof(TOKEN)); // set start symbol ptr = follow_set_start; insert_set( ptr , "£f" ); do { change = FALSE; for( G_ptr = G_start ; G_ptr != NULL ; G_ptr = G_ptr->next ) // start with first production rule { for( nonterm = G_ptr->rhs_string ; nonterm != NULL ; nonterm = nonterm->next ) { if( search_token( nonterminal_start , nonterm->string ) == NULL ) continue; // follow[B] = follow[B] U (comput_first(£]) - £f) if ( comput_first_f( nonterm->next , follow_set_start , nonterm->string ) == TRUE ) change = TRUE; // if ( £f in comput_first(£]) then follow[B] = follow[B] U follow[A]) if( is_lambda == TRUE ) { ptr = search_token( follow_set_start , G_ptr->lhs ); ptr2 = search_token( follow_set_start , nonterm->string ); //printf(" %s %s \n",ptr->string , ptr2->string); if ( add_set( ptr2 , ptr) == TRUE ) change = TRUE; } } } } while ( change ); }
int insert_grammar( int rule , char *lhs , char *rhs ) { GRAMMAR *ptr , *create; TOKEN *token_start , *token_end; char string[64] , *c_ptr; token_start = NULL; token_end = NULL; create = (GRAMMAR*) malloc( sizeof(GRAMMAR) ); ptr = G_end; create->rule = rule; create->set = NULL; create->next = NULL; strncpy( create->lhs , lhs , strlen(lhs)+1 ); strncpy( create->rhs , rhs , strlen(rhs)+1 ); c_ptr = rhs; while ( c_ptr != NULL ){ sscanf( c_ptr , "%s" , string ); insert_token( &token_start , &token_end , string ); if ( search_token( terminal_start , string ) == NULL && strcmp( string , "£f" ) != 0 ) // £f is not terminal { insert_token( &terminal_start , &terminal_end , string ); // make terminal table } c_ptr = strstr( c_ptr , " " ); if ( c_ptr == NULL ) break; c_ptr++; } create->rhs_string = token_start; if( G_start == NULL ) { G_start = create; G_end = create; } else { ptr->next = create; G_end = create; } }
static void action_A(void) { int pos; /* check if yytext is a token? If yes, return token, otherwise return yytext. --sun */ if ((pos = search_token(yytext)) != -1) { lsd_parse(tokens[pos].token); } else { /* it is an observable or agentname*/ lsd_lval.s = strdup(yytext); lsd_parse(ID); } }
int _file_ply::next_token() { char *p1,*p2; char Aux_token[100]; int Num_char; do { if (strlen(Buffer)==0) { if (read_line()==-1) return(-1); while (Buffer[0]=='#') if (read_line()==-1) return(-1); } p1=Buffer; while ((*p1==' ' || *p1=='\t') && *p1!='\n') p1++; p2=p1; while (*p2!=' ' && *p2!='\t' && *p2!='\n') p2++; Num_char=p2-p1; if (Num_char>99) { error("number of characters for token is too long\n"); return(-1); } if (Num_char!=0) { strncpy(Aux_token,p1,Num_char); Aux_token[Num_char]='\0'; } if (*p2=='\n') Buffer[0]='\0'; else strcpy(Buffer,p2); } while (Num_char==0); if (!is_number(Aux_token)) { //printf("es un numero %s\n",Aux_token); Yylval.Real=atof(Aux_token); return((int)_tokens(NUMBER)); } else { //printf("es texto %s\n",Aux_token); Yylval.Text=Aux_token; return(search_token(Aux_token)); } }
int _file_ply::next_token() { char Aux_token[100]; char *Aux_token1; int Num_char=0; do{ if (strlen(Buffer)==0){ // leer una linea si no se ha leido antes if (read_line()==-1) return(-1); // si es un comentario lee la siguiente while (Buffer[0]=='#') if (read_line()==-1) return(-1); } Aux_token1= strtok(Buffer," \t\n"); if (Aux_token1!=NULL){ strcpy(Aux_token,Aux_token1); Num_char=strlen(Aux_token); strcpy(Buffer,Buffer+Num_char+1); } else{// por si hay delimitadores al final de una linea Num_char=0; Buffer[0]=0; } } while (Num_char==0); if (!is_number(Aux_token)) { //printf("es un numero %s\n",Aux_token); Yylval.Real=atof(Aux_token); return((int)_tokens(NUMBER)); } else { //printf("es texto %s\n",Aux_token); Yylval.Text=Aux_token; return(search_token(Aux_token)); } }
/** @brief Construct an SQL query, but without executing it. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Method parameters: - query token, as previously returned by the .prepare method. Returns: A string containing an SQL query.. */ int doSql( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Returning SQL for token \"%s\"", token ); if( query->state->error ) { osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state, "No valid prepared query available for query id # %d", query->query->id )); osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "No valid prepared query available" ); return -1; } else if( buildSQL( query->state, query->query )) { osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( query->state, "Unable to build SQL statement for query id # %d", query->query->id )); osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Unable to build SQL statement" ); return -1; } osrfAppRespondComplete( ctx, jsonNewObject( OSRF_BUFFER_C_STR( query->state->sql ))); return 0; }
/** @brief Return a list of column names for the SELECT list. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Method parameters: - query token, as previously returned by the .prepare method. Returns: An array of column names; unavailable names are represented as nulls. */ int doColumns( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token from a method parameter const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Listing column names for token %s", token ); jsonObject* col_list = oilsGetColNames( query->state, query->query ); if( query->state->error ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Unable to get column names" ); if( query->state->panic ) { osrfLogError( OSRF_LOG_MARK, sqlAddMsg( query->state, "Database connection isn't working" )); osrfAppSessionPanic( ctx->session ); } return -1; } else { osrfAppRespondComplete( ctx, col_list ); return 0; } }
static void action_A(void) { int pos; char new_yytext[256]; extern int inPrefix; /* which mode of VA are we in: SUN_A or A_SUN... */ if ((pos = search_constant(yytext)) != -1) { yylval.i = constants[pos].val; yyparse(constants[pos].token); } else if ((pos = search_token(yytext)) != -1) { yyparse(tokens[pos].token); } else { /* it is a variable */ /* printf("yytext= %s %s %i %i %i\n", yytext, agentName, appAgentName, append_NoAgentName, ScoutAppAgentName); */ if (*agentName != 0 && appAgentName > 0 && append_NoAgentName > 0 && ScoutAppAgentName > 0) { /* for distributed tkEden -- sun */ new_yytext[0] = '\0'; if (yytext[0] == '_' && inPrefix) { strcat(new_yytext, "_"); yytext++; } if (inPrefix) strcat(new_yytext, agentName); else strcat(new_yytext, yytext); strcat(new_yytext, "_"); if (inPrefix) strcat(new_yytext, yytext); else strcat(new_yytext, agentName); strcpy(yytext, new_yytext); } yylval.v = lookUp(yytext); yyparse((yylval.v)->type); } }
/** @brief Return a list of previously generated error messages for a specified query. @param ctx Pointer to the current method context. @return Zero if successful, or -1 if not. Method parameters: - query token, as previously returned by the .prepare method. Returns: A (possibly empty) array of strings, each one an error message generated during previous operations in connection with the specified query. */ int doMessages( osrfMethodContext* ctx ) { if(osrfMethodVerifyContext( ctx )) { osrfLogError( OSRF_LOG_MARK, "Invalid method context" ); return -1; } // Get the query token from a method parameter const jsonObject* token_obj = jsonObjectGetIndex( ctx->params, 0 ); if( token_obj->type != JSON_STRING ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid parameter; query token must be a string" ); return -1; } const char* token = jsonObjectGetString( token_obj ); // Look up the query token in the session-level userData CachedQuery* query = search_token( ctx, token ); if( !query ) { osrfAppSessionStatus( ctx->session, OSRF_STATUS_BADREQUEST, "osrfMethodException", ctx->request, "Invalid query token" ); return -1; } osrfLogInfo( OSRF_LOG_MARK, "Returning messages for token %s", token ); jsonObject* msgs = jsonNewObjectType( JSON_ARRAY ); const osrfStringArray* error_msgs = query->state->error_msgs; int i; for( i = 0; i < error_msgs->size; ++i ) { jsonObject* msg = jsonNewObject( osrfStringArrayGetString( error_msgs, i )); jsonObjectPush( msgs, msg ); } osrfAppRespondComplete( ctx, msgs ); return 0; }
int propagate( int state , Config_set *A , Config_set *B ) { STATE *shift_state; Config_set *C_ptr , *ptr; TOKEN *T_ptr , *first; TOKEN *dot; TOKEN *L2; TOKEN *L1; int i; L1 = A->lookahead; dot = A->dot; L2 = B->lookahead; //First(£^L) for( T_ptr = dot->next ; T_ptr != NULL ; T_ptr = T_ptr->next ) { first = search_token( first_set_start , T_ptr->string ); if (first == NULL ) continue; if ( search_set(first,"£f") == TRUE ) { add_set(L2,first); delete_set( &(L2->set) , "£f" ); continue; } else { if ( add_set(L2,first) == FALSE ) return 0; break; } } //First(£^L) , £^ is lambda if(T_ptr == NULL) { add_set( L2 , L1 ); } //printf("%d\n", parser_table[state][make_id(dot->string)].go_to ); if( B->dot == NULL ) return 0; i = parser_table[state][make_id(B->dot->string)-1].go_to; //printf("%d %s -> %d\n",state , B->dot->string , i ); //view_state(state); if( i == state ) return 0; // shift state for( shift_state = state_start ; i != shift_state->statenum ; shift_state = shift_state->next ); for( C_ptr = shift_state->config_set ; C_ptr != NULL && B->rule != C_ptr->rule ; C_ptr = C_ptr->set_next ); { // if ( i == 105 ) // printf("hit\n"); propagate_link( i , B , C_ptr ); } //comput lookahead by closue 0 C_ptr = shift_state->config_set; for( C_ptr = shift_state->config_set ; C_ptr != NULL && C_ptr->set_next != NULL ; C_ptr = C_ptr->set_next ) { for( ptr = shift_state->config_set ; ptr != NULL ; ptr = ptr->set_next ) { if ( C_ptr->dot == NULL ) continue; if ( strcmp( C_ptr->dot->string , ptr->rule->lhs ) == 0 ) { propagate( shift_state->statenum , C_ptr , ptr ); } } } return 0; }
int main() { char *regex = "dup.com"; char *str = "dup.com"; int token_position = -1; int last_regex_pos = 0; int last_str_pos = 0; int i = 0; int j = 0; int match = 0; int state = 0; /* * Initial states */ if (regex[i] == '*') state = '*'; // Start matching else state = 'c'; // Start comparing while (!match) { if (state == '0') break; switch(state) { case '*': i++; while (str[j] != '\0') { j++; } if (regex[i] == '\0') { match = 1; } else { j--; // Not NULL state = 's'; // Search for the token } break; case 's': // Search token token_position = search_token(str, j, regex[i]); /* * Remember where we last matched */ last_regex_pos = i; last_str_pos = token_position; state = 'm'; break; case 'm': // Match token j = token_position; // Start matching str at this position if (token_position != -1) { while (regex[i] != '\0' && str[j] != '\0') { if (regex[i] == str[j]) { i++; j++; } else { /* * If non-match char is a metachar then we * go to state '*' again */ if (regex[i] == '*') { state = '*'; } else { i = last_regex_pos; j = last_str_pos - 1; token_position = -1; // Reset token pos state = 's'; } break; } } // If regex and str matches then we say it's a MATCH if (regex[i] == '\0' && str[j] == '\0') { match = 1; } } else { match = 0; state = '0'; } break; case 'c': token_position = 0; state = 'm'; break; } } if (match) printf("Match"); else printf("No Match"); }
int comput_first_f( TOKEN *alpha , TOKEN *dst , char *dst_name ) { int change = FALSE; TOKEN *ptr , *ptr2 , *ptr3 , *ptr_end; TOKEN *tmp; is_lambda = FALSE; tmp = (TOKEN*) malloc(sizeof(TOKEN)); strncpy( tmp->string , "tmp" , 4 ); tmp->set = NULL; tmp->next = NULL; if( alpha == NULL ) { is_lambda = TRUE; return FALSE; } if ( strcmp( alpha->string , "£f" ) == 0 ) { //ptr = search_token( dst , dst_name ); //if ( insert_set( ptr , "£f" ) == TRUE ) is_lambda = TRUE; change = TRUE; } else { ptr = search_token( dst , dst_name ); ptr2 = search_token( first_set_start , alpha->string ); add_set( tmp , ptr2 ); delete_set( &(tmp->set) , "£f" ); if ( add_set( ptr , tmp ) == TRUE ) change = TRUE; free_set( &(tmp->set) ); for( ptr3 = alpha ; ptr3 != NULL ; ptr3 = ptr3->next ) { ptr2 = search_token( first_set_start , ptr3->string ); if( search_set( ptr2 , "£f" ) != TRUE ) break; if ( ptr3->next == NULL ) { ptr_end = ptr3; ptr3 = search_token( first_set_start , ptr_end->string ); if( search_set( ptr3 , "£f" ) == TRUE ) { if ( insert_set( ptr , "£f" ) == TRUE ) { is_lambda = TRUE; change = TRUE; } } break; } ptr2 = search_token( first_set_start , ptr3->next->string ); add_set( tmp , ptr2 ); delete_set( &(tmp->set) , "£f" ); if ( add_set( ptr , tmp ) == TRUE ) { delete_set( &(ptr->set) , "£f" ); change = TRUE; } free_set( &(tmp->set) ); } } free_token(&tmp); return change; }
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; }