在 ANTLR 中跳过输入文件的部分内容
我想构建一个解析器来分析大型输入文件,但我不需要整个输入文件,只需要它的某些部分。
例如,输入文件可能如下所示:
bla bla bla bla bla ...
EVENT: e1
type: t1
version: 1
additional-info: abc
EVENT: e2
type: t2
version: 1
uninteresting-info: def
blu blu blu blu blu ...
从该文件中,我想要的只是输入一个事件映射(e1=>t1,e2=>t2)。所有其他信息我都不感兴趣。
我如何构建一个简单的 ANTLR 语法来执行此操作?
I want to build a parser for analyzing a large input file, but I don't need the entire input file, only some parts of it.
For exmaple, the input file may look like this:
bla bla bla bla bla ...
EVENT: e1
type: t1
version: 1
additional-info: abc
EVENT: e2
type: t2
version: 1
uninteresting-info: def
blu blu blu blu blu ...
From this file, all I want is to have a map of event to type (e1=>t1, e2=>t2). All other information is of no interest for me.
How can I build a simple ANTLR grammar that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在词法分析器中引入一个布尔标志来跟踪是否遇到了
event
- 或type
- 关键字来做到这一点。如果遇到,词法分析器不应跳过该单词,而应跳过所有其他单词。一个小演示:
您可以使用以下类测试解析器:
它将产生以下输出:
You can do that by introducing a boolean flag inside your lexer that keeps track whether an
event
- ortype
-keyword has been encountered. If it has been encountered, the lexer should not skip the word, all other words should be skipped.A small demo:
You can test the parser with the following class:
which will produce the following output: