bison's reduction didn't work as expected

邮差的信 提交于 2019-12-02 16:50:28

问题


I'm trying to write a EPL parser,so I'm learning flex and bison.I try using it with following rules(SQL):

SELECT { cout<<"SELECT detected"<<endl;return SELECT; }
FROM { 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 FROM identifier_expr {    cout<<"select statement founded"<<endl; }
         ;

identifier_expr : DOL IDENTIFIER {
$$=$2;
cout<<"ident_expr:"<<$$<<endl;
}
;

all tokens and non-terminals have type "char*"

as the input from stdin is "select $abc from $ddd" reudction happend when lexer returned token FROM,in "identifier_expr" action ,output is "ident_expr:abc from" why this happened?


回答1:


You must create a copy of the token string (yytext) if you want to use it outside of the flex action. The string pointed to by yytext is a temporary value, and will be modified as soon as the lexer is re-entered.

See the bison FAQ, the flex manual, or any number of SO questions (which are harder to search for because many of the questioners misdiagnose the problem).



来源:https://stackoverflow.com/questions/16661791/bisons-reduction-didnt-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!