With ANTLR2, you could define something like this in grammar definition file:
options
{
language = \"CSharp\";
namespace = \"Extended.Tokens\";
}
toke
I know little C#, but there shouldn't be much difference with the Java target.
You can create - and let ANTLR use - a custom tree by setting the ASTLabelType
in the options { ... }
section (an XTree
in this case):
grammar T;
options {
output=AST;
ASTLabelType=XTree;
}
tokens {
ROOT;
}
@parser::header {
package demo;
import demo.*;
}
@lexer::header {
package demo;
import demo.*;
}
parse
: Any* EOF -> ^(ROOT Any*)
;
Any
: .
;
You then create a custom class which extends a CommonTree
:
package demo;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class XTree extends CommonTree {
public XTree(Token t) {
super(t);
}
public void x() {
System.out.println("XTree.text=" + super.getText() + ", children=" + super.getChildCount());
}
}
and when you create an instance of your TParser
, you must create and set a custom TreeAdaptor
which creates instances of your XTree
:
package demo;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "ABC";
TLexer lexer = new TLexer(new ANTLRStringStream(source));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.setTreeAdaptor(new CommonTreeAdaptor(){
@Override
public Object create(Token t) {
return new XTree(t);
}
});
XTree root = (XTree)parser.parse().getTree();
root.x();
}
}
Running the demo:
java -cp antlr-3.2.jar org.antlr.Tool T.g -o demo/
javac -cp antlr-3.2.jar demo/*.java
java -cp .:antlr-3.2.jar demo.Main
will print:
XTree.text=ROOT, children=3
For more info, see: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction