-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parentheses without quantifier in parser rule lead to syntax error on non-root rule parsing #1545
Comments
Hi. 1. is suspicious!. 2. that is expected. when you call statement as root rule, EOF can follow possibly but it must look at next token to see if it should keep looping, hence, not expecting '.'. (it's not called from program). 3. it consumes |
Starting with #118 (a known issue), this seems to be a more detailed and slightly broader example of cases which can fail. 📝 I'm able to reproduce all three bugs in my fork as described, suggesting that this bug is related to a pretty fundamental understanding for how we treat both the end of the start rule and the EOF symbol. |
Where is the emoji for "holy crap!"? |
😮 ? ⛪️ 💩 ? |
More information: For the scenario where the input is By using The second bug appears to be caused by a failure in the template for LL1OptionalBlock(choice, alts, error) ::= <<
setState(<choice.stateNumber>);
_errHandler.sync(this);
switch (_input.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<alt>
break;}; separator="\n">
default:
- <error>
+ break;
}
>> |
This change updates the default sync() strategy to match the strategy used for selecting an alternative when prediction leaves the decision rule prior to reaching a syntax error. Closes antlr#1545
This change updates the default sync() strategy to match the strategy used for selecting an alternative when prediction leaves the decision rule prior to reaching a syntax error. Closes antlr#1545
@rslemos Thanks for taking the time to post such an excellent set of repro steps. Your examples allowed us to fix two different bugs in the prediction algorithm implementation. 🥇 |
Consider the following grammar:
When parsing the text
OPEN DEVICE DEVICE
(directly throughstatement()
, and not throughprogram()
) everything works fine.Trace output:
Tree output:
Exercise 1
Now consider substituting any of the alternative
statement
definitions (where one or moreOPTx
tokens are parenthized).Tree output is still the same. But now some
syntaxError()
get reported on theANTLRErrorListener
. Here the trace along withConsoleErrorListener
output:Maybe ANTLR4 is expecting either: any
OPTx
(an optional argument to the previousDEVICE
), anotherDEVICE
,OPEN
(for a newstatement
) or.
(period, to close theprogram
). I understand that<EOF>
is not to be expected if we were to parse the text from the root ruleprogram
(here I just assume that ANTLR4 breaks its promise to be able to parse from any rule, giving no rule the special quality of being the root rule or the main rule).Exercise 2 (on top of exercise 1)
Going one step further, lets then parse the text
OPEN DEVICE DEVICE.
(notice the period). Again our parsing starts atstatement
rule.What trace do we get?
Weirdly it is expecting
<EOF>
(and notOPEN
nor.
because they are outsidestatement
rule).The output tree wrongly incorporates the
.
:(well, at least I expected it to stop at the second
DEVICE
, leaving the.
unparsed)I think there are 3 bugs here:
OPTx
should have no effect in the generated parser;statement
should not see the<EOF>
as unexpected (afterDEVICE
or anyOPTx
token);statement
should not consume the.
token (not sure about this one; this may only apply to lexers, not to parsers).What do you think?
The text was updated successfully, but these errors were encountered: