[antlr-interest] ANTLR Questions

Sam Harwell sharwell at pixelminegames.com
Wed Jan 21 06:58:23 PST 2009


ASTLabelType only changes what nodes are cast to inside the generated code. For example, if you call a function from an action in your grammar, and pass it a node, it will pass a Tree interface (ITree in the CSharp/CSharp2 runtime) by default:

MyFunction( ((Tree)someNode) );

However, if you specify the option 'ASTLabelType=CommonTree;', then it will call the function like this:

MyFunction( ((CommonTree)someNode) );

If you use an ASTLabelType that is not CommonTree or one of its base classes, you are responsible for setting the grammar's TreeAdaptor to one that generates nodes of the proper type. Make sure your TreeAdaptor handles creating error nodes as well, or you'll get invalid cast exceptions during error handling.

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Johannes Luber
Sent: Wednesday, January 21, 2009 8:05 AM
To: Thomas Brandon; bruno.marc-aurele at polymtl.ca
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] ANTLR Questions

> On Wed, Jan 21, 2009 at 11:55 PM, Bruno Marc-Aurele
> <bruno.marc-aurele at polymtl.ca> wrote:
> > Another question pops for me: can I define new attributes for a node in
> semantic
> > actions? For example, if I write the following rule:
> >
> > rule: ID subrule {$subrule.newAttribute = 1;} INT;
> >
> > ... will my node object have a "newAttribute" property (C# target) when
> > generated?
> ANTLR doesn't generate the custom AST types for you, you must manually
> create them.
> 
> Tom.

Isn't the option ASTLabelType used to switch to a different class? Or would one implement a new TreeNodeFactory (class name may be different)?

Johannes
-- 
NUR NOCH BIS 31.01.! GMX FreeDSL - Telefonanschluss + DSL 
für nur 16,37 EURO/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list