ANTLR 生成空条件
我正在尝试学习使用 ANTLR,但在这种情况下我无法弄清楚我的代码有什么问题。我希望这对于任何有相关经验的人来说都会很容易。这就是语法(很短)。
grammar SmallTest;
@header {
package parseTest;
import java.util.ArrayList;
}
prog returns [ArrayList<ArrayList<String>> all]
:(stat { if ($all == null)
$all = new ArrayList<ArrayList<String>>();
$all.add($stat.res);
} )+
;
stat returns [ArrayList<String> res]
:(element { if ($res == null)
$res = new ArrayList<String>();
$res.add($element.text);
} )+ NEWLINE
| NEWLINE
;
element: ('a'..'z'|'A'..'Z')+ ;
NEWLINE:'\r'? '\n' ;
问题是,当我生成 Java 代码时,有一些空的 if 条件,并且编译器因此显示错误,我可以手动编辑它,但这可能会更糟。我想这里面有什么问题。
抱歉问这个问题,这真的很愚蠢,但我的例子与网站上的例子非常相似,我无法想象有什么方法可以再原子化差异。
非常感谢。
I'm trying to learn to use ANTLR, but I cannot figure out what's wrong with my code in this case. I hope this will be really easy for anyone with some experience with it. This is the grammar (really short).
grammar SmallTest;
@header {
package parseTest;
import java.util.ArrayList;
}
prog returns [ArrayList<ArrayList<String>> all]
:(stat { if ($all == null)
$all = new ArrayList<ArrayList<String>>();
$all.add($stat.res);
} )+
;
stat returns [ArrayList<String> res]
:(element { if ($res == null)
$res = new ArrayList<String>();
$res.add($element.text);
} )+ NEWLINE
| NEWLINE
;
element: ('a'..'z'|'A'..'Z')+ ;
NEWLINE:'\r'? '\n' ;
The problem is that when I generate the Java code there are some empty if conditions, and the compiler displays an error because of that, I could edit that manually, but that would probably be much worse. I guess something is wrong in this.
Sorry for asking, this has to be really stupid, but my example is so similar to those in the site that I cannot imagine a way to atomize the differences any more.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将列表的初始化放在规则的
@init { ... }
块中,该块在规则中的任何内容匹配之前执行。另外,您的
element
规则不应该是解析器规则,而应该是词法分析器规则(它应该以大写字母开头!)。解析器的入口点,
prog
规则,应该以EOF
标记结束,否则解析器可能会在所有标记得到正确处理之前停止。最后,
@header { ... }
部分仅适用于解析器(它是@parser::header { ... }
的简写),您还需要将包声明添加到词法分析器中。一个有效的演示:
SmallTest.g
Main.java
要运行它,请执行以下操作:
这会产生:
You should put the initialization of your lists inside the
@init { ... }
block of the rules, which get executed before anything in the rule is matched.Also, your
element
rule should not be a parser rule, but a lexer rule instead (it should start with a capital!).And the entry point of your parser, the
prog
rule, should end with theEOF
token otherwise the parser might stop before all tokens are handled properly.Finally, the
@header { ... }
section only applies to the parser (it is a short-hand for@parser::header { ... }
), you need to add the package declaration to the lexer as well.A working demo:
SmallTest.g
Main.java
And to run it all, do:
which yields:
尝试将
element
转换为令牌Try converting
element
into a token