Question in Flex (parser)
I want to ask you a question about Flex, the program for parsing code. Supposing I have an instruction like this one, in the rules part:
"=" BEGIN(attribution);
<attribution>{var_name} { fprintf(yyout, "="); ECHO; }
<attribution>";" BEGIN(INITIAL);
{var_name} is a regular expression that matches a variable's name, and all I want to do is to copy at the output all the attribution instructions, such as a = 3; or b = a;
My rule though cannot write with fprintf the left member of the attribution, but only = 3; or =a;
One solution for that might be that, after I make the match "=" and I am 开发者_如何学编程in the attribution state, to go 2 positions back as to get the left operand as well. How can I do that in Flex?
Why are you using flex for syntactical analysis?
What you are doing sounds like a bison stuff not a flex job. You'll be able to store previous token.
If you still want to use flex, you can use the / pattern. Using this may lead to inefficiencies and the lexer can be bogus; it depends of the whole rule set.
{var_name}/"=" { ECHO; BEGIN(attribution); }
See the flex manual.
精彩评论