我可以在运行时添加 Antlr 令牌吗?
我遇到的情况是,我的语言包含一些在构建时未知但在运行时已知的单词,导致需要不断重建/重新部署程序以考虑新单词。我在想 Antlr 是否可以从配置文件生成一些令牌?
例如,在一个简化的示例中,如果我有一条规则
rule : WORDS+;
WORDS : 'abc';
并且我的语言在运行时遇到“bcd”,我希望能够修改配置文件以将 bcd 定义为单词,而不必重建然后重新部署。
I have a situation where my language contains some words that aren't known at build time but will be known at run time causing the need to constantly rebuild / redeploy the program to take into account new words. I was wandering if it was possible in Antlr generate some of the tokens from a config file?
e.g In a simplified example if I have a rule
rule : WORDS+;
WORDS : 'abc';
And my language comes across 'bcd' at runntime, I would like to be able to modify a config file to define bcd as a word rather than having to rebuild then redeploy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将某种集合添加到您的词法分析器类中。该集合将保存所有运行时单词。然后,您在规则中添加一些可能与这些运行时单词匹配的自定义代码,并更改令牌的类型(如果令牌存在于集合中)。
演示
假设您要解析输入:
在运行时,单词
"foo"
和"baz"
应成为特殊的运行时单词。下面的语法展示了如何解决这个问题:还有一个小测试类:
它将产生以下输出:
演示 II
这是另一个更适合您的问题的演示(一开始我太快地浏览了您的问题,但我会留下我的第一个演示到位,因为它可能对某人有用)。其中没有太多评论,但我的猜测是您不会在理解发生的事情上遇到问题(如果没有,请随时要求澄清!)。
并且类:
将产生:
如果某些运行时单词以另一个单词开头,请小心。例如,如果您的运行时单词包含
"stack"
和"stacker"
,您希望首先检查较长的单词!根据字符串的长度对集合进行排序应该是有序的。最后要注意的是:如果运行时单词列表中只有
"stack"
并且词法分析器遇到"stacker"
,那么您可能不想创建 < code>"stack"-token 并让"er"
悬空。在这种情况下,您需要检查单词
中最后一个字符后面的字符是否不是字母:You could add some sort of collection to your lexer class. This collection will hold all runtime-words. Then you add some custom code inside the rule that could possibly match these runtime-words and change the type of the token if it is present in the collection.
Demo
Let's say you want to parse the input:
and at runtime, the words
"foo"
and"baz"
should become special runtime words. The following grammar shows how to solve this:And a little test class:
which will produce the following output:
Demo II
Here's another demo that is more tailored to your problem (I skimmed your question too quickly at first, but I'll leave my first demo in place because it might come in handy for someone). There's not much comments in it, but my guess is that you won't have problems grasping what happens (if not, don't hesitate to ask for clarification!).
and the class:
will produce:
Be careful if some of your runtime words start with another one. For example, if your runtime words contain
"stack"
and"stacker"
, you want the longer word to be checked first! Sorting the set based on the length of the strings should be in order.One final word of caution: if only
"stack"
is in your runtime word list and the lexer encounters"stacker"
, then you probably don't want to create a"stack"
-token and leave"er"
dangling. In that case, you'll want to check if the character after the last char in theword
is not a letter: