ANTLR 语法:解析器和词法分析器文字

发布于 2024-08-26 17:44:47 字数 349 浏览 6 评论 0原文

之间有什么区别

...
if_statement : 'if' condition 'then' statement 'else' statement 'end_if';
... 

这个语法:和这个:

...
if_statement : IF condition THEN statement ELSE statement END_IF;
...

IF : 'if';
THEN: 'then';
ELSE: 'else';
END_IF: 'end_if';
....

如果有任何差异,因为这会影响性能...... 谢谢

What's the difference between this grammar:

...
if_statement : 'if' condition 'then' statement 'else' statement 'end_if';
... 

and this:

...
if_statement : IF condition THEN statement ELSE statement END_IF;
...

IF : 'if';
THEN: 'then';
ELSE: 'else';
END_IF: 'end_if';
....

?

If there is any difference, as this impacts on performance ...
Thanks

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

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

发布评论

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

评论(4

权谋诡计 2024-09-02 17:44:47

除了威尔的答案之外,最好明确定义您的词法分析器标记(在您的词法分析器语法中)。如果您将它们混合在解析器语法中,则并不总是清楚词法分析器对标记进行标记的顺序。显式定义它们时,它们始终按照放入词法分析器语法的顺序(从上到下)进行标记。

In addition to Will's answer, it's best to define your lexer tokens explicitly (in your lexer grammar). In case you're mixing them in your parser grammar, it's not always clear in what order the tokens are tokenized by the lexer. When defining them explicitly, they're always tokenized in the order they've been put in the lexer grammar (from top to bottom).

温馨耳语 2024-09-02 17:44:47

最大的区别可能对您来说并不重要。如果您的词法分析器规则位于词法分析器中,那么您可以使用继承来让多个词法分析器共享一组通用的词法规则。

如果您仅在解析器规则中使用字符串,则无法执行此操作。如果您从不打算重用您的词法分析器语法,那么这个优势并不重要。

此外,我猜大多数 Antlr 老手更习惯在实际的词法分析器语法中查找词法分析器规则,而不是与解析器语法混合在一起,因此有人可能会说,通过将规则放入词法分析器中可以提高可读性。

根据这两种方法构建 Antlr 解析器后,不会对运行时性能产生影响。

The biggest difference is one that may not matter to you. If your Lexer rules are in the lexer then you can use inheritance to have multiple lexer's share a common set of lexical rules.

If you just use strings in your parser rules then you can not do this. If you never plan to reuse your lexer grammar then this advantage doesn't matter.

Additionally I, and I'm guessing most Antlr veterans, are more accustom to finding the lexer rules in the actual lexer grammar rather than mixed in with the parser grammar, so one could argue the readability is increased by putting the rules in the lexer.

There is no runtime performance impact after the Antlr parser has been built to either approach.

不再让梦枯萎 2024-09-02 17:44:47

唯一的区别是,在您的第一个生产规则中,关键字标记是隐式定义的。隐式定义的标记与显式定义的标记没有运行时性能影响。

The only difference is that in your first production rule, the keyword tokens are defined implicitly. There is no run-time performance implication for tokens defined implicitly vs. explicitly.

怀里藏娇 2024-09-02 17:44:47

还有另一个区别:当您显式定义词法分析器规则时,您可以通过您提供的名称访问它们(例如,当您需要检查特定标记类型时)。否则 ANTLR 将使用任意数字(带有前缀)。

Yet another difference: when you explicitly define your lexer rules you can access them via the name you gave them (e.g. when you need to check for a specific token type). Otherwise ANTLR will use arbitrary numbers (with a prefix).

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