java主< inputfile 不起作用(Main 是一个用于测试 ANTLR 语法的 java 类)

发布于 2024-12-14 03:09:08 字数 639 浏览 1 评论 0 原文

我正在尝试使用这样的标准测试设备来测试 ANTLR 语法

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();
  }
}

,我有一个名为 mytest00.txt 的测试文件。现在我想使用这个文件作为输入。我想我正在这里进行标准 IO 重定向。

   bash-3.2$ java Main < mytest00

但它给了我这样的错误信息。请问有什么问题吗?谢谢。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Main.main(SampleTest.java:5)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-12-21 03:09:08

您正在尝试使用 args[0] ,但实际上并未传入任何命令行参数 - 您只是将文件重定向到进程的标准输入中。因此该数组没有元素,并且您会收到异常,因为您试图获取该空数组的第一个元素。

目前还不清楚您是否实际上想要ANTLRStringStream。我怀疑你想要 ANTLRInputStream 包装 System.in if args.length == 0ANTLRFileStream(args[0])< /代码> 否则。

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.

懒猫 2024-12-21 03:09:08

当您使用 < 作为参数时,操作系统将其视为输入重定向。所以它会检查它并且不会将参数传递给 java main()

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()

穿透光 2024-12-21 03:09:08

异常的含义以及一般如何处理异常

at Main.main(SampleTest.java:5)

问题出现在代码的第 5 行中,即:

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

异常是:

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

这意味着您正在尝试从数组中检索 0 元素args,已使用非法索引访问数组,因为数组为空 (size=0)


解决方案示例

您想要使用此构造函数:

public ANTLRStringStream(String str)

为此,您可以:

  1. 读取某个字符串的标准输入
  2. 并将该字符串传递给ANTLRStringStream 构造函数

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文