[antlr-interest] C# target doesn't do numbers

Wincent Colaiuta win at wincent.com
Wed Jun 27 03:53:58 PDT 2007


El 27/6/2007, a las 12:34, Vaclav Barta escribió:

> Wincent Colaiuta wrote:
> > El 27/6/2007, a las 11:19, Vaclav Barta escribió:
> >> Digit : '0'..'9' ;
> >>
> >> Integer : ( Digit )+ ;
> ...
> >> line 1:0 no viable alternative at input '1'
> > Have you tried making Digit a fragment rule? seeing as it is not
> > referenced by any parser rule.
> Err, what is a fragment rule and how do I make it? My ANTLR  
> knowledge is rather dismal... What I tried was removing Digit  
> altogether (it was used in another place in the full grammar, but  
> that could be worked around), and
>
> Integer : ( '0'..'9' )+ ;
>
> does work - but still, it would be nice to understand why... :-)

Others can probably explain it better than me, but the basic reason  
is the following:

Rules Digit and Integer are ambiguous because given input like "1"  
ANTLR doesn't know whether to match using rule Digit or Integer;  
either would work. And ANTLR wants to make deterministic decisions;  
that is, for any given input symbol it wants to know *exactly* which  
(unique) path to take. The rules may not seem ambiguous to your human  
eye because you read them and understand them to mean "if you see 0  
to 9 it's a Digit, unless you see a series of them in which case it's  
an Integer"; ANTLR can make no such assumptions and so you have to  
help it by adding the "fragment" keyword so that it knows that the  
Digit rule is only there for use by other rules.

Checking your grammar in ANTLRWorks and Looking at the syntax diagram  
can help you to see these ambiguous paths.

Cheers,
Wincent



More information about the antlr-interest mailing list