ANTLR - 运行/调试期间的非确定性行为

发布于 2024-12-29 18:18:42 字数 1926 浏览 0 评论 0原文

我正在尝试使用 ANTLR(尝试过 3.3 和 3.4)。当我尝试运行测试代码时,发生了奇怪的事情。请先看我非常简单的代码,然后我会解释我的问题。

测试语法:

lexer grammar CSVLexer;
Comma 
  :  ','
  ;
LineBreak
  :  '\r'? '\n'
  |  '\r'
  ;
SimpleValue
  :  ~(',' | '\r' | '\n' | '"')+
  ;
QuotedValue
  :  '"' ('""' | ~'"')* '"'
  ;

测试代码:

import org.antlr.runtime.*;
public class JMain {
    public static void main(String[] args) throws Exception {
        String source = "val1,val2,val3";
        CSVLexer lexer = new CSVLexer(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        int n = 1;
        for (Object o : tokens.getTokens()) {
            CommonToken token = (CommonToken) o;
            System.out.println("token(" + n + ") = " + token.getText().replace("\n", "\\n"));
            n++;
        }
    }
}

编译:

$ export CLASSPATH=antlr3.jar
$ java org.antlr.Tool CSVLexer.g
$ javac *.java
$ java JMain

第一个问题是它不打印任何内容:)。我在 Intellij IDEA (10) 中工作,我尝试从这里运行代码但没有成功(在 CLASSPATH 中使用 ANTLR)。但是,如果我在 int i = 1 上放置断点,调试并等待,比如 1-3 秒,然后继续,它就会起作用!如果我在没有断点的调试模式下运行它,则不会。有人可以向我解释一下可能是什么问题吗?谢谢。

编辑:

所以我尝试了 tu put:

tokens.fill()

之后

CommonTokenStream tokens = new CommonTokenStream(lexer);

就成功了。然而,然后我开始调试 CommonTokenStream 构造函数(几分钟),当我进入 tokens.fill() 时,我得到了 IndexOutOfBoundException。所以不知何故,后台做了一些事情,但我在 IDEA 中看不到其他线程。

编辑2:

问题解决了。看来有必要首先调用 fill() 并且 BufferedTokenStream 可能不应该这样使用。我遵循了本教程 http://bkiers.blogspot.com/2011 /03/2-introduction-to-antlr.html 它可能以某种方式对作者有用(我想知道为什么)。 IntelliJ IDEA 调试器在本地对象上调用 toString() ,然后又调用 fill() ,这就是我设置断点时它起作用的原因。 toString() 修改状态是邪恶的!

I'm trying to use ANTLR (tried 3.3 and 3.4). Strange thing happens when I try to run my test code. Please see my very simple code first, I'll explain my problem afterwards.

Test grammar:

lexer grammar CSVLexer;
Comma 
  :  ','
  ;
LineBreak
  :  '\r'? '\n'
  |  '\r'
  ;
SimpleValue
  :  ~(',' | '\r' | '\n' | '"')+
  ;
QuotedValue
  :  '"' ('""' | ~'"')* '"'
  ;

Test code:

import org.antlr.runtime.*;
public class JMain {
    public static void main(String[] args) throws Exception {
        String source = "val1,val2,val3";
        CSVLexer lexer = new CSVLexer(new ANTLRStringStream(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        int n = 1;
        for (Object o : tokens.getTokens()) {
            CommonToken token = (CommonToken) o;
            System.out.println("token(" + n + ") = " + token.getText().replace("\n", "\\n"));
            n++;
        }
    }
}

Compiation:

$ export CLASSPATH=antlr3.jar
$ java org.antlr.Tool CSVLexer.g
$ javac *.java
$ java JMain

The first problem is that it doesn't print anything :). I work in Intellij IDEA (10) and I tried to run the code from here without success (with ANTLR in CLASSPATH). HOWEVER, if I put breakpoint on int i = 1, debug and wait, say 1-3 seconds before continuing, it works! If I run it in debug mode without breakpoint it doesn't. Could someone please explain to me what could be the problem? Thank you.

EDIT:

So I tried tu put:

tokens.fill()

right after

CommonTokenStream tokens = new CommonTokenStream(lexer);

and it worked. However then I started debugging CommonTokenStream constructor (several mins) and when I got on tokens.fill() I got IndexOutOfBoundException. So somehow something in background did something but I can't see no other threads in IDEA.

EDIT2:

Problem solved. It appears that it is necessary to call fill() first and that BufferedTokenStream probably wasn't meant to be used like this. I followed this tutorial http://bkiers.blogspot.com/2011/03/2-introduction-to-antlr.html where it probably somehow worked for author (I wonder why). IntelliJ IDEA debugger called toString() on local objects and it in turn called fill() so that's why it worked when I set breakpoint. toString() modyfing state is evil!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文