[antlr-interest] booleans in interpreters

José María García Rodríguez darthia at gmail.com
Mon Sep 26 14:13:31 PDT 2005


It seems right to me using your second approach, because, actually,
"true" and "false" are keywords (literals, I mean), so if it works, go
on :)

Regards,
--
José María García Rodríguez

2005/9/23, Michael Laszlo <mjl at nova.edu>:
> In my lexer code, my ID pattern picks out keywords and literals.
> For example:
>
> ID:     LETTER (LETTER|DIGIT)* {
>         ...
>         // primitive literals
>         else if (getText().equals("true")) { $setType(BOOLEAN); }
>         else if (getText().equals("false")) { $setType(BOOLEAN); }
>         }
>
> My parser includes this code:
>
> tokens {
>         BOOLEAN<AST=BooleanNode>;
>         ...
> }
>
> My intention is to build a true and a false instance of the
> BooleanNode class. The BooleanNode constructor uses getText()
> to distinguish the two cases.
>
> However, antlr.Tools complains about nondeterminisms between
> rules BOOLEAN and ID.
>
> I've noticed that if I include an assignment clause in the
> tokens declarations, things work fine. Something like:
>
> tokens {
>         BOOLEAN_TRUE="true"<AST=BooleanNode>;
>         BOOLEAN_FALSE="false"<AST=BooleanNode>;
>         ...
> }
>
> However, it shouldn't be necessary to define distinct token types
> for true and false.
>
> Any suggestions?
>
> Thanks, Michael


More information about the antlr-interest mailing list