4
votes

I'm implementing a Javascript parser using Bison. The ECMAScript specification states that

ExpressionStatement:
    [lookahead ∉ { '{' , 'function'}] Expression ;

That breaks the ambiguity between "{ }" as a BlockStatement (empty statement block) and as an ExpressionStatement (empty object literal) because an ExpressionStatement just can't start with a '{' token, although an Expression could.

Example of an empty statement block:

if (a > 5) {}

Example of an empty object literal:

var a = {};

How can I specify in a bison/yacc grammar that some production should not start with some particular tokens? I mean, something like:

expressionStatement
    : %must-not-start-with('{', TOKEN_FUNCTION) expression ';'
    ;

I know I could duplicate all my expression rules to define an "ExpressionNotStartingWithOpenCurlyBraceOrFunction", but that would increase a lot the size of my grammar, so I'm trying to avoid it.

1

1 Answers

1
votes

There is no such directive in Bison. I see two more options you could explore. One would be to study the resulting conflict and see if you can resolve it using precedence directives (have a look at http://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html). Another would be to move to using a GLR parser, and resolve the ambiguity at runtime.

First option is probably easier, if applicable. But that would depend a lot from your grammar.