组合语法ANTLR选项过滤器

发布于 2024-08-21 04:20:29 字数 104 浏览 4 评论 0原文

我有一个组合语法(词法分析器和解析器位于同一文件上)。如何设置词法

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 技术交流群。

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

发布评论

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

评论(3

罪歌 2024-08-28 04:20:29

为了澄清巴特在他的回答中所说的话:

The options{filter=true;} only works in lexer-grammars. You will need to define a parser- and lexer grammar in two separate files:

您实际上不需要将词法分析器和解析器放在两个单独的文件中。您只需将过滤规则放入词法分析器语法中即可。词法分析器和解析器语法可以位于同一文件中。

尽管巴特建议将它们放在单独的文件中,但我通常使用的方法。

举个例子,您可以在单个文件中执行此操作

带有解析器和词法分析器的单个文件 Grammar.g

parser grammar FooParser;

parse
:  Number+ EOF
;

lexer grammar FooLexer;

options{filter=true;}

Number
:  '0'..'9'+
;

请注意过滤器规则如何位于词法分析器类定义中,但词法分析器和解析器位于同一文件中。

To Clarify what Bart said in his answer:

The options{filter=true;} only works in lexer-grammars. You will need to define a parser- and lexer grammar in two separate files:

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

parser grammar FooParser;

parse
:  Number+ EOF
;

lexer grammar FooLexer;

options{filter=true;}

Number
:  '0'..'9'+
;

Note how the filter rule is in the lexer class definition but the lexer and parser are in the same file.

苍暮颜 2024-08-28 04:20:29

你不知道。 options{filter=true;} 仅适用于词法分析器语法。您需要在两个单独的文件中定义解析器和词法分析器语法(编辑:不,它们可以放在一个文件中,请参阅 chollida 的答案):

FooParser.g

parser grammar FooParser;

parse
  :  Number+ EOF
  ;

FooLexer .g

lexer grammar FooLexer;

options{filter=true;}

Number
  :  '0'..'9'+
  ;

将这些词法分析器和解析器组合起来将产生一个解析器,该解析器仅解析由数字组成的文件(或字符串)中的字符串。

请注意,ANTLR 插件或 ANTLRWorks 可能会抱怨,因为词法分析器规则对其不可见,但在命令行上生成词法分析器和部件分析器:

java -cp antlr-3.2.jar org.antlr.Tool FooLexer.g 
java -cp antlr-3.2.jar org.antlr.Tool FooParser.g 

工作正常。

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

parser grammar FooParser;

parse
  :  Number+ EOF
  ;

FooLexer.g

lexer grammar FooLexer;

options{filter=true;}

Number
  :  '0'..'9'+
  ;

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:

java -cp antlr-3.2.jar org.antlr.Tool FooLexer.g 
java -cp antlr-3.2.jar org.antlr.Tool FooParser.g 

works fine.

东风软 2024-08-28 04:20:29

只是,不要分别将解析器语法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 ^^

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