0
votes

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

1 Answers

1
votes

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).