Startup limitation in yacc

I am using a Javascript parser using Bison. the ECMAScript specification states that

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

This breaks the ambiguity between "{}" as a BlockStatement (empty statement block) and as an ExpressionStatement expression (empty object literal), because the ExpressionStatement expression simply cannot start with a '{' token, although the expression can.

An example of an empty block:

 if (a > 5) {} 

An example of an empty object literal:

 var a = {}; 

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

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

I know that I could duplicate all my expression rules to define "ExpressionNotStartingWithOpenCurlyBraceOrFunction", but that would increase the size of my grammar, so I try to avoid this.

+4
source share
1 answer

There is no such directive in Bison. I see two more options that you could explore. You could study the conflict and see if you can resolve it using priority directives (see http://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html ). Another would be to switch to using the GLR analyzer and eliminate the ambiguity at runtime.

The first option is probably simpler if applicable. But it will depend on your grammar.

+1
source

Source: https://habr.com/ru/post/1487079/


All Articles