[antlr-interest] trees with payloads??
Terence Parr
parrt at cs.usfca.edu
Wed Nov 10 11:13:48 PST 2004
Hi,
[i posted this email to http://www.antlr.org/blog/antlr3/trees.tml ]
After Loring bitched at me on the phone yesterday <snicker>, I'm
beginning to think he's right: if separating the node data from the
navigation is the right concept, then trees should be a single
implementation that simply carry a "payload" object defined by the
user. This is like Sun's MutableTreeNode.
The interesting thing is that the tree nodes themselves are all
homogeneous, but the payload object could be totally different at each
node. So, the parser creates exactly one kind of node with a variety
of payload (or a single) types. The payload can be defined in the
grammar itself so that ANTLR can generate the required type(s).
grammar P;
AST {
Token token; // probably a default field
String blort;
...
}
Then ANTLR would generate P_AST type for use when building trees. For
heterogeneous trees, we currently allow type names in the tokens
section:
tokens {
ID<AST=IDASTNode>; //something like that
...
}
If we allow field specifications, that is better than specifying a type
name maybe:
tokens {
ID<String name, int n>;
...
}
Whatever. A random idea.
As a bonus to the payload strategy, we can enhance the tree
functionality later w/o forcing alterations in people's application;
their payload objects still fit in our nodes.
This all comes at the cost of an additional object creation (payload
creation + node creation).
Side note: Mitchell suggested Tokens and Tree nodes should have not
only fixed fields like this, but the ability to dynamically acquire
"attributes"; this would basically be a hashmap. It cuts down on a
bazillion subclasses. It would be useful when parsing xml for example.
The TAG token could have a list of tag attributes w/o creating
subclasses etc...
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list