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

查看:31
本文介绍了扩展 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 的情况下,我可以将树节点装饰成我喜欢的任何方法,然后在解析器运行后通过从根节点调用类似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在这种情况下):

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 来创建您的 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天全站免登陆