Flex / Bison: Poor Token Management?

I have a problem in my lexer and in my parser.

Firstly, in my lexer, I have a line like this:

"if"    beginScope(stOTHER); return IF;

And in my parser:

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

In this code:

if(sth) {
    dosth;
}

if(other) {
    doothersth;
}

beginScope will be called twice because (I think) Bison doesn't know where the statement ends if, so when it finds a token if, it takes it as the end if, and read it a second time to run another statement if...

Please help me...

+5
source share
1 answer

As Zack noted in the comments, you should call beginScopefrom the parser action:

stmt: IF { beginScope(stOTHER); } '(' exp ')' stmts
+1
source

All Articles