0

I am trying to get the hang of parsing by defining a very simple language in Jison (a javascript parser). It accepts the same / very similar syntax to bison.

Here is my grammar:

%token INT TRUE FALSE WHILE DO IF THEN ELSE LOCATION ASSIGN EOF DEREF

%left "+"
%left ">="

/* Define Start Production */
%start Program 

/* Define Grammar Productions */
%%

Program
    : Statement EOF
    ;

Statement
    : Expression
    | WHILE BoolExpression DO Statement
    | LOCATION ASSIGN IntExpression
    ;

Expression
    : IntExpression
    | BoolExpression
    ;

IntExpression
    : INT IntExpressionRest
    | IF BoolExpression THEN Statement ELSE Statement
    | DEREF LOCATION
    ;   

IntExpressionRest
    : /* epsilon */
    | "+" IntExpression
    ;

BoolExpression
    : TRUE
    | FALSE
    | IntExpression ">=" IntExpression
    ;

%%

I am getting one shift/reduce conflict. The output of Jison is here:

Conflict in grammar: multiple actions possible when lookahead token is >= in state 6
- reduce by rule: Expression -> IntExpression
- shift token (then go to state 17)

States with conflicts:
State 6
  Expression -> IntExpression . #lookaheads= EOF >= THEN DO ELSE
  BoolExpression -> IntExpression .>= IntExpression #lookaheads= EOF DO THEN ELSE >=
user2269972
  • 31
  • 1
  • 2

2 Answers2

1

Your shift reduce conflict is detected because the >= is in the follow set of the Expression non-terminal. This is basically caused by the fact that a Statement can be an Expression and IntExpression can end with a statement. Consider the following input IF c THEN S1 ELSE S2 >= 42, if you had parentheses to disambiguate then this could be interpreted both as (IF c THEN S1 ELSE S2) >= 42 and IF c THEN S1 ELSE (S2 >= 42). Since the shift is preferred over the reduce, the latter will be chosen.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
-1

Your issue comes from

 IF BoolExpression THEN Statement ELSE Statement

If the Statement after THEN contains an IF, how do you know if the ELSE belongs to the first or second IF? See here for more information: http://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html

The only 100% non-ambiguous fix is to require some sort of delimiter around your if/else statements (most languages use the brackets "{" and "}"). Ex,

 IF BoolExpression THEN '{' Statement '}' ELSE '{' Statement '}'
Josh
  • 1,574
  • 1
  • 15
  • 36
  • Sorry, no if/then/else problem this time. His `ELSE` is not optional which is a very good way too to avoid this standard pitfall. – Bryan Olivier Apr 14 '13 at 00:34