flex lexer - bison's reduction didn't work as expected -
i'm trying write epl parser,so i'm learning flex , bison.i try using following rules(sql):
select { cout<<"select detected"<<endl;return select; } { cout<<"from detected"<<endl;return from;} [a-za-z][0-9a-za-z]* { cout<<"identifier detected"<<endl;yylval.c=yytext; return identifier; } '$' { return dol;} [ \t] { cout<<"space founded:"<<int(yytext[0])<<endl; } \n { return eol;} . {}
and bison rules are:
sel_stmt : {cout<<"void"<<endl;} | select identifier_expr identifier_expr { cout<<"select statement founded"<<endl; } ; identifier_expr : dol identifier { $$=$2; cout<<"ident_expr:"<<$$<<endl; } ;
all tokens , non-terminals have type "char*"
as input stdin "select $abc $ddd" reudction happend when lexer returned token from,in "identifier_expr" action ,output "ident_expr:abc from" why happened?
you must create copy of token string (yytext
) if want use outside of flex action. string pointed yytext
temporary value, , modified lexer re-entered.
see bison faq, flex manual, or number of questions (which harder search because many of questioners misdiagnose problem).
Comments
Post a Comment