[antlr-interest] A proposal for keywords

Terence Parr parrt at cs.usfca.edu
Wed May 24 11:21:09 PDT 2006


On May 23, 2006, at 11:09 AM, Loring Craymer wrote:

> I was concerned that this might not work with the LL(*) DFAs of  
> ANTLR 3, until I realized that the predicate hoisting mechanism  
> provides almost all of the support needed.  (Some sort of type  
> patching table may also be required; “if” might be matched by a  
> state that allowed either LITERAL_if or TEXT as the type for that  
> token; for a first implementation, the type patching may not be  
> necessary since tree walkers could also do dynamic lookup when  
> matching literals.  Patching, though, seems preferable over the  
> longer term.)

Hi...seems like we can just let preds handle it.  There is a cool  
example in the examples-v3 dir about turning enum on / off, but this  
concept needs a different example.  I think this will work:

stat : if ... | ID '=' expr | ... ;

if : {input.LT(1).getText().equals("if")}? ID ;

but, that's expensive doing the compare all the time.  Another way is  
to simply list the keywords:

id : ID | 'if' | 'then' | 'begin' | ... ;

then use like

stat : 'if' ... | id '=' expr | ... ;

What that do what you want?

Ter


More information about the antlr-interest mailing list