如何捕获antlr3树语法中的标记列表?

pf_*_*les 4 antlr antlr3

我以一种虚拟语言为例:它只接受一个或多个“!”。它的词法分析器和语法规则是:

grammar Ns;

options {
  output=AST;
  ASTLabelType=CommonTree;
}
tokens {
  NOTS;
}

@header { 
  package test;
}
@lexer::header {
  package test;
}

ns : NOT+ EOF -> ^(NOTS NOT+);

NOT : '!';
Run Code Online (Sandbox Code Playgroud)

好的,如您所见,这代表一种接受“!”的语言 或者 '!!!' 或者 '!!!!!'...

我定义了一些有意义的类来构建 AST:

public class Not {
    public static final Not SINGLETON = new Not();

    private Not() {
    }
}



public class Ns {
    private List<Not> nots;

    public Ns(String nots) {
        this.nots = new ArrayList<Not>();
        for (int i = 0; i < nots.length(); i++) {
            this.nots.add(Not.SINGLETON);
        }
    }

    public String toString() {
        String ret = "";
        for (int i = 0; i < this.nots.size(); i++) {
            ret += "!";
        }
        return ret;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是树语法:

tree grammar NsTreeWalker;

options {
  output = AST;
  tokenVocab = Ns;
  ASTLabelType = CommonTree;
}
@header { 
  package test;
}
ns returns [Ns ret] : ^(NOTS n=NOT+) {$ret = new Ns($n.text);};
Run Code Online (Sandbox Code Playgroud)

以及带有一些示例数据的主类代码,用于测试生成的类:

public class Test {

    public static void main(String[] args) throws Exception {
        ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream("!!!".getBytes("utf-8")));
        NsLexer lexer = new NsLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        NsParser parser = new NsParser(tokens);
        CommonTree root = (CommonTree) parser.ns().getTree();
        NsTreeWalker walker = new NsTreeWalker(new CommonTreeNodeStream(root));
        try {
            NsTreeWalker.ns_return r = walker.ns();
            System.out.println(r.ret);
        } catch (RecognitionException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但最终打印的输出是“!”,而不是预期的“!!!”。这主要是因为这行代码:

ns returns [Ns ret] : ^(NOTS n=NOT+) {$ret = new Ns($n.text);};
Run Code Online (Sandbox Code Playgroud)

上面的 $n 只捕获了一个 '!',我不知道如何捕获 '!' 的所有三个标记,换句话说,捕获 '!' 的列表 与$n。有人可以帮忙吗?谢谢!

Bar*_*ers 5

!仅打印一个的事实是因为您的规则:

ns returns [Ns ret] 
  :  ^(NOTS n=NOT+) {$ret = new Ns($n.text);}
  ;
Run Code Online (Sandbox Code Playgroud)

或多或少被翻译为:

Token n = null
LOOP
  n = match NOT_token
END
return new Ns(n.text)
Run Code Online (Sandbox Code Playgroud)

因此,n.text永远只是一个!

您需要做的是将这些NOT令牌收集到一个列表中。+=在 ANTLR 中,您可以使用运算符而不是“单个令牌”运算符创建令牌列表=。因此,将您的ns规则更改为:

ns returns [Ns ret] 
  :  ^(NOTS n+=NOT+) {$ret = new Ns($n);}
  ;
Run Code Online (Sandbox Code Playgroud)

其翻译为:

List n = null
LOOP
  n.add(match NOT_token)
END
return new Ns(n)
Run Code Online (Sandbox Code Playgroud)

请务必将Ns类的构造函数更改为List

public Ns(List nots) {
    this.nots = new ArrayList<Not>();
    for (Object o : nots) {
        this.nots.add(Not.SINGLETON);
    }
}
Run Code Online (Sandbox Code Playgroud)

之后测试类的输出将是:

!!!
Run Code Online (Sandbox Code Playgroud)

祝你好运!