gpt4 book ai didi

java - ANTLR 如何获取重写的代码源? (使用 TokenRewriteStream)

转载 作者:行者123 更新时间:2023-11-30 11:48:52 25 4
gpt4 key购买 nike

我正在尝试创建简单的翻译器来翻译如下内容:

aaa | bbb | ccc

1 : aaa
2 : bbb
c : ccc

这是语法test01.g:

grammar test01;

options {
output=AST;
}

@members{
int N;
}

test
@init{
N = 0;
}:
id ('|' id)* -> id (BR id)*;

id : {N++;} ID -> {new CommonTree(new CommonToken(ID, Integer.toString(N) + " : " + $ID.text))};
ID : ('a'..'z')+;
BR : '\n';
WS : ' '{$channel=HIDDEN;};

翻译源 FooTest.java:

import org.antlr.runtime.*;

class FooTest {
public static void main(String[] args) throws Exception {
String text = "aaa | bbb | ccc";
System.out.println("parsing: "+text);
ANTLRStringStream in = new ANTLRStringStream(text);
test01Lexer lexer = new test01Lexer(in);
CommonTokenStream tokens = new TokenRewriteStream(lexer);
test01Parser parser = new test01Parser(tokens);
parser.test();
System.out.println("Result: "+tokens.toString());
}
}

当我运行它时,我期望得到类似的东西:

parsing: aaa | bbb | ccc
Result:
1 : aaa
2 : bbb
3 : ccc

但是我得到:

  parsing: aaa | bbb | ccc
Result: aaa | bbb | ccc

文本似乎没有被修改。

如何获取修改源?

最佳答案

您只是通过执行以下操作来打印扁平标记列表:

CommonTokenStream tokens = new TokenRewriteStream(lexer);
// ...
System.out.println("Result: "+tokens.toString());

如果您将 FooTest 类调整为:

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

class FooTest {
public static void main(String[] args) throws Exception {
String text = "aaa | bbb | ccc";
System.out.println("parsing: "+text);
ANTLRStringStream in = new ANTLRStringStream(text);
test01Lexer lexer = new test01Lexer(in);
CommonTokenStream tokens = new TokenRewriteStream(lexer);
test01Parser parser = new test01Parser(tokens);
CommonTree root = (CommonTree)parser.test().getTree();
for(int i = 0; i < root.getChildCount(); i++) {
CommonTree child = (CommonTree)root.getChild(i);
System.out.println("root.children[" + i + "] = " + child);
}
}
}

以下内容打印到控制台:

parsing: aaa | bbb | ccc
root.children[0] = 1 : aaa
root.children[1] = BR
root.children[2] = 2 : bbb
root.children[3] = BR
root.children[4] = 3 : ccc

请注意,您不需要在解析器类中放置全局变量。规则还处理变量(对它们而言是本地的)。这是首选:

grammar test01;

options {
output=AST;
}

test:
id ('|' id)* -> id (BR id)*;

id
@init{
int N = 0;
}
: {N++;} ID -> {new CommonTree(new CommonToken(ID, Integer.toString(N) + " : " + $ID.text))}
;

// other rules

关于java - ANTLR 如何获取重写的代码源? (使用 TokenRewriteStream),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8587176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com