[antlr-interest] Lexer and Java keywords

Marcin Rzeźnicki marcin.rzeznicki at gmail.com
Wed Dec 9 08:57:18 PST 2009


2009/12/9 Sam Harwell <sharwell at pixelminegames.com>
>
> Do you currently have the IDENTIFIER lexer rule located before (as in line number) ABSTRACT, etc.? I'm guessing that's the cause of your current problem.

Hi Sam,
Thanks for the quick response.
Unfortunately no, my IDENTIFIER rule is the last non-fragment rule in
the grammar file.

>
> Also, don't specify a value for k in your lexer.
>

Ok, but it does not matter much in my case.

>
> On a side note, this really isn't the ANTLR way to do things, but your generated code will be smaller and faster if you do this. I might have the syntax slightly wrong since I'm not a Java programmer. If you are using a combined grammar (lexer and parser in the same file), a downside of doing this is you have to always use ABSTRACT in the parser rules, where normally 'abstract' would alias itself to the token.
>
> @lexer
> {
> Hashtable<String, Integer> keywords = new Hashtable()
>    {{
>    put("abstract", ABSTRACT);
>    put("break", BREAK);
>    }};
> }
>
> // the fragment rules assign values to the token types that you can use in the parser.
> fragment ABSTRACT : ;
> fragment BREAK : ;
>
> IDENTIFIER
> @after
> {
> Integer value = keywords.get($text);
> if (value != null)
>    setType(value); // might be state.setType
> }
>    : ...
>    ;


Interesting, that might work. I'll try this soon. But can you tell me
why my original example is wrong? If I understand ANTLR correctly this
should not pose any problems for LL(*)

--
Greetings
Marcin Rzeźnicki


More information about the antlr-interest mailing list