[英]Missing variable in Antlr4 parser
我正在嘗試使用Antlr4構建一種語言的符號表。 我的語法文件中有以下規則。
/* Global String Declaration */
//string_decl : STRING id ASSIGN str SEMICOLON ;
string_decl returns [StrEntry s] : STRING id ASSIGN ex=str SEMICOLON
{ $s = new StrEntry(); s.addID($id.text); s.addValue($ex.text);} ;
我還創建了StrEntry類(虛擬實現)
public class StrEntry{
String value;
String id;
String type;
void addID(String x){
id = x;
}
void addValue(String c){
value = c;
}
}
當我編譯( javac *.java
)時,出現以下錯誤:
MicroParser.java:382: error: cannot find symbol
((String_declContext)_localctx).s = new StrEntry(); s.addID((((String_declContext)_localctx).id!=null? _input.getText(((String_declContext)_localctx).id.start, ((String_declContext)_localctx).id.stop):null)); s.addValue((((String_declContext)_localctx).ex!=null?_input.getText(((String_declContext)_localctx).ex.start, ((String_declContext)_localctx).ex.stop):null));
^
symbol: variable s
location: class MicroParser
它說缺少StrEntry類型的變量s,但是我已經在語法文件中定義了它。 我認為在MicroParser.java文件中對其進行編輯不是一個好主意,因為它是由Antlr4生成的。
我該怎么辦?
$s = new StrEntry(); s.addID($id.text); s.addValue($ex.text)
在這里,您同時使用了$s
(將在生成的Java代碼中轉換為_localctx.s
)和s
(將僅保留s
)。 后者是編譯器找不到的符號,因為在該塊中沒有定義該名稱的變量。
換句話說,您只需要始終使用$s
而不是s
,它將可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.