2009-12-15 11 views
7

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

+0

Strettamente legato a: http://stackoverflow.com/q/1920604/15168 e http://stackoverflow.com/q/1909166/15168 (anche se non proprio un duplicato di entrambi). –

risposta

4

Consiglio sempre this page per le persone che vogliono imparare lex/yacc (o flex/bisonte)

+0

Il collegamento è rotto. –

+1

Non più quando ho controllato ora. – Wernsey

+0

L'esempio fornito non utilizza scan_string. Utile per il generale generico, ma non per la domanda –

6

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; 
} 
+1

come si dichiara yy_scan_string nella prima sezione del bisonte? Inoltre devo aggiungere qualcosa in flex? – Ruturaj

+0

str_input dovrebbe essere meglio const char * in questo caso. –

15

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; 
} 
+1

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

3

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); 
}