so I'm trying to create a launcher for my ANTLR4 parser in java and I get 'incompatible types' error when I try to run my code:
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();}
}
}
and i get the following errors.
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
can someone explain to me what I'm doing wrong?
ALSO, since we're here, can you give me a good source to learn ANTRL? I need to somehow make an AST of the input code and convert it to another language but I'm having trouble even understanding how ANTLR works and how i could get it done.
import org.antlr.runtime.*;
You're importing the wrong runtime. org.antlr.runtime
is the ANTLR3 runtime. The ANTLR4 runtime lives in org.antlr.v4.runtime
(from which you're already using CharStream
and CharStreams
, but not CommonTokenStream
).