AST 重写规则与 "* +"在蚂蚁 [英] AST rewrite rule with " * +" in antlr

查看:26
本文介绍了AST 重写规则与 "* +"在蚂蚁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重写规则以在 antlr 中将解析树转换为 AST 树时遇到了麻烦.

I'm having a trouble about rewrite rule to convert from parsing tree into AST tree in antlr.

这是我的 antlr 代码:

Here's my antlr code:

grammar MyGrammar;

options {
  output= AST;
  ASTLabelType=CommonTree;
  backtrack = true;
}


tokens {
    NP;
    NOUN;
    ADJ;
}

//NOUN PHRASE
np  :    ( (adj)*  n+ (adj)*  -> ^(ADJ adj)*  ^(NOUN n)+ ^(ADJ adj)* )
    ;


adj : 'adj1'|'adj2';
n   : 'noun1';

当我输入 "adj1 noun1 adj2" 时,解析树的结果是这样的:

When I input "adj1 noun1 adj2" , the result of parse tree like this:

但是重写规则后的AST树似乎和解析树不完全一样,adj是双重的,没有顺序,像这样:

But the AST tree after rewrite rule seem not exactly like the parse tree, the adj is double and not in order, like this:

所以我的问题是如何重写规则以获得类似于上面解析树的结果?

So my question is how can I rewrite rule to have a result like the parsing tree above?

推荐答案

你的名词短语规则把所有的形容词都收集起来,复制到名词的两边,因为ANTLR不能自动区分一组匹配的adjs 和另一个.

Your noun phrase rule collects all the adjectives and copies them to both sides of the nouns because ANTLR can't automatically distinguish between one group of matched adjs and another.

以下是 np 规则的细分:

Here is a break-down of the np rule:

np  :    ( 
           (adj)*  //collect some adjectives
             n+ 
           (adj)*  //collect some more adjectives 
               -> ^(ADJ adj)*  //all adjectives written
                  ^(NOUN n)+   //all nouns written
                  ^(ADJ adj)*  //all adjectives written again
         )
    ;

区分这两个组的一种方法是将它们收集到各自的列表中.这是一个应用于规则 np 的示例:

One way to separate the two groups is to collect them into their own respective lists. Here's an example, applied to rule np:

np  :    ( 
           (before+=adj)*  //collect some adjectives into "before"
             n+ 
           (after+=adj)*  //collect some adjectives into "after"
               -> ^(ADJ $before)*  //"before" adjectives written
                  ^(NOUN n)+   //all nouns copied
                  ^(ADJ $after)*  //"after" adjectives written
         )
    ;

这样ANTLR就知道在n之前和之后写出哪些adj.

This way ANTLR knows which adjs to write out before and after the ns.

这篇关于AST 重写规则与 "* +"在蚂蚁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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