[antlr-interest] Expression parser expecting token at EOF
Micheal J
open.zone at virgin.net
Tue Mar 21 10:26:30 PST 2006
Hi Aaron,
> I apologize if this is the wrong forum for this message, but
> I'm still new here (so punt me the right direction if need
> be). I've written a parser for what I think is a relatively
> simple grammar for expressions. I'm using antlr 2.7.5 with
> C# as the language.
ANTLR 2.7.6 has been out for a while now.
> I noticed that when I provided the input "test" to the
> expression element that it would fail when attempting to do a
> lookahead(1). So I narrowed the problem down to the variant
> element that is listed in the grammer and reduced the variant
> element to only allow one expression for ease of reading.
>
> I wrote a small C# wrapper around this that calls variant()
> on the parser.
>
> The sample works for the inputs "test(20)" and "test[20]",
> but fails for "test". The problem appears to be in the output
> code to "variantAtoms" which performs a switch(LA(1)) and
> then requires a token to be present or it throws
> NoViableAltException and my execution is done.
>
> Ironically, if I enter "test)" it works. variantAtom has a
> case statement for RPAREN and for the life of me I can't
> understand where in the tree it would expect to see RPAREN as
> the next token.
>
> Thanks in advance...
I tried your grammar with ANTLR 2.7.6 (language=C#) and it works as
expected:
"test" => fails because variant is defined as "IDENT variantAtoms"
"test)" => fails for same reason since ")" is not matched as a
"variantAtoms"
"test(20)", "test[20" => recognized.
Cheers,
Micheal
ANTLR/C#
More information about the antlr-interest
mailing list