[antlr-interest] Yet another bug in ANTLRv4

Terence Parr parrt at cs.usfca.edu
Tue Feb 28 11:45:24 PST 2012


Interesting. I will add this to the issue list.

https://github.com/antlr/antlr4/issues/27

Ter

> Jan Finis <mailto:finis at in.tum.de>
> February 28, 2012 8:29 AM
> Hi Ter,
>
> I tried to change the "expr with concatenation" grammar to work without
> using a doubly left-recursive concat operation ( expr : expr expr;)
> in case that Honey Badger will never allow such rules again.
>
> The resulting grammar is accepted by latest Honey Badger (the one pulled
> from parrt/antlr). However, the resulting parser contains errors: an
> unused semPred method with errors is produced. Since it is unused, I
> simply deleted it. However, the parser still does not match the input
> correctly. I guess both things have are symptoms of the same bug.
>
> Here is (a simplified version of) the grammar producing the erroneous
> parser:
>
> rule returns [String r]
> : t=ID ':' alts+=expr ('|' alts+= expr)* ';'
> { $r = "x"; }
> ;
>
> strongExpr returns [String r]
> : ID { $r = "x"; }
> | '(' ep=parenExpr ')' { $r = "x"; }
> | e1=strongExpr '?' { $r = "x"; }
> ;
>
> expr returns [String r]
> : e=strongExpr (e2=expr)? { $r = "x"; }
> | e1=expr '->' ID {$r = "x"; }
> ;
>
> parenExpr returns [String r]
> : e1=expr '|' e2=parenExpr { $r = "x"; }
> | expr {$r = $expr.r; }
> ;
>
> ID : ('A'..'Z'|'a'..'z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
> ;
>
> the resulting unused sempred with errors:
>
> public boolean sempred(RuleContext _localctx, int ruleIndex, int
> predIndex) {
> switch ( ruleIndex ) {
> case 1 : return strongExpr_sempred((StrongExprContext)_localctx,
> predIndex);
> case 2 : return expr_sempred((ExprContext)_localctx, predIndex);
> }
> return true;
> }
>
> the problems are the casts (StrongExprContext)_localctx and
> (ExprContext)_localctx, since _localctx is a RuleContext here.
> As mentioned, the sempred is not called anyway.
>
> Best regards,
> Jan
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> Terence Parr <mailto:parrt at cs.usfca.edu>
> February 27, 2012 11:31 AM
> Jan Finis wrote:
>> Hi,
>>
>> I encountered two more bugs in ANTLRv4, tested with the parrt/antlr4
>> fork pulled yesterday from github:
>>
>> First Bug:
>>
>> The following rule does not work:
>> expr
>>      : 'x'
>>      | e1=expr '?'
>>      | e1=expr e2=expr
>>      ;
>>
>> Error: The following sets of rules are mutually left-recursive [expr]
>
> that does not fit the expression pattern and so is definitely left
> recursive by my current definition. Hmm... I'm not sure I am able to
> translate this one properly given the double recursion to itself.
>
>> It seems to be the concatenation operation of two expressions (last
>> alternative). If I leave it out, it works fine.
>>
>> I am sure that I did not have this bug with my old 4.0ea ANTLR build. So
>> it must be introduced lately.
>>
>> Second Bug:
>>
>> If you take the rule from above and leave out the second alternative
>> like that:
>>
>> expr
>>      : 'x'
>>      | e1=expr e2=expr
>>      ;
>
> zoiks. adding to the bug list. your expression rule doesn't make sense,
>    by the way. You probably want something else.
> Ter
>> then the following exception is thrown:
>>
>>            [java] can't find rule grammarSpec or tree structure error:
>> (COMBINED_GRAMMAR ANTLRv4 (RULES (RULE expr (BLOCK (ALT 'x') (ALT (= e1
>> expr[1]) (= e2 expr[1]))))))
>>            [java] java.lang.reflect.InvocationTargetException
>>            [java]     at
>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>            [java]     at
>> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>            [java]     at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>            [java]     at java.lang.reflect.Method.invoke(Unknown Source)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.visit(GrammarTreeVisitor.java:192)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.visitGrammar(GrammarTreeVisitor.java:186)
>>            [java]     at
>> org.antlr.v4.semantics.SymbolCollector.process(SymbolCollector.java:69)
>>            [java]     at
>> org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:95)
>>            [java]     at
>> org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:301)
>>            [java]     at org.antlr.v4.Tool.process(Tool.java:290)
>>            [java]     at
>> org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:254)
>>            [java]     at org.antlr.v4.Tool.main(Tool.java:144)
>>            [java] Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
>>            [java]     at
>> org.antlr.v4.semantics.SymbolCollector.discoverOuterAlt(SymbolCollector.java:112)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.outerAlternative(GrammarTreeVisitor.java:2197)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.ruleBlock(GrammarTreeVisitor.java:2105)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.rule(GrammarTreeVisitor.java:1576)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.rules(GrammarTreeVisitor.java:1096)
>>            [java]     at
>> org.antlr.v4.parse.GrammarTreeVisitor.grammarSpec(GrammarTreeVisitor.java:307)
>>            [java]     ... 12 more
>>            [java] error(9):  internal error: Rule expr[1] undefined
>>            [java] error(9):  internal error: Rule expr[1] undefined
>>            [java] Exception in thread "main" java.lang.NullPointerException
>>            [java]     at
>> org.antlr.v4.automata.ParserATNFactory.elemList(ParserATNFactory.java:386)
>>            [java]     at
>> org.antlr.v4.automata.ParserATNFactory.alt(ParserATNFactory.java:375)
>>            [java]     at
>> org.antlr.v4.parse.ATNBuilder.alternative(ATNBuilder.java:615)
>>            [java]     at
>> org.antlr.v4.parse.ATNBuilder.ruleBlock(ATNBuilder.java:294)
>>            [java]     at
>> org.antlr.v4.automata.ParserATNFactory._createATN(ParserATNFactory.java:127)
>>            [java]     at
>> org.antlr.v4.automata.ParserATNFactory.createATN(ParserATNFactory.java:109)
>>            [java]     at
>> org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:309)
>>            [java]     at org.antlr.v4.Tool.process(Tool.java:290)
>>            [java]     at
>> org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:254)
>>            [java]     at org.antlr.v4.Tool.main(Tool.java:144)
>>
>> Regards,
>> Jan
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> Jan Finis <mailto:finis at in.tum.de>
> February 23, 2012 12:46 AM
> Hi,
>
> I encountered two more bugs in ANTLRv4, tested with the parrt/antlr4
> fork pulled yesterday from github:
>
> First Bug:
>
> The following rule does not work:
> expr
> : 'x'
> | e1=expr '?'
> | e1=expr e2=expr
> ;
>
> Error: The following sets of rules are mutually left-recursive [expr]
>
> It seems to be the concatenation operation of two expressions (last
> alternative). If I leave it out, it works fine.
>
> I am sure that I did not have this bug with my old 4.0ea ANTLR build. So
> it must be introduced lately.
>
> Second Bug:
>
> If you take the rule from above and leave out the second alternative
> like that:
>
> expr
> : 'x'
> | e1=expr e2=expr
> ;
>
> then the following exception is thrown:
>
> [java] can't find rule grammarSpec or tree structure error:
> (COMBINED_GRAMMAR ANTLRv4 (RULES (RULE expr (BLOCK (ALT 'x') (ALT (= e1
> expr[1]) (= e2 expr[1]))))))
> [java] java.lang.reflect.InvocationTargetException
> [java] at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> [java] at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> [java] at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> [java] at java.lang.reflect.Method.invoke(Unknown Source)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.visit(GrammarTreeVisitor.java:192)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.visitGrammar(GrammarTreeVisitor.java:186)
> [java] at
> org.antlr.v4.semantics.SymbolCollector.process(SymbolCollector.java:69)
> [java] at
> org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:95)
> [java] at
> org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:301)
> [java] at org.antlr.v4.Tool.process(Tool.java:290)
> [java] at
> org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:254)
> [java] at org.antlr.v4.Tool.main(Tool.java:144)
> [java] Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
> [java] at
> org.antlr.v4.semantics.SymbolCollector.discoverOuterAlt(SymbolCollector.java:112)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.outerAlternative(GrammarTreeVisitor.java:2197)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.ruleBlock(GrammarTreeVisitor.java:2105)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.rule(GrammarTreeVisitor.java:1576)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.rules(GrammarTreeVisitor.java:1096)
> [java] at
> org.antlr.v4.parse.GrammarTreeVisitor.grammarSpec(GrammarTreeVisitor.java:307)
> [java] ... 12 more
> [java] error(9): internal error: Rule expr[1] undefined
> [java] error(9): internal error: Rule expr[1] undefined
> [java] Exception in thread "main" java.lang.NullPointerException
> [java] at
> org.antlr.v4.automata.ParserATNFactory.elemList(ParserATNFactory.java:386)
> [java] at
> org.antlr.v4.automata.ParserATNFactory.alt(ParserATNFactory.java:375)
> [java] at
> org.antlr.v4.parse.ATNBuilder.alternative(ATNBuilder.java:615)
> [java] at
> org.antlr.v4.parse.ATNBuilder.ruleBlock(ATNBuilder.java:294)
> [java] at
> org.antlr.v4.automata.ParserATNFactory._createATN(ParserATNFactory.java:127)
> [java] at
> org.antlr.v4.automata.ParserATNFactory.createATN(ParserATNFactory.java:109)
> [java] at
> org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:309)
> [java] at org.antlr.v4.Tool.process(Tool.java:290)
> [java] at
> org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:254)
> [java] at org.antlr.v4.Tool.main(Tool.java:144)
>
> Regards,
> Jan
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list