如何创建给定形状的 antlr 树?
我有一个 antlr 语法,它有这样的规则:
rule:
ID (COMMA ID)*
;
其中 ID 是用于匹配典型变量名称的词法分析器规则,逗号是匹配逗号的词法分析器规则。因此,该规则匹配一个或多个逗号分隔的变量,
我想创建一棵如下所示的树:
ITEM
ID
ITEM
ID
....
其中 ITEM 是一个虚构的节点,我将其插入到找到的每个 ID 前面。
我将如何使用树重写语法来做这样的事情?
通常,如果我想收集我只使用的所有 id
rule:
ID (COMMA ID)* -> ^(ITEM ID+)
;
,但这会生成树:
ITEM
ID
ID
....
如果实现语言和 ANTLR 3.1.3 很重要,我正在使用 C#
I've got an antlr grammar that has a rule like so:
rule:
ID (COMMA ID)*
;
where ID is a lexer rule for matching a typical variable name and comma is a lexer rule that matches a comma. So the rule matches one or more comma separated variables
I'd like to create a tree that looks like so:
ITEM
ID
ITEM
ID
....
where ITEM is an imaginary node that I insert in front of each ID that is found.
How would I do such a thing with the tree rewrite syntax?
Typically if I wanted to gather all the ids I'd just use
rule:
ID (COMMA ID)* -> ^(ITEM ID+)
;
but this produces the tree:
ITEM
ID
ID
....
I'm using C# if it matters as the implementation langauge and ANTLR 3.1.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问完之后我很快就反应过来了。
为了获得树的形状,我需要使用重写语法,如下所示:
I came to my senses quickly after asking the question.
To get the tree shape I'd like I need to use the rewrite syntax like so: