I have a ANTLR project called "Test.g4" and with antlrworks2 I created without any problems the files: Test.tokens, TestBaseListner.java, TestLexer.java, TestLexer.tokens, TestListener.java and TestParser.java.
Now I want to use the grammer in my program Test.java:
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class Test {
public static void main(String[] args) throws Exception {
// create a CharStream that reads from standard input
ANTLRInputStream input = new ANTLRInputStream(System.in);
// create a lexer that feeds off of input CharStream
TestLexer lexer = new TestLexer(input);
// create a buffer of tokens pulled from the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// create a parser that feeds off the tokens buffer
TestParser parser = new TestParser(tokens);
ParseTree tree = parser.init(); // begin parsing at init rule
System.out.println(tree.toStringTree(parser)); // print LISP-style tree
}
}
When I try to compile it with "javac -classpath /path/java2/antlr-4.4-complete.jar Test.java" I get this errors:
Test.java:19: error: cannot find symbol
TestLexer lexer = new TestLexer(input);
^
symbol: class TestLexer
location: class Test
Test.java:19: error: cannot find symbol
TestLexer lexer = new TestLexer(input);
^
symbol: class TestLexer
location: class Test
Test.java:25: error: cannot find symbol
TestParser parser = new TestParser(tokens);
^
symbol: class TestParser
location: class Test
Test.java:25: error: cannot find symbol
TestParser parser = new TestParser(tokens);
^
symbol: class TestParser
location: class Test
4 errors
Thank you!
TestLexer.java and TestParser.java should also be compiled with Test.java in the same command, otherwise the compiler will not know where to look for their binaries. Try calling javac as follows:
javac -classpath /path/java2/antlr-4.4-complete.jar *java
Or manually pass all files:
javac -classpath /path/java2/antlr-4.4-complete.jar Test.java TestLexer.java TestParser.java
You need to build and import lexer and parser generated by ANTLR. To do it you need to:
add import statement to a file with your main method
put classes generated by ANTLR into a package as in your import statement
build both generated classes and class with your main method
Related
I want to use antlr4 in a java project in eclipse. I have finished prepare work, such as install ANTLR4 in eclipse market, wrote g4 file, add antlr-4.7-complete.jar to build path, and make target/generated-sources/antlr4 as source folder.
But in main java function, eclipse told me 'ArrayInitLexer' cannot be resolved to a type
here's my g4 file.
grammar ArrayInit;
init : '{' value (',' value)* '}' ;
value : init
| INT
;
INT : [0-9]+;
WS : [\t\r\n]+ -> skip;
It's correct, I test it in command line, and it can return me a parse tree.
here's my project structure:
and in the Test.java
public class Test {
public static void main(String[] args) throws IOException {
ANTLRInputStream input = new ANTLRInputStream(System.in);
Lexer lexer = new ArrayInitLexer(input); //ArrayInitLexer couldn't resolve
CommonTokenStream tokens = new CommonTokenStream(lexer);
ANTLRParser parser = new ArrayIntParser(tokens);
}
}
i want read text file in java8, i am getting error "Type mismatch: cannot convert from FileReader to Reader". If I change Reader class to FileReader than I get error "The constructor BufferedReader(FileReader) is undefined"
My statements are
Reader fr = new FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
Please suggest
To confirm that you are having a class with the name FileReader, just use the full class name in the code :
java.io.Reader fr = new java.io.FileReader("testfile.txt");
java.io.BufferedReader br = new java.io.BufferedReader(fr);
This will assure that you use the specific class and not a yourPackage.FileReader class.
Then, since only FileReader seems to be problematic, you can clean it a bit like :
import java.io.*
...
Reader fr = new java.io.FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
Only specifying the FileReader full name.
NOTE:
using Class.GetPackage, you should find out which class you are using.
System.out.println(FileReader.class.getPackage());
Explanation:
JLS - 7.5. Import Declarations
The scope and shadowing of a type or member imported by these declarations is specified in §6.3 and §6.4.
6.4.1. Shadowing
A package declaration never shadows any other declaration.
A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:
any top level type named n declared in another compilation unit of p
any type named n imported by a type-import-on-demand declaration in c
any type named n imported by a static-import-on-demand declaration in c
Example
A
A.Run
A.Test
B
B.Test
In A.Run.java
System.out.println(Test.class.getPackage());
Here is the output :
Without import : Package A
Without import import B.* : Package A
Without import import B.Test : Package B
You're probably importing something other than java.io.BufferedReader and java.io.Reader.
This works
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Reader fr = new FileReader("testfile.txt");
BufferedReader br = new BufferedReader(fr);
}
}
Please check your imports;
I have written a program in Java. But when I compile this program it shows an error
How to solve this problem?
the source code of the program is:
import org.rosuda.JRI.Rengine;
public class JavaGDExample1 {
public static void main(String[] args) {
Rengine re;
String[] dummyArgs = new String[1];
dummyArgs[0] = "--vanilla";
re = new Rengine(dummyArgs, false, null);
re.eval("library(JavaGD)");
// This is the critical line: Here, we tell R that the JavaGD() device that
// it is supposed to draw to is implemented in the class MyJavaGD. If it were
// in a package (say, my.package), this should be set to
// my/package/MyJavaGD1.
re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");
re.eval("JavaGD()");
re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
re.end();
}
}
it shows this error
No symbol for REngine
please reply
I assume that you are getting 'cannot find symbol' error message when you attempt to compile this. In that case, it means that you have not added the relevant JAR file into your classpath.
You can specify the classpath as follows.
java -cp <PATH_TO_YOUR_LIB> <YOUR_CLASSES>
If you are using an IDE, you will have to add these JAR files as libraries into it.
In the following code snippet I really don't understand why the compiler is issuing
the "cannot find symbol" error message.
public class LU62XnsCvr extends Object
{
// these two variables (among many others) are declared here as "public"
static StringBuffer message_data = new StringBuffer();
static File Mesg_File = new File("C:\\...\\Mesg_File.txt"); // path snipped
public static void SendMesg() // This "method" is "called" from various
// sections of the LU62XnsCvr program
{
if (mesgcount == 1)
{
// On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File);
FileChannel MesgChnl = MesgOut.getChannel();
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128);
}
// Send Message to the Message Log
String mesg_str = message_data.toString(); // convert buffer to a string
MesgWork = mesg_str.getBytes(); // Convert string to byte array
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen ); // copy MesgWork to buffer
MesgChnl.write( Mesg_Bufr ); // write message buffer out to the file channel
Mesg_Bufr.clear();
message_data.append(" "); // set message_data to 16 blanks
for ( ndx = 0; ndx < MGbuflen; ++ndx )
{
MesgWork[ndx] = 0x20; // clear MesgWork byte area using blank character
}
} // End of Send Message log write sub-routine
The above looks okay to me; BUT I get the following:
src\LU62XnsCvr.java:444: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable MesgChnl
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:446: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.clear();
^
Unless I'm missing something here it appears that Mesg_Bufr is "spelled" correctly.
Why can't the compiler "find" the variable?
You're declaring Mesg_Bufr in the if block so it's only visible in that block.
if (mesgcount == 1)
{
//On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File) ;
FileChannel MesgChnl = MesgOut.getChannel() ;
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128) ;
}
Same goes for the others. I can't tell what you're trying to do (and tbh I don't care) but to make that run correctly you probably have to put all the code inside the if, or, better yet, return if mesg != 1.
import java.lang.*;
import java.util.*;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
public class Test
{
public static void main(String[] args) throws IOException
{
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
boolean[] correctAnswers = new boolean[20];
infile.close();
}
}
I'm getting these errors for some reason:
C:\Users\Rawr\Documents\Test.java:11: cannot resolve symbol
symbol : class Scanner
location: class Test
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
^
C:\Users\Rawr\Documents\Test.java:11: cannot resolve symbol
symbol : class Scanner
location: class Test
Scanner infile = new Scanner(new FileReader("historyGrades.txt"));
^
2 errors
Tool completed with exit code 1
I have no idea what's going on.
Help is appreciated, Thanks.
Which version of Java are you using? Scanner was added in 1.5.
Run the following command on command prompt (terminal);
java -version
If the returned version number is less than 1.5 then you have to download the new version of Java. Scanner class is not available in prior versions. Download the new version of SDK from here;
Java SE Downloads
After setting up the new version of Java, add the following import statement in your source file;
import java.util.Scanner;
Now compile your source. It should go like a F16 now. But feel free to ask in case of any problem.