扩展ANTLR3 AST的 [英] Extend ANTLR3 AST's

查看:100
本文介绍了扩展ANTLR3 AST的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ANTLR2,您可以在语法定义文件中定义以下内容:

With ANTLR2, you could define something like this in grammar definition file:

options
{
   language = "CSharp";
   namespace = "Extended.Tokens";
}

tokens {
   TOKEN<AST=Extended.Tokens.TokenNode>;
}

然后,您可以创建一个类:

And then, you could create a class:

public class TokenNode: antlr.BaseAST
{
    ...
}

是否可以使用类似的想法(将类创建委托给AST工厂,而不是我手动进行树复制)?仅通过从旧格式到新格式的简单语法定义复制就无法正常工作,我试图在其网站和样本中搜索类似内容.有提示吗?

Any ideea if something like this can be used (delegate class creation to AST factory instead of me doing the tree replication manually)? 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'.

要执行"一棵树,您有2个选择(据我了解):

In order to 'execute' a tree you have 2 choices (as far as I understood):

  1. 创建树访问者"并处理值,或
  2. 通过几乎重复"语法定义来创建树解析器.

在v2中,我可以将树节点装饰为我想要的whateveer方法,然后在解析器运行后通过从根节点调用"execute"之类的方法来调用它们.

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.

推荐答案

我对C#知之甚少,但与Java目标之间应该没有太大区别.

I know little C#, but there shouldn't be much difference with the Java target.

您可以通过设置options { ... }部分(在本例中为XTree)中的ASTLabelType来创建-并让ANTLR使用-自定义树:

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
  :  .
  ;

然后创建一个扩展CommonTree的自定义类:

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());
  }
}

,并且在创建TParser的实例时,必须创建并设置一个自定义的TreeAdaptor,该自定义TreeAdaptor会创建XTree的实例:

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();
  }
}

运行演示:

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

将打印:

XTree.text=ROOT, children=3

有关更多信息,请参见: http://www.antlr.org/wiki/display/ANTLR3/Tree + construction

For more info, see: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction

这篇关于扩展ANTLR3 AST的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆