0

I am getting the error

    [fatal] rule statement has non-LL(*) decision due to recursive rule invocations reachable from alts 6,7. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.  

I don't know exactly which part of my grammar is throwing this error. and 1 other same error for alt 3,4

    parser grammar syn1;

    options {
      tokenVocab = lex1;
      buildAST=true;
    }


    program : 
         statements
         ;

    statements :
            statement ( SEMICOLON^ statement )*
        ;

    statement :
            variable ASSIGN^ exp
        |   SKIP
        |   IF^ boolexp THEN statement ELSE statement
        |   WHILE^ boolexp DO statement
        |   READ^ OPENPAREN! variable CLOSEPAREN! 
        |   WRITE^ OPENPAREN! exp CLOSEPAREN!
        |   WRITE^ OPENPAREN! boolexp CLOSEPAREN!
        |   WRITE^ OPENPAREN! STRING CLOSEPAREN!
        |   WRITELN
        |   OPENPAREN! statements CLOSEPAREN!
        ;

    boolexp : 
            boolterm ( AND^ boolterm )* 
        ;

    boolterm :
            NOT^ bool
        |   bool
        ;

    bool :
            TRUE
        |   FALSE
        |   exp EQUALS^ exp
        |   exp LESSEQUALS^ exp
        |   OPENPAREN! boolexp CLOSEPAREN!
        ;

    exp : 
            term (( ADD | SUBTRACT )^ term )*
        ;

    term :
            factor ( MULTIPLY^ factor ) *
        ;

    factor : 
            variable
        |   INTNUM
        |   OPENPAREN exp CLOSEPAREN
        ;

    variable :
            IDENTIFIERS
        ;

I don't know which part needs rearranging to remove the left recursion and would be grateful if someone could point it out.

Arthur Le Calvez
  • 413
  • 2
  • 7
  • 17

1 Answers1

0

The problem seems to be these two alternatives:

    |   WRITE^ OPENPAREN! exp CLOSEPAREN!
    |   WRITE^ OPENPAREN! boolexp CLOSEPAREN!

Example inputs that cause recursion (i.e. I can repeat last part as many times as I want and it's still a valid prefix of these two rules):

  • WRITE (((((((((((...
  • WRITE ( myVar + myVar + ...

In both cases, you can't know which alternative to choose until you see a +, -, *, AND, NOT or )

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43