动态创建词法分析器规则
这是一个简单的规则:
NAME : 'name1' | 'name2' | 'name3';
是否可以使用包含字符串的数组动态地为此类规则提供替代方案?
Here is a simple rule:
NAME : 'name1' | 'name2' | 'name3';
Is it possible to provide alternatives for such rule dynamically using an array that contains strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,只需在
Id
完全匹配后进行检查,看看Id
的文本是否匹配位于预定义的集合中。如果它在集合中(在我的示例中为Set
),请更改令牌的类型。一个小演示:
如果您现在运行以下演示:
您将看到打印以下内容:
ANTLR4
对于 ANTLR4,您可以执行以下操作:使用
class: 测试它,
它将打印:
In that case, simply do a check after the
Id
has matched completely to see if the text theId
matched is in a predefined collection. If it is in the collection (aSet
in my example) change the type of the token.A small demo:
And if you now run the following demo:
You will see the following being printed:
ANTLR4
For ANTLR4, you can do something like this:
test it with the class:
which will print: