antlr/antlr4 — Issue #27: Generating bad sempred method
Author: parrt
Created: 2012-02-28 19:45:08+00:00
State: open
Labels: code-gen, comp:tool

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.