[antlr-interest] Clarification on Attribute Reference in Actions

Kay Roepke kroepke at classdump.org
Wed Jul 4 16:22:07 PDT 2007


On Jul 5, 2007, at 12:57 AM, Randall R Schulz wrote:

> Is there more to it than what I showed? It seems pretty self-contained
> to me.

Yeah, sorry. :)

> The only rule referred to there but not included in my excerpt has  
> this
> intro:
>
> termSequence
> returns [ List<Term> ts ]
>
>
> Everything else is either an application type (Term, Function) or a
> token (ComplexType).

Ok, given this cooked up grammar:

------------------------------------
tree grammar Attr;

options {
	ASTLabelType=CommonTree;
}
@treeparser::members {

class Term {
	public Term() { }
}

public Object asObject(Term t) {
	System.err.println("Received term");
}
}

term returns [Term t]
	:	^(ComplexTerm term termSequence)
	{
		Object o = asObject($term.t);
	}
	;

termSequence
returns [ List<Term> ts ]
@init {
	ts = new List<Term>();
}
	:  (t=term {ts.add($t.text);})
	;
	
start
	:	term
	;

------------------------------------

Everything looks well from my perspective.
The code fragment in question reads :
------------------------------------

     // $ANTLR start term
     // Attr.g:17:1: term returns [Term t] : ^( ComplexTerm term  
termSequence ) ;
     public final term_return term() throws RecognitionException {
         term_return retval = new term_return();
         retval.start = input.LT(1);

         try {
             // Attr.g:18:4: ( ^( ComplexTerm term termSequence ) )
             // Attr.g:18:4: ^( ComplexTerm term termSequence )
             {
             match(input,ComplexTerm,FOLLOW_ComplexTerm_in_term37);

             match(input, Token.DOWN, null);
             pushFollow(FOLLOW_term_in_term39);
             term();
             _fsp--;

             pushFollow(FOLLOW_termSequence_in_term41);
             termSequence();
             _fsp--;


             match(input, Token.UP, null);

             		Object o = asObject(retval.t);
             	

             }

         }
         catch (RecognitionException re) {
             reportError(re);
             recover(input,re);
         }
         finally {
         }
         return retval;
     }
     // $ANTLR end term
------------------------------------

Notice how it references retval.t in my version. I think this is a  
bug in the actionparser
code. It should warn you that $term.t will refer to the rule's return  
value and not the subtree
reference's retval object.

I wonder why it just put a t in your output?
For that reason it'd be helpful to see the actual grammar that caused  
this...

cheers,
-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list