具有 ANTLR 文本属性的 NullPointerException
我有一个问题已经困扰了一段时间,如果可能的话,我希望得到一些帮助。
我在 ANTLR tree 语法中有一些规则:
block
: compoundstatement
| ^(VAR declarations) compoundstatement
;
declarations
: (^(t=type idlist))+
;
idlist
: IDENTIFIER+
;
type
: REAL
| i=INTEGER
;
我编写了一个 Java 类 VarTable,我将按照在源文件开头声明的方式将所有变量插入其中。该表还将保存它们的变量类型(即实数或整数)。我还可以使用此变量表来检查未声明的变量或重复声明等。
所以基本上我希望能够将变量类型从“声明”规则发送到“idlist”规则,然后循环遍历idlist规则中的每个标识符,将它们一一添加到我的变量表中。
我遇到的主要问题是,当我尝试访问“文本”属性时,如果“声明”规则中的 $t 变量(这是引用类型的一个),我会收到 NullPointerException。
然而,如果我尝试访问“type”规则中 $i 变量的“text”属性,那就没有问题了。
我查看了 Java 文件中生成 NullPointerException 的位置,但它对我来说仍然没有意义。
因为规则是,所以可能有多种类型,这是否有问题
(^(typeidlist))+
?
当我了解 idlist 规则时,我遇到了同样的问题,因为我不确定如何编写一个操作来允许我循环遍历找到的所有标识符令牌。
感谢任何帮助或评论。
干杯
I have a problem that I've been stuck on for a while and I would appreciate some help if possible.
I have a few rules in an ANTLR tree grammar:
block
: compoundstatement
| ^(VAR declarations) compoundstatement
;
declarations
: (^(t=type idlist))+
;
idlist
: IDENTIFIER+
;
type
: REAL
| i=INTEGER
;
I have written a Java class VarTable that I will insert all of my variables into as they are declared at the beginning of my source file. The table will also hold their variable types (ie real or integer). I'll also be able to use this variable table to check for undeclared variables or duplicate declarations etc.
So basically I want to be able to send the variable type down from the 'declarations' rule to the 'idlist' rule and then loop through every identifier in the idlist rule, adding them to my variable table one by one.
The major problem I'm getting is that I get a NullPointerException when I try and access the 'text' attribute if the $t variable in the 'declarations' rule (This is one one which refers to the type).
And yet if I try and access the 'text' attribute of the $i variable in the 'type' rule, there's no problem.
I have looked at the place in the Java file where the NullPointerException is being generated and it still makes no sense to me.
Is it a problem with the fact that there could be multiple types because the rule is
(^(typeidlist))+
??
I have the same issue when I get down to the idlist rule, becasue I'm unsure how I can write an action that will allow me to loop through all of the IDENTIFIER Tokens found.
Grateful for any help or comments.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法像在树语法中尝试那样引用生产规则中的属性,只能在解析器(或组合)语法中引用(它们是不同的对象!)。请注意,
INTEGER
不是产生式规则,只是一个“简单”标记(终端)。这就是为什么您可以调用其.text
属性。因此,如果您想在树语法中保留
type
规则的文本并将其打印在declarations
规则中,您可以执行如下操作:如果您确实想在不指定返回对象的情况下执行此操作,则可以执行以下操作:
请注意,
type
返回TreeRuleReturnScope
的实例,该实例具有名为的属性start
又是一个CommonTree
实例。然后,您可以在该CommonTree
实例上调用getText()
。You can't reference the attributes from production rules like you tried inside tree grammars, only in parser (or combined) grammars (they're different objects!). Note that
INTEGER
is not a production rule, just a "simple" token (terminal). That's why you can invoke its.text
attribute.So, if you want to get a hold the text of the
type
rule in your tree grammar and print it in yourdeclarations
rule, your could do something like this:But if you really want to do it without specifying a return object, you could do something like this:
Note that
type
returns an instance of aTreeRuleReturnScope
which has an attribute calledstart
which in its turn is aCommonTree
instance. You could then callgetText()
on thatCommonTree
instance.