[antlr-interest] ANTLR parsing too lazy

shmuel siegel antlr at shmuelhome.mine.nu
Mon Dec 17 03:05:40 PST 2007


Marco Bakera wrote:
> Hey everybody,
>
> It seem that the parser that I use for parsing mathematical expressions 
> accepts inputs like 
>
> '(a & b))' 
>
> although there is no rule for the second closing parenthesis. This expression 
> will be parsed into a syntax tree like the following
>
> &
> +- a
> \- b
>
> Whatever follows '( a & b)' will be ignored since no rules matches. 
>
> How can I manage to let antlr throw an exception when the input reaches a 
> place where no rule matches?
>
> Thanks for any kind of help.
>
> Greetings,
> Marco
You haven't included your grammar so I am just guessing. The usual cause 
of this problem is that you wrote your grammar to recognize what it can 
and then stop. You didn't insist that the grammar recognize the entire 
input. For instance, your case could have been written as

prog: sentence+;
sentence: '(' IDENT '&' IDENT ')';

It hits the second right paren and stops; it found a sentence; hence it 
found a prog.

Instead write
prog: sentence+ EOF;

Now, everything until the EOF must be recognized. I hope that it is 
obvious how to apply this to your case.

Shmuel


> .
>   




More information about the antlr-interest mailing list