[英]Why do I get an error when trying to give a lexer as input of CommonTokenStream in ANTLRv4
所以我試圖在 java 中為我的 ANTLR4 解析器創建一個啟動器,當我嘗試運行我的代碼時出現“不兼容的類型”錯誤:
import org.antlr.runtime.*;
import java.io.IOException;
import static org.antlr.v4.runtime.CharStreams.fromFileName;
public class TLLauncher {
public static void main(String[] args) {
try{
String inp = "input.txt";
org.antlr.v4.runtime.CharStream in = fromFileName(inp);
gramLexer lexer = new gramLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer); // ###HERE
gramParser parser = new gramParser(tokens); // ###HERE
parser.r();
} catch (IOException e) {e.printStackTrace();}
}
}
我收到以下錯誤。
incompatible types: gramLexer cannot be converted to org.antlr.runtime.TokenSource
incompatible types: org.antlr.runtime.CommonTokenStream cannot be converted to org.antlr.v4.runtime.TokenStream
有人可以向我解釋我做錯了什么嗎?
另外,既然我們在這里,你能給我一個學習ANTRL的好資源嗎? 我需要以某種方式制作輸入代碼的 AST 並將其轉換為另一種語言,但我什至無法理解ANTLR 的工作原理以及如何完成它。
導入 org.antlr.runtime.*;
您正在導入錯誤的運行時。 org.antlr.runtime
是 ANTLR3 運行時。 ANTLR4 運行時位於org.antlr.v4.runtime
中(您已經在其中使用CharStream
和CharStreams
,但不是CommonTokenStream
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.