组合语法ANTLR选项过滤器
我有一个组合语法(词法分析器和解析器位于同一文件上)。如何设置词法
filter = true
分析器?
谢谢
I have a combined grammar (lexer and parser on the same file). How do I set the
filter = true
to the lexer?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了澄清巴特在他的回答中所说的话:
您实际上不需要将词法分析器和解析器放在两个单独的文件中。您只需将过滤规则放入词法分析器语法中即可。词法分析器和解析器语法可以位于同一文件中。
尽管巴特建议将它们放在单独的文件中,但我通常使用的方法。
举个例子,您可以在单个文件中执行此操作
带有解析器和词法分析器的单个文件 Grammar.g
请注意过滤器规则如何位于词法分析器类定义中,但词法分析器和解析器位于同一文件中。
To Clarify what Bart said in his answer:
You don't actually need to put the lexer and parser in 2 separate files. You just need to put the filter rule in the lexer grammar. Both the lexer and the parser grammars can be in the same file.
Though Bart's advice of putting them in seperate files is the approach I normally use.
So to take an example you can do this in a single file
Single file with parser and lexer Grammar.g
Note how the filter rule is in the lexer class definition but the lexer and parser are in the same file.
你不知道。
options{filter=true;}
仅适用于词法分析器语法。您需要在两个单独的文件中定义解析器和词法分析器语法(编辑:不,它们可以放在一个文件中,请参阅 chollida 的答案):FooParser.g
FooLexer .g
将这些词法分析器和解析器组合起来将产生一个解析器,该解析器仅解析由数字组成的文件(或字符串)中的字符串。
请注意,ANTLR 插件或 ANTLRWorks 可能会抱怨,因为词法分析器规则对其不可见,但在命令行上生成词法分析器和部件分析器:
工作正常。
You don't. The
options{filter=true;}
only works in lexer-grammars. You will need to define a parser- and lexer grammar in two separate files (EDIT: no, they can be put in one file, see chollida's answer):FooParser.g
FooLexer.g
Combining these lexer and parser will result in a parser that will only parse the strings in a file (or string) consisting of digits.
Note that ANTLR plugins or ANTLRWorks might complain because because the lexer rule(s) is/are not visible to it, but generating a lexer and partser on the command line:
works fine.
只是,不要分别将解析器语法yourGrammar和词法分析器语法yourLexer添加到解析器和词法分析器文件中,而是将语法yourGrammarAndLExer添加到组合的开头文件^^
just, instead of adding parser grammar yourGrammar and lexer grammar yourLexer to your parser and lexer files respectevely, add grammar yourGrammarAndLExer to the beginning of your combined file ^^