I was trying to use rule reference in grammar action.
我试图在语法动作中使用规则引用。
string_decl_list : (string_decl)+;
string_decl : ('STRING' id ':=' str ';')
{//table.currentScope().define(new BaseDescriptor(), id.text, ValueType.STRING);
System.out.println($str.text);
};
str : STRINGLITERAL;
My grammar looks like this. The compilation is fine. But when I try to parse my file.
我的语法看起来像这样。汇编很好。但是当我尝试解析我的文件时。
There's an exception coming out.
有一个例外。
Exception in thread "main" java.lang.NoSuchFieldError: str
at MicroParser.string_decl(MicroParser.java:368)
at MicroParser.string_decl_list(MicroParser.java:312)
at MicroParser.decl(MicroParser.java:246)
at MicroParser.pgm_body(MicroParser.java:187)
at MicroParser.program(MicroParser.java:107)
at Compiler.main(Compiler.java:32)
Java Result: 1
I guess I am doing it right for the rule reference, could anyone help me about this? I don't know how to solve it
我想我正在为规则参考做正确的事,有人可以帮我解决这个问题吗?我不知道如何解决它
1 个解决方案
#1
0
You get the error
你得到错误
Exception in thread "main" java.lang.NoSuchFieldError: str
because you don't have a return value for the parser rule "str".
因为您没有解析器规则“str”的返回值。
Try something like this:
尝试这样的事情:
str returns [String str] : STRINGLITERAL { $str = new String($STRINGLITERAL.text); } ;
#1
0
You get the error
你得到错误
Exception in thread "main" java.lang.NoSuchFieldError: str
because you don't have a return value for the parser rule "str".
因为您没有解析器规则“str”的返回值。
Try something like this:
尝试这样的事情:
str returns [String str] : STRINGLITERAL { $str = new String($STRINGLITERAL.text); } ;