[antlr-interest] How can I ignore reserved words in certain cases ?

Gavin Lambert antlr at mirality.co.nz
Thu Nov 30 23:28:09 PST 2006


At 09:11 30/11/2006, Micheal J wrote:
 >I am writing a command line editor that has reserved words, such 

 >as "print", that should accept, at times, any string value. For
 >example the following should be allowed:
 >
 >print hello
 >print print
[...]
 >Obviously the second example ("print print") does not work
 >because the token PRINT takes priority since we have 
testLiterals = true
 >(note that this example is taken from a more complex grammar).
 >
 >The question is: how do I, under certain circumstances, ignore
 >reserved word tokens?

I haven't tried this, but I think you ought to be able to use a 
catchall here.  Something like this:

expr1: PRINT what:. { print($what.getText()); };

(Or more likely a ~-set, since you probably don't want to allow 
symbols instead of words.)

I could be wrong, though :)



More information about the antlr-interest mailing list