[antlr-interest] enums in v4 ANTLR Java code generation considered useless

Scott Stanchfield scott at javadude.com
Wed May 19 11:39:13 PDT 2010


You can still define the match in the superclass -- just use an
interface like Edgar mentioned and I demonstrated in the
"clarification" note I sent.

I think the big value here would be that it forces every place that
uses the token types to use the enum names (as there are no integer
values). I think that would help debugging enormously (rather than
seeing '4' as the value in the variables window, you'd see 'IDENT').
-- Scott

----------------------------------------
Scott Stanchfield
http://javadude.com



On Wed, May 19, 2010 at 2:34 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>
> On May 18, 2010, at 2:58 PM, Scott Stanchfield wrote:
>
>> There are several advantages to enums:
>> * there is a discrete set of values that can be used (no accidental
>> 42's passed in when 42 isn't a token type)
>> * the enum value can carry extra information
>> * the enum values can override methods differently
>
> These are all excellent advantages. I believe that these mostly apply when you're writing code, not generating. Just like the compiler generates integers underneath, if antlr is generating integers, it's probably okay.
>
>> OH - one of the things that's clouding this is that you really don't
>> need the numeric type identifers anymore. You can just have
>>
>>  public enum TokenType {
>>    IDENT, INT ...;
>>  }
>>
>> then in your match method:
>>
>>  void match(TokenType type) {
>>    if (LA(1).getType() == type) {
>>        ...
>>    }
>>  }
>
> The only problem is that match() lives up in the superclass in the library but the generated parser needs to define the enum.
>
> I also have the problem that I need to merge token types from multiple grammars for grammar imports. This gets more competition with enum types without inheritance.
>
>>
>> And you can use the types in a switch statement:
>>
>>  switch(type) {
>>    case INT:
>>    case IDENT:
>>    ...
>>  }
>>
>> No more magic numbers! Woohoo!
>
> ANTLR already uses the labels when possible such as INT. If you use a literal in your grammar such as ';' in don't label it in the lexer, than I had no choice but to generate the integer token type or a weird label like TOKEN34.
>
> Ter
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list