[antlr-interest] Reserved words

Raphael Reitzig r_reitzi at cs.uni-kl.de
Sat Jul 12 03:27:07 PDT 2008


Hi Kevin!

Things like keywords are kind of static semantics. I do not feel you can
solve those matters elegantly whith syntactical means.
In my opinion, you should parse _all_ (syntactically correct) identifiers
as such and check later (that means, in the action according to this rule)
if any keyword is used. You can easily throw an exception then. In
particular, you can provide a much better error message than "No viable alt
found" or "error matching ..." - for what you really want to say is "You
may not use keyword XY as identifier!".

Regards

Raphael

On Sat, 12 Jul 2008 11:41:13 +0200, Johannes Luber <jaluber at gmx.de> wrote:
> 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