[antlr-interest] Heterogeneous AST nodes (was: 2.7.2a4 ANTLR/C# - build ok, panic shutdown)

slightlynew gackle at shaw.ca
Thu Dec 5 21:30:31 PST 2002


> Perhaps you grabbed 11-29's version not the 30th :)

Yeah, or made a dumber error than that!

If I do get it to work I'll let you know.  I might just wait for the 
C# version since my work is in C# these days.

By the way, I have a question about heterogeneous ASTs that relates 
to the "third arg".  From recent posts I gather that the generated 
code used to do something like this (I'm simplifying here):

  ASTNode n = new MyTypeOfNode();

and that this has been changed to something like this:

  ASTNode n = getASTFactory().createInstance("MyTypeOfNode");

Internally, the factory appears to be using reflection to go from the 
string type name to a new object of that type.  (I should qualify 
that I'm talking about the C# code, and assuming that the Java code 
does something similar.)

Isn't creating an object in this way significantly more expensive?  
If you're creating, say, thousands of nodes of many different types, 
wouldn't one expect slower performance?  Given that the type 
information *is* known at compile time, why pay the overhead of run-
time object creation?  

This isn't an argument against using a factory.  The factory could do 
a big switch statement (or the equivalent) on the name, like this:

case "MyTypeOfNode":
  return new MyTypeOfNode();

and then resort to the run-time way at the end:

default:
  return createInstance("MyTypeOfNode");

Although, come to think of it, it's not obvious to me exactly what 
the advantage of the factory approach is in this context?

Any comments?

Ciao,
Daniel


--- In antlr-interest at y..., Terence Parr <parrt at j...> wrote:
> Hi Daniel,
> 
> Just tried this
> 
> /tmp $ java antlr.Tool t.g
> ANTLR Parser Generator   Version 2.7.2a6 (20021130-1)   1989-2002 
> jGuru.com
> /tmp $ cat t.g
> class P extends Parser;
> 
> a : A {#[a,b,c];}
>    ;
> 
> Perhaps you grabbed 11-29's version not the 30th :)
> 
> Ter
> 



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list