1

I'm trying to implement a parser in Jison. The parser has support for both JSON objects: { a: 1 } and blocks: if (true) { statement(); }.

My grammar looks like:

block:
  : '{' '}'
  | '{' statementList '}'
  ;

objectExpression:
  : '{' '}'
  | '{' properties '}'
  ;

There's a {} rule in both block and objectExpression because I need to support both empty objects and empty blocks.

It works, but it outputs lots of warnings such as:

Conflict in grammar: multiple actions possible when lookahead token is ; in state 52
- reduce by rule: objectExpression -> { }
- reduce by rule: block -> { }

How can I fix or suppress these warnings?

Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288

2 Answers2

2

The string {} could be parsed either as a block, or as an objectExpression. You have a few choices.

In Javascript parsers, if there is an ambiguity between block and objectExpression, it will choose the one that comes first in the parent rule. In the following expression, the empty block will be preferred over the empty object.

expression:
    : block
    | objectExpression
    ;

The parser will complain about any ambiguity (also known as shift/reduce conflict), but it will still work. Assuming the Jison parser is compatible with the Bison parser, use the expect declaration to suppress the warning.

You could also make blocks and objectExpressions only appear in different contexts (e.g. blocks can only appear after if statements or for statements). A third choice would be to remove support for empty objects.

Joshua
  • 2,431
  • 15
  • 23
1

I ended up by adding another lex rule:

\{\} return 'EMPTY_OBJECT';

and changing the objectExpression to:

objectExpression:
  : 'EMPTY_OBJECT'
  | '{' properties '}'
  ;

It worked (even though it's a bit ugly).

Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
  • I'm skeptical that this solution actually works (and not just because it confusingly makes `{}` and `{ }` syntactically distinct). Test it thoroughly. The correct solution will involve identifying the contexts in which the two empty braced syntax can occur. – rici Oct 19 '14 at 22:18