Antlr AST产生(可能)疯狂 [英] Antlr AST generating (possible) madness

查看:108
本文介绍了Antlr AST产生(可能)疯狂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下可能吗?我想反转"输入给antlr的输入,并使每个标记成为上一个标记的子代.

Is the following even possible? I want to "reverse" the input given to antlr and make each token a child of the previous one.

因此,对于输入(假设每个令牌都由'.'字符分隔):

So, for the input (Assume each token is separated by the '.' char) :

Stack.Overflow.Horse

我希望我的语法产生以下AST:

I would like my grammar to produce the following AST:

Horse
  |---Overflow
         |---Stack

到目前为止,我已经设法反转了节点,但是我无法使它们彼此成为子节点:

So far, I've managed to reverse the nodes, but I'm unable to make them children of each other:

function
 : ID PERIOD function
   -> function ID
 | ID
 ;

ID  : 'a'..'z'*
    ;

推荐答案

我认为没有简单的方法可以做到这一点.您可以这样制定规则:

I don't think there's an easy way to do that. You could make your rule like this:

function
 : ID PERIOD function
   -> ^(function ID)
 | ID
 ;

但这只会使最后一个节点成为根节点,而所有其他节点为其子节点.例如,以下来源:

but that only makes the last node the root and all other nodes its children. For example, the following source:

a.b.c.d.e

将产生以下树:

    e
 / / \ \
d c   b a

我看不到简单的解决方法,因为当您第一次解析a.b.c.d.e时,a将是IDb.c.d.e的递归调用function:

I can't see an easy fix since when you first parse a.b.c.d.e, a will be the ID and b.c.d.e the recursive call to function:

a.b.c.d.e
| +-----+
|    |
|    `-----> function
|
`----------> ID

导致b.c.d.e将以a作为其子级的事实.然后,当b成为ID时,它也作为子元素添加到a旁边.在您的情况下,应将a作为子项删除,然后将其添加到b的子项列表中.但是AFAIK,这在ANLTR中是不可能的(至少,不是在语法内部以一种清晰的方式).

resulting in the fact that b.c.d.e will have a as its child. When then b becomes the ID, it too is added as a child next to a. In your case, a should be removed as a child and then added to the list of b's children. But AFAIK, that is not possible in ANLTR (at least, not in a clean way inside the grammar).

编辑

好吧,作为一种变通办法,我脑子里有一些优雅的东西,但这并没有达到我的期望.因此,作为不太优雅的解决方案,您可以将last节点作为重写规则中的根节点:

Okay, as a work-around I had something elegant in mind, but that didn't work as I had hoped. So, as a less elegant solution, you could match the last node as the root in your rewrite rule:

function
  :  (id '.')* last=id -> ^($last)
  ;

,然后使用+=运算符收集List中所有可能的先前节点(children):

and then collect all possible preceding nodes (children) in a List using the += operator:

function
  :  (children+=id '.')* last=id -> ^($last)
  ;

并使用解析器中的自定义成员方法将这些children注入"到树的根中(在List中从右到左!):

and use a custom member-method in the parser to "inject" these children into the root of your tree (going from right to left in your List!):

function
  :  (children+=id '.')* last=id {reverse($children, (CommonTree)$last.tree);} -> ^($last)
  ;

一个小演示:

grammar ReverseTree;

options {
  output=AST;
}

tokens {
  ROOT;
}

@members {
  private void reverse(List nodes, CommonTree root) {
    if(nodes == null) return;
    for(int i = nodes.size()-1; i >= 0; i--) {
      CommonTree temp = (CommonTree)nodes.get(i);
      root.addChild(temp);
      root = temp;
    }
  }
}

parse
  :  function+ EOF -> ^(ROOT function+)
  ;

function
  :  (children+=id '.')* last=id {reverse($children, (CommonTree)$last.tree);} -> ^($last)
  ;

id 
  :  ID
  ;

ID
  :  ('a'..'z' | 'A'..'Z')+
  ;

Space
  :  ' ' {skip();}
  ;

还有一个小测试班:

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("a.b.c.d.e    Stack.Overflow.Horse    singleNode");
        ReverseTreeLexer lexer = new ReverseTreeLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ReverseTreeParser parser = new ReverseTreeParser(tokens);
        ReverseTreeParser.parse_return returnValue = parser.parse();
        CommonTree tree = (CommonTree)returnValue.getTree();
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = gen.toDOT(tree);
        System.out.println(st);
    }
}

这将产生如下所示的AST:

which will produce an AST that looks like:

  • (image generated using http://graph.gafol.net)

对于输入字符串:

"a.b.c.d.e    Stack.Overflow.Horse    singleNode"

这篇关于Antlr AST产生(可能)疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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