I am trying to test an ANTLR grammar with such a standard test rig
import org.antlr.runtime.*;
class Main {
public static void main(String[] args) throws Exception {
SampleLexer lexer = new SampleLexer(new ANTLRStringStream(args[0]));
SampleParser parser = new SampleParser(new CommonTokenStream(lexer));
parser.program();
}
}
I have a test file called mytest00. Now I want to use this file as input. I suppose I am doing a stardard IO redirection here.
bash-3.2$ java Main < mytest00
But it gives me such an error message. What is the problem please? Thanks.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(SampleTest.java:5)
You’re trying to use
args[0]but you haven’t actually passed in any command line arguments – you’ve just redirected a file into the standard input of the process. So the array has no elements, and you’re getting an exception because you’re trying to get the first element of that empty array.It’s not really clear that you actually want
ANTLRStringStream. I suspect you wantANTLRInputStreamwrappingSystem.inifargs.length == 0, andANTLRFileStream(args[0])otherwise.