[英]Extend ANTLR3 AST's
With ANTLR2, you could define something like this in grammar definition file: 使用ANTLR2,您可以在语法定义文件中定义类似的内容:
options
{
language = "CSharp";
namespace = "Extended.Tokens";
}
tokens {
TOKEN<AST=Extended.Tokens.TokenNode>;
}
And then, you could create a class: 然后,您可以创建一个类:
public class TokenNode: antlr.BaseAST
{
...
}
Any ideea if something like this can be used (delegate class creation to AST factory instead of me doing the tree replication manually)? 任何想法都可以使用(将类创建委托给AST工厂而不是我手动进行树复制)? It's not working just by simple grammar definition copy from old to new format, and I tried to search their site and samples for somthing similar.
它只是通过从旧格式到新格式的简单语法定义复制工作,我试图搜索他们的网站和样本以寻找类似的东西。 Any hints?
任何提示?
I'm not trying to create custom tokens, but custom 'node parsers'. 我不是要创建自定义令牌,而是自定义“节点解析器”。
In order to 'execute' a tree you have 2 choices (as far as I understood): 为了“执行”一棵树你有两个选择(据我所知):
In v2 case, I could decorate the tree node to whateveer method I would have liked and then call them after the parser ran by just calling something like 'execute' from root node. 在v2的情况下,我可以将树节点装饰成我想要的方法,然后在解析器运行之后通过从根节点调用类似'execute'的方法来调用它们。
I know little C#, but there shouldn't be much difference with the Java target. 我知道很少C#,但与Java目标应该没什么区别。
You can create - and let ANTLR use - a custom tree by setting the ASTLabelType
in the options { ... }
section (an XTree
in this case): 您可以通过在
options { ... }
部分(在本例中为XTree
中设置ASTLabelType
来创建 - 并让ANTLR使用 - 自定义树:
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
: 然后创建一个扩展
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
: 当您创建
TParser
的实例时,您必须创建并设置一个自定义TreeAdaptor
,用于创建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 有关详细信息,请参阅: http : //www.antlr.org/wiki/display/ANTLR3/Tree+construction
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.