antlr / antlr/antlr4

Exception from Tool while parsing an ANTLR grammar

Open
#559 0 comments 0 reactions 0 assignees View on GitHub
comp:tool grammars type:bug
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

this particular case causes antlr4 to pop out "can't find rule grammarSpec or tree structure error" without the mutual recursion error that identifies the rules in violation. Commenting out the recursion in rule2 allows for the mutual recursion error to surface.

```
grammar T;

rule1:
rule2
| rule3
;

rule2:
rule1
| rule2
;

rule3:
rule1
| rule3
;
```

gets exception

```
can't find rule grammarSpec or tree structure error: (COMBINED_GRAMMAR T (RULES (RULE rule1 (BLOCK (ALT rule2) (ALT rule3))) (RULE rule2 (BLOCK (ALT rule1) (ALT rule2))) (RULE rule3 (BLOCK (ALT rule1) (ALT rule3)))))
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
at org.antlr.v4.semantics.SymbolCollector.discoverOuterAlt(SymbolCollector.java:111)
at org.antlr.v4.parse.GrammarTreeVisitor.outerAlternative(GrammarTreeVisitor.java:2285)
at org.antlr.v4.parse.GrammarTreeVisitor.ruleBlock(GrammarTreeVisitor.java:2198)
at org.antlr.v4.parse.GrammarTreeVisitor.rule(GrammarTreeVisitor.java:1664)
at org.antlr.v4.parse.GrammarTreeVisitor.rules(GrammarTreeVisitor.java:1242)
at org.antlr.v4.parse.GrammarTreeVisitor.grammarSpec(GrammarTreeVisitor.java:481)
... 12 more
Exception in thread "main" java.lang.NullPointerException
at org.antlr.v4.tool.Rule.getAltLabels(Rule.java:217)
at org.antlr.v4.tool.LeftRecursiveRule.getAltLabels(LeftRecursiveRule.java:90)
at org.antlr.v4.tool.Rule.hasAltSpecificContexts(Rule.java:200)
at org.antlr.v4.tool.LeftRecursiveRule.hasAltSpecificContexts(LeftRecursiveRule.java:61)
at org.antlr.v4.semantics.SymbolChecks.checkForLabelConflicts(SymbolChecks.java:157)
at org.antlr.v4.semantics.SymbolChecks.process(SymbolChecks.java:105)
at org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:107)
at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:397)
at org.antlr.v4.Tool.process(Tool.java:384)
at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:343)
at org.antlr.v4.Tool.main(Tool.java:190)
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with the supplied grammar, then inspect org.antlr.v4.semantics.SymbolCollector.discoverOuterAlt and the GrammarTreeVisitor rule-processing paths named in the stack trace. Confirm that mutual recursion is reported instead of the ArrayIndexOutOfBoundsException, and that the later NullPointerException does not occur.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.