antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.22k stars 3.29k forks source link

Parser issues mutual left recursion error when the left-recursive part of a rule is in parenthesis #564

Open JFinis opened 10 years ago

JFinis commented 10 years ago

Here is the description of this problem from stackoverflow:

http://stackoverflow.com/questions/23626376/antlr4-mutual-left-recursion

Here is the issue:

 classOrInterfaceType 
    : (classOrInterfaceType) '.' Identifier
    | Identifier
    ;

This provokes a mutually left recursion error. When removing the parenthesis around classOrInterfaceType, then everything is fine. The parentheses are superfluous but are inserted by some grammar generation tools, so it would be fine if this would work!

parrt commented 10 years ago

True it breaks the pattern but (x) is same as x. why use (x) then?

sharwell commented 10 years ago

This issue will hopefully be resolved by the introduction of an optimization pass to remove unnecessary blocks sometime after the Tool is rewritten using ANTLR 4 grammars and parse trees instead of ANTLR 3 and ASTs which it uses now.

JFinis commented 10 years ago

@parrt See my text below the example: A human would of course leave out the block. However, a grammar generation tool might introduce a block and it would be cumbersome to remove the blocks by hand after running such tool.

parrt commented 10 years ago

oh right. yep.

sharwell commented 8 years ago

:warning: When this is fixed, we will need to report a warning whenever removing parentheses around a top-level alternative causes it to be treated as a prefix operator. Otherwise you will have a silent change in behavior for some grammars due to the issue described in the following question:

Precedence of alternation vs sequencing in ANTLR4

azrdev commented 6 years ago

Another "use case" which might introduce such parenthesis: When manually inlining a left-recursive rule, I'd wish to clearly separate the inlined part, and the obvious choice would be parentheses, which should not alter semantics.