2

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)
zell
  • 9,830
  • 10
  • 62
  • 115

3 Answers3

4

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 want ANTLRInputStream wrapping System.in if args.length == 0, and ANTLRFileStream(args[0]) otherwise.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Sorry would you please be more precise? I really cannot catch it. What would you propose to do? I think my program is waiting for an input through its arg[0] parameter, and the standard input is the actual value that arg[0] will take. Shouldn't it work like that?? Thanks. – zell Nov 05 '11 at 10:16
  • @zell: No, it shouldn't work like that. There's a big difference between the standard input of the program and the arguments used to invoke it. What would you expect it to do if you'd written `java Main foo bar < mytest00`? In that case, `args` would be `{ "foo", "bar" }`. – Jon Skeet Nov 05 '11 at 10:20
  • Thanks Jon. But what would be a neat way to treat my input file as the input of the ANTLR test rig? Thanks. For information, I had used gunit for that purpose, but the problem of gunit to me is it provides no useful error message. So it would be nice to have a file as direct input of the JAVA Main. – zell Nov 05 '11 at 10:23
  • @zell: As I said in my answer, I suspect you want an `ANTLRFileStream` which lets you pass in a filename - or you can use `ANTLRInputStream` wrapping `System.in` if you still want the stdin redirection. – Jon Skeet Nov 05 '11 at 10:24
  • @zell, note that when using `ANTLRFileStream(args[0])` as @Jon suggested, you need to drop the `<` from `java Main < mytest00`, making that: `java Main mytest00` – Bart Kiers Nov 05 '11 at 10:43
  • Thank you for all your replies and comments. Very helpful. Really appreciate. And it works now! – zell Nov 05 '11 at 10:59
  • By the way, those ANTLR interpreters are so buggy that you can never, ever count on them. Since one week, I have had a dozen of examples where the interpreters report fautive errors. – zell Nov 05 '11 at 11:05
  • 1
    @zell, by "ANTLR interpreters" you probably mean the interpreter in ANTLRWorks (it's just one). The debugger from ANTLRWorks works perfect, however. – Bart Kiers Nov 05 '11 at 11:09
  • @Bart. Is there any other interpreter? You said Eclipse IDE used the same interpreter as ANTLRWorks. – zell Nov 05 '11 at 11:20
  • @zell, no, AFAIK, there's only one: ANTLRWorks' interpreter (used by the Eclipse plugin). Because you used interpreters (plural), I thought you were talking about more than one. – Bart Kiers Nov 05 '11 at 11:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4750/discussion-between-zell-and-bart-kiers) – zell Nov 05 '11 at 14:27
2

when you use < as parameter , OS treats it as input redirection . so it will check for it and it won't pass argument the java main()

jmj
  • 237,923
  • 42
  • 401
  • 438
2

What the exception means and how to deal with exceptions generally

at Main.main(SampleTest.java:5)

The problem appears in 5 line of your code, which is:

SampleLexer lexer = new SampleLexer(new ANTLRStringStream(args[0]));

and the Exception is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

which means you're trying to retrieve 0-element from your array args, the array has been accessed with an illegal index because the array is empty (size=0)


Example solution

You want to use this constructor:

public ANTLRStringStream(String str)

To do this you can:

  1. read standard input to some String
  2. pass this String to ANTLRStringStream constructor
lukastymo
  • 26,145
  • 14
  • 53
  • 66