5

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

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Unarelith
  • 495
  • 3
  • 13
  • Can you call `beginScope()` for "{" in your Flex file? That way, you can `endScope()` when you see "}". Just have these single-character expressions return a token like OPENBRACE and CLOSEBRACE to Bison. – chrisaycock May 14 '12 at 19:23
  • No I can't, because beginScope needs a type in parameter, and there is not always a "{" as you can see – Unarelith May 14 '12 at 19:39
  • 3
    Move the `beginScope` operation into your parser actions. Use a [mid-rule action](http://www.gnu.org/software/bison/manual/html_node/Mid_002dRule-Actions.html) if necessary. The lexer must not have side effects, for exactly the reason you have discovered. – zwol May 14 '12 at 20:19
  • Oh yes! The mid-rule action is perfect, thanks!!! – Unarelith May 14 '12 at 22:08

1 Answers1

1

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

stmt: IF { beginScope(stOTHER); } '(' exp ')' stmts
vitaut
  • 49,672
  • 25
  • 199
  • 336