[antlr-interest] How can I modify a tree node value according to the value of another tree node ?
Michael Bedward
michael.bedward at gmail.com
Tue Mar 17 17:30:36 PDT 2009
> How could I store value in AS.g and how can AS.g and AStree.g communicate?
> Could you plz give me a small example?
>
Hi Annie
Just define variables in the members section of each grammar.
Here's a general example where parser grammar A.g needs to collect
data which will then be used in tree grammar Bwalker.g
In grammar A.g...
@members {
private List<MyClass> myList = new ArrayList<MyClass>();
public List<MyClass> getList() { return myList; }
}
Then one or more rules in A.g have actions to create MyClass objects
and add them to the list.
In grammar Bwalker.g...
@members {
private List<MyClass> aList;
public void setList( List<MyClass> list ) { aList = list };
}
Now the client code runs AParser and gets the AST and the list...
AParser parser = new AParser(...);
AParser.prog_return r = parser.prog();
CommonTree primaryAST = (CommonTree) r.getTree();
List<MyClass> listFromA = parser.getList();
Then it gives the AST and the list to Bwalker...
CommonTreeNodeStream nodes = new CommonTreeNodeStream(primaryAST);
nodes.setTokenStream(tokens);
BWalker bwalker = new BWalker(nodes);
bwalker.setList( listFromA );
BWalker.start_return r = bwalker.start();
Hope this helps
Michael
More information about the antlr-interest
mailing list