Voglio analizzare una stringa che fornisco al parser nella funzione principale di yacc. So che questo potrebbe essere fatto usando yy_scan_string
ma non so come usarlo. Ho cercato sul web e sulle pagine man ma non mi è ancora chiaro. Mi aiuti per favore.come usare yy_scan_string in lex
risposta
ho trovato e l'esempio qui a me stesso. Possa esso può essere utile per voi:
Questo funziona per me. Ho questo codice nella sezione subroutine (vale a dire la terza sezione) del mio file Bison:
struct eq_tree_node *parse_equation(char *str_input)
{
struct eq_tree_node *result;
yy_scan_string(str_input);
yyparse();
/* to avoid leakage */
yylex_destroy();
/* disregard this. it is the function that I defined to get
the result of the parsing. */
result = symtab_get_parse_result();
return result;
}
come si dichiara yy_scan_string nella prima sezione del bisonte? Inoltre devo aggiungere qualcosa in flex? – Ruturaj
str_input dovrebbe essere meglio const char * in questo caso. –
Nel caso qualcuno ha bisogno del campione per una lexer rientrante:
int main(void)
{
yyscan_t scanner;
YY_BUFFER_STATE buf;
yylex_init(&scanner);
buf = yy_scan_string("replace me with the string youd like to scan", scanner);
yylex(scanner);
yy_delete_buffer(buf, scanner);
yylex_destroy(scanner);
return 0;
}
Nel caso in cui qualcun altro sta ricevendo il simbolo non è definito o altri errori di questo tipo quando si prova questo: ricordarsi di includere '% opzione reentrant' nel file lexer. – chacham15
Questo ha funzionato per me ... uso yy_scan_string()
int main(int argc, char **argv)
{
char Command[509];
int ReturnVal;
char input[40] = "This is my input string";
/*Copy string into new buffer and Switch buffers*/
yy_scan_string (input);
/*Analyze the string*/
yylex();
/*Delete the new buffer*/
yy_delete_buffer(YY_CURRENT_BUFFER);
}
Strettamente legato a: http://stackoverflow.com/q/1920604/15168 e http://stackoverflow.com/q/1909166/15168 (anche se non proprio un duplicato di entrambi). –