我的 antlr 解析器语法中出现左递归错误
I am getting a left recursion error in my antlr parser grammar
我遇到错误
[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
;
我不知道哪一部分需要重新排列以删除左递归,如果有人能指出,我将不胜感激。
问题似乎出在这两个备选方案上:
| WRITE^ OPENPAREN! exp CLOSEPAREN!
| WRITE^ OPENPAREN! boolexp CLOSEPAREN!
导致递归的示例输入(即我可以根据需要多次重复最后一部分,它仍然是这两个规则的有效前缀):
WRITE (((((((((((...
WRITE ( myVar + myVar + ...
在这两种情况下,在看到 +
、-
、*
、AND
、[=17= 之前,您无法知道选择哪个选项] 或 )
我遇到错误
[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
;
我不知道哪一部分需要重新排列以删除左递归,如果有人能指出,我将不胜感激。
问题似乎出在这两个备选方案上:
| WRITE^ OPENPAREN! exp CLOSEPAREN!
| WRITE^ OPENPAREN! boolexp CLOSEPAREN!
导致递归的示例输入(即我可以根据需要多次重复最后一部分,它仍然是这两个规则的有效前缀):
WRITE (((((((((((...
WRITE ( myVar + myVar + ...
在这两种情况下,在看到 +
、-
、*
、AND
、[=17= 之前,您无法知道选择哪个选项] 或 )