[antlr-interest] Custom AST node type - guidance needed

Bill Andersen andersen at highfleet.com
Mon Jul 19 13:02:57 PDT 2010


Thanks Jay

That did it. 

On Jul 19, 2010, at 15:52 , Junkman wrote:

> Hi Bill,
> 
> You have
> 
> import antlr.*;
> 
> instead of
> 
> import org.antlr.runtime.*;
> 
> Maybe that's the problem?
> 
> org.antlr.runtime.CommonToken implements org.antlr.runtime.Token, so
> explicit casting seems out of wack.
> 
> Jay
> 
> 
> Bill Andersen wrote:
>> Thanks John
>> 
>> Below is my class def..  Eclipse tells me the casts to Token below are required.  Running a grammar that executes these constructors tells me I can't cast to Token.
>> 
>> Here's a sample tree building expression where this class is used.
>> 
>> 	^(AS_TYPE_LIST<ASNode>[s.getType($ID.text)] ID)
>> 
>> the getType method just looks up a ASType object by name from a table.
>> 
>> import org.antlr.runtime.tree.*;
>> 
>> import antlr.*;
>> 
>> public class ASNode extends CommonTree {
>> 	
>> 	public ASType type = null;
>> 	
>> 	public ASNode() { }
>> 	
>> 	public ASNode(int ttype, ASType type) {
>> 		token = (Token) new CommonToken(ttype,"");
>> 		this.type = type;
>> 	}
>> 	
>> 	public ASNode(int ttype) {
>> 		token = (Token) new CommonToken(ttype,"");
>> 	}
>> 	
>> 	@Override
>> 	public String toString() {
>> 		return (token!=null?token.getText():"")+"<ASNode>;"+type;
>> 	}
>> 
>> 	@Override
>> 	public Tree dupNode() {
>> 		ASNode n = new ASNode();
>> 		n.token = token;
>> 		n.type = type;
>> 		return n;
>> 	}
>> 	
>> }
>> 
>> On Jul 19, 2010, at 15:15 , John B. Brodie wrote:
>> 
>>> On Mon, 2010-07-19 at 14:29 -0400, Bill Andersen wrote:
>>>> Hi folks
>>> Greetings!
>>> 
>>>> Having some trouble making ASTs using a custom node type.  Before I ask any stupid questions, what is the best place to look on how to do this ( I'm using 3.2 )?  I'm finding bits and pieces, some of them contradictory.  For example I find this example
>>>> 
>>>> static class V extends CommonTree {
>>>> public int x,y,z;
>>>> public V(int ttype, int x, int y, int z) {
>>>>   this.x=x; this.y=y; this.z=z; token=new CommonToken(ttype,"");
>>>> }
>>>> at http://www.antlr.org/wiki/display/ANTLR3/Tree+construction
>>>> 
>>>> When I try to define a constructor like this I'm forced to put a cast between on CommonToken to Token, which causes a runtime can't cast error.
>>>> 
>>>> What I want is really quite simple.  Need to add one extra field to my AST nodes to carry DSL type information.
>>>> 
>>>> Any help appreciated.  Thanks.
>>> Unable to reproduce.
>>> 
>>> Attached please find a complete example derived from the grammar on the
>>> above wiki page that uses the class V. maybe I'm using a different
>>> example than yours?
>>> 
>>> Need more info....
>>>  -jbb
>>> 
>>> <T.g>
>> 
>> Bill Andersen 
>> Highfleet, Inc. (www.highfleet.com)
>> 3600 O'Donnell Street, Suite 600
>> Baltimore, MD 21224
>> Office: +1.410.675.1201
>> Cell: +1.443.858.6444
>> Fax: +1.410.675.1204
>> 
>> 
>> 
>> 
>> 
>> 
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>> 
> 

Bill Andersen 
Highfleet, Inc. (www.highfleet.com)
3600 O'Donnell Street, Suite 600
Baltimore, MD 21224
Office: +1.410.675.1201
Cell: +1.443.858.6444
Fax: +1.410.675.1204







More information about the antlr-interest mailing list