2015-11-06 71 views
0

我收到錯誤我得到一個左遞歸誤差在我ANTLR解析器語法

[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. 

我不到底哪個我的語法部分是引發此錯誤認識。和1個其他相同的錯誤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 
     ; 

我不知道是哪部分需要重新安排,以去除左遞歸和將不勝感激,如果有人能指出來。

回答

0

的問題似乎是這兩個選擇:

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

例輸入導致遞歸(即我可以重複最後一部分多次,我想和它仍然是這兩個規則有效的前綴):

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

在這兩種情況下,你可以不知道選擇哪個替代,直到你看到一個+-*ANDNOT)