[antlr-interest] A lost label?
Markus Stoeger
spamhole at gmx.at
Sat Jan 24 17:45:52 PST 2009
I ran into a nasty "ArgumentNullException" problem with the following
rule today:
label returns [string name]
: x=unsignedInt {$name = $x.text;}
| x=identifier {$name = $x.text;}
;
The generated code (CSharp2) for this rule is below. For some reason
ANTLR doesn't assign the label x in the first alternative which results
in "name" being null all the time:
case 1 : {
unsignedInt(); // missing assignment to label x!
name = ((x != null) ?
input.ToString((IToken)(x.Start),(IToken)(x.Stop)) : null);
break;
}
case 2 : {
x = identifier();
name = ((x != null) ?
input.ToString((IToken)(x.Start),(IToken)(x.Stop)) : null);
break;
}
If I call one label x and the other one y it works fine (both get assigned).
Shouldn't it be possible to use the x label for both assignments, since
they are assigned in two different alternatives? The book (page 124)
says "variables are visible to the immediately surrounding alternative,
but not other alternatives". If I understand this right, each
alternative has its own scope so reusing the variable name in another
scope should be possible? Or maybe ANTLR could display an error while
generating the parser in such a case? That would be better than silently
discarding the assignment... :-)
Max
More information about the antlr-interest
mailing list