[antlr-interest] strict parsing

Gavin Lambert antlr at mirality.co.nz
Thu Jun 26 00:50:48 PDT 2008


At 18:23 26/06/2008, Gerard van de Glind wrote:
>I have a grammar that recognizes simple arithmetic expressions.
>So something like 'x + 2' is a valid expression.
>However the expression '[x + 2' is also accepted, in my log file 
>I see the following message:
>
>line 1:0 no viable alternative at character '['
>
>'[' is not valid token in my grammar, so it's ignored.
>I want to make my grammar stricter that it doesn't accept these 
>kind of expressions anymore.

If a '[' is an illegal character in your language, then you could 
start with this:

ILLEGAL: .;

(Make sure that this is the last lexer rule in the grammar file!)

This will recognise any character not otherwise mentioned as a 
token called ILLEGAL, and (since the parser will never expect this 
token) will generate an error in the parser when 
encountered.  This will let you centralise your error-handling 
code.



More information about the antlr-interest mailing list