私は次のようなものを翻訳する簡単な翻訳者を作成しようとしています:
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
テキストは変更されていないようです。
変更されたソースを取得する方法は?