[antlr-interest] Need help with simple grammar

Gavin Lambert antlr at mirality.co.nz
Mon Apr 23 01:05:34 PDT 2007


At 08:57 23/04/2007, Johannes Luber wrote:
 >FILE : ID;
 >
 >3. Exchange all token rules with normal grammar rules like:
 >
 >GET : {input.LT(1).getText().equals("get")}? ID;

Another approach, which avoids the predicate (and so is slightly 
more "pure" grammar) is to do something like this:

tokens {
   GET = 'get';
   PUT = 'put';
}
...
keyword: GET | PUT;
target: ID | keyword;

This is a bit more work, since every time you add a keyword you 
not only have to add it as a token but you also have to add it to 
the keyword rule.  You also need to get the order of lexer token 
definitions correct, because despite what someone posted last week 
it does appear to make a difference (at least it did when I tried 
it).

Plus, while this will match "get" as a filename, the token type 
will still be GET, not ID.  If that's important to you, I think 
you can override that with rewrite rules, but I haven't played 
with those long enough to give you a concrete example.



More information about the antlr-interest mailing list