Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex/Bison: Bad token management?

I've a problem in my lexer and in my parser.

First, in my lexer I've a line like that:

"if"    beginScope(stOTHER); return IF;

And in my parser:

stmt: IF '(' exp ')' stmts
...
stmts: stmt
       | '{' stmt_list '}'
       | '{' '}'

In a code like that:

if(sth) {
    dosth;
}

if(other) {
    doothersth;
}

beginScope will be called two times, because ( I think ) Bison don't know where is the end of the if statement, so when it found the IF token, he takes that as the end of the if statement, and read it a second time to start the other if statement...

Please help me...

like image 436
gnidmoo Avatar asked Nov 13 '22 05:11

gnidmoo


1 Answers

As Zack mentioned in the comments, you should call beginScope from a parser action:

stmt: IF { beginScope(stOTHER); } '(' exp ')' stmts
like image 62
vitaut Avatar answered Dec 19 '22 11:12

vitaut