[antlr-interest] Re: RFC: slight change in ! semantics
Terence Parr
parrt at jguru.com
Sat Oct 6 14:21:28 PDT 2001
On Friday, October 5, 2001, at 05:10 AM, Ric Klaren wrote
...
classDefinition ! [AST modifiers]
> : "class" IDENT
> // it _might_ have a superclass...
> sc:superClassClause
> // it might implement some interfaces...
> ic:implementsClause
> // now parse the body of the class
> cb:classBlock
> {#classDefinition = #(#[CLASS_DEF,"CLASS_DEF"],
> modifiers,IDENT,sc,ic,cb);}
> ;
>
> The problem is the use of IDENT in the action code and the use of the !
> operator. Because the ast generation is turned of no declaration for the
> AST for IDENT is made, and I have no nice way of finding out that IDENT
> is
> used in the action (prescanning the action will be ugly AFAIK). I always
> used the ! as nothing gets generated and so I always label these tokens
> in
> these cases upon reading the docs it turns out that ANTLR indeed
> supports
> the above (another of these little inconsistencies).
I'll add a few thoughts here. I can remember wondering if the unlabeled
reference to a token from an AST action was a good idea. Can't remember
if John Lilley or I came up with the "shortcut" of not requiring a
label, but I definitely remember being nervous about the decision;
naturally, the decisions in the early days of Java-based ANTLR were done
in extreme haste given my time.
I turned off code generation on unlabeled token references since they
couldn't be used in an action...well, that is until Ric reminds of the
dreaded IDENT reference from AST code. FYI, I *rarely* do this. I
always use a reference since it's explicit. Probably why I didn't run
into this. OTOH, didn't I do the AST stuff for the Java grammar? ;)
oops.
> So the options are:
> 1) No optimization and generate 'sloppy' code until a later release.
> 2) Label the IDENT and use the label in cases like this.
> 3) always generate declarations for things like IDENT (not for things
> like
> "class" since you can't reference it in actions (can't?) )
This is a shortcut that will work and a decent compiler will remove the
code for creating the value if it can determine the ctor is side-effect
free. Yeah, right. ;)
Since the AST action is special, it's best to track which tokens are
referenced and update the "generate AST node" boolean for that grammar
item. These actions have to be translated anyway, so a simple check
inside the action recognizer should fix this. Oh wait, actually, forget
that. You'd have to do 2 passes over the rule looking for backward
references. Ick.
Option 3 is good for now and optimization folks can simply rewrite to
not use the rule level ! and use individual ! on the grammar elements.
Ter
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list