[antlr-interest] How to match variables with reserved words in it?

Natan nvivo.misc at gmail.com
Tue Dec 22 08:20:43 PST 2009


Hi,

I have a grammar like this :

tokens {
     ERROR = 'error';
     IF = 'if';
     ELSE = 'else';
}

...

variableAssignment
     :  variable '=' expression
     ;

variable
     :  '$' ID
     ;

errorStatement
     :    ERROR STRING
     ;

...

ID
     :    ('a' .. 'z' | 'A' .. 'Z' | '_') ( 'a' .. 'z' | 'A' .. 'Z' | 
'0' .. '9' | '_' )*
     ;


As you can see, variables are like in Perl with $ in front of them. 
Everything works fine until I try to call a variable "$error" or "$if".

If I try to use a variable like $error anywhere in the code, I get a 
MismatchedTokenException with a message "mismatched input 'error' 
expecting ID". It calls the variable() method in the parser, matches the 
$ and then gives an error in this line trying to match the ID:

Match(input,ID,FOLLOW_ID_in_variable1557)

This seems to happen for any word that is already defined in any other rule.

Why is the parser unable to follow the ID after the $, and how can I 
make this work to allow any ID after the $?

I'm using C# Antlr 3.1.3.

Thanks.


More information about the antlr-interest mailing list