[antlr-interest] Languages where keywords can be used as identifiers

Terence Parr parrt at cs.usfca.edu
Tue Feb 7 14:25:44 PST 2006


On Feb 7, 2006, at 2:16 PM, Adam Bishop (DSLWN) wrote:

> I am parsing a language where “loop” is a keyword, however a label  
> can be named loop.  The rule for label expects an identifier token,  
> but the lexer will return a loop token.  Is there any way to switch  
> testLiterals for a particular rule?
>
>
>
> Ideally the Lexer shouldn’t be doing testLiterals for any usage of  
> the token ID in the parser.
>
>
>
> NOTE:  To make things worse, I am having this problem wherever I  
> have a rule in the parser that expects an identifier
>
> e.g.
>
>
>
> “goto” ID
>
>
>
> Will fail for input “goto loop”
I would not use "loop" as a keyword ever.  Then when you have a  
statement that uses loop as a keyword, use a semantic predicate:

stat : {LT(1).getText().equals("loop")}? ID ...
        | "if" ...
       ...
        ;

Ter


More information about the antlr-interest mailing list