[antlr-interest] Reserved words
Johannes Luber
jaluber at gmx.de
Sat Jul 12 02:41:13 PDT 2008
rkevinburton at charter.net schrieb:
> I am trying to build a simple script language that mimics C#. As such there are dome identifiers that are reserved. For example 'int a;' is legal but 'int int' or 'int bool' doesnt make much sense. As such I would like to make a list of some reserved words. My fiirst attempt failed miserably.
>
> RESERVED : 'class' | 'struct' | 'int' | 'bool';
>
> When I check this grammer I get an error that 'class' is unreachable. Further on in the grammar I have:
>
> CLASS : 'class';
>
> class: SCOPE CLASS IDENT '{' body '}';
>
> It seems that even before I get around to defining IDENT to be all the legal characters for an identifier minus the reserved words, I get an error even listing the reserved words. I am obviously looking at this wrong. Any ideas?
>
> Thank you.
>
> Kevin
>
Remove RESERVED and define CLASS, STRUCT, etc. before the ident rule. If
you truly need RESERVED elsewhere, turn it into
reserved : CLASS | STRUCT | etc. ;
Don't use embedded string literals in parser rules as those just give
problems. And in tree grammars they aren't allowed anyway.
Johannes
More information about the antlr-interest
mailing list