I am trying to create rewrite rule rewriting node’s text to method’s return value.
But when i use rewriting rule parser parses only first node and then stops without any exception. Without rewrite-rule parser works fine.
Here is my not working example:
grammar test01;
options
{
output=AST;
}
@members{
public String MyTestFun(){
return "test";
}
}
test : id+;
id : ID -> {MyTestFun()}; // With this rule only first node is parsed
// id : ID -> {"test"}; // and with this rule too
ID : ('a'..'z')+;
So question is how to create rewrite rule rewriting node text to method’s value?
You can’t put a
Stringinside the right of a rewrite arrow->between the{ ... }. It needs to be an instance of aTree(CommonTreeis the default).This works:
If you now run the demo by doing:
(on Windows, the last command is:
java -cp .;antlr-3.3.jar test01Parser)the output
testis printed to the console.Note that if you change the return type of
myTestFun()toCommonToken, you will get a class cast exception:Whenever you have
output=ASTinside youroptions { ... }, you must use an instance of aTree.