文件中的 Antlr 令牌

发布于 2025-01-03 20:04:05 字数 278 浏览 3 评论 0原文

向 Antlr 提供大量代币的最佳方式是什么? 假设我们有 100,000 个英语动词的列表,我们如何将它们添加到我们的语法中?我们当然可以包含一个像 verbs.g 这样的巨大语法文件,但也许有一种更优雅的方法,通过修改 .token 文件等?

grammar verbs;

VERBS:
'eat' |
'drink' |
'sit' |
...
...
| 'sleep'
;

标记还应该是词法分析器或解析器标记,即 VERBS: 或 verbs: ?可能是动词:。

What is the best way to feed Antlr with huge numbers of tokens?
Say we have a list of 100,000 English verbs, how could we add them to our grammar? We could of cause include a huge grammar file like verbs.g, but maybe there is a more elegant way, by modifying a .token file etc?

grammar verbs;

VERBS:
'eat' |
'drink' |
'sit' |
...
...
| 'sleep'
;

Also should the tokens rather be lexer or parser tokens, ie VERBS: or verbs: ? Probably VERBS:.

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

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

发布评论

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

评论(1

撩心不撩汉 2025-01-10 20:04:05

我宁愿使用语义谓词。

为此,您必须定义一个标记

word : [a-z]+

,并在每个要使用动词(而不是通用单词)的站点上放置一个语义谓词来检查解析的单词是否在动词列表中。

使用建议不要使用解析器/词法分析器来执行此类任务

  • 每个附加动词都会改变语法
  • 每个附加动词都会扩大生成的代码
  • 结合更容易
  • 大写/小写可以更容易处理

I rather would use semantic predicates.

For this you have to define a token

word : [a-z]+

and at every site you want to use a verb (instead of a generic word) put a semantic predicate that checks if the parsed word is in the list of verbs.

Using recommend not to use the parser/lexer for such a task

  • each additional verb would change the grammar
  • each additional verb enlarges the generated code
  • conjugation is easier
  • upper/lower case could be handled easier
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文