Re: tricky lexical grammar ambiguity thoughts
- Michael Schmitt wrote:
>Ok...here's the problem...I assume *any* of your characters can follow a
> Hi Terence,
> it took me quite some time to find a suitable example but here it comes
> finally: If you compile the attached lexer specification you do not get
> any comment on an existing ambiguity. However, if you start the lexer
> with input
> you get a "Mismatched char" exception because ANTLR chooses the wrong
> (first) alternative. There may be different ways of interpreting the end
> of a token but at least ANTLR should warn the user about problems.
> Any comments are welcome...
rule since your main rule is called repeatedly by nextToken. A parser
would correctly detect a problem here I believe.
Here is a simpler grammar to think about (k=2):
MAIN : A
A : 'y' 'b'
| 'a' 'x'
B : 'a'
'a' 'b' cannot be matched by A, but when lumped together as lookahead
for A in rule MAIN, 'a' 'b' predicts A due to the approx lookahead.
Normally you'd get an error, but the 'b' from B is only contributed by
repeated calls to MAIN, therefore, I cannot accurately say there is an
ambiguity. You have nothing in the grammar that indicates B B is a
valid sequence. This is a very very tricky question and I'm making a
FAQ entry right now!
hope that helps,
Chief Scientist, http://www.jGuru.com -- Your Java Portal
Co-Founder, http://www.NoWebPatents.org -- Stop Patent Stupidity!