[antlr-interest] A very simple grammar problem.

Gavin Lambert antlr at mirality.co.nz
Thu Mar 6 10:51:58 PST 2008


At 06:45 7/03/2008, Kenneth Domino wrote:
>warning(138): test_not_ok.g:0:0: grammar test_not_ok: no start 
>rule (no rule can
>  obviously be followed by EOF)

It's a good idea to have an explicit start rule that ends in 
EOF.  (Doesn't have to actually be called 'start', though.)  If 
you don't do this, then the parser may simply give up early 
instead of reporting an error towards the end of the input, since 
by leaving out the EOF you've basically told it that it doesn't 
have to consume all the input.

>warning(201): test_not_ok.g:3:3: The following alternatives are 
>unreachable: 2
[...]
>b : 'A' b
>   | 'A'
>   ;

It's not a good idea to have alts with common left prefixes; this 
can sometimes get the lookahead into trouble (which is probably 
why you got the warning above).  Instead try:

b : 'A' b?;

However this is a recursive rule, and wherever possible you should 
replace recursion with iteration (since recursion eats up stack 
space, but iteration doesn't):

b : 'A'+;

And so as a complete grammar this ought to work:

grammar test;

b : 'A'+ EOF;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080307/7377cf1b/attachment-0001.html 


More information about the antlr-interest mailing list