Antlr4 子上下文索引
Antlr4 child context index
我正在使用 Antlr4 解析 Java.g4 语法文件。我使用的解析器规则是:
typeArgument
: typeType
| '?' (('extends' | 'super') typeType)?
;
我已经通过以下方式实现了此解析器规则的访问者方法:
public String visitTypeArgument(JavaParser.TypeArgumentContext ctx) {
StringBuilder typArg = new StringBuilder();
if(ctx.getChild(0).getText().equalsIgnoreCase("?")){
// '?' (('extends' | 'super') typeType)?
typArg.append("?").append(" ");
TypeTypeContext typTypCtx = ctx.typeType();
if(typTypCtx != null){
typArg.append(ctx.getChild(1).getText()).append(" "); // <- Confusion is here
typArg.append(this.visitTypeType(typTypCtx));
}
}
else{
TypeTypeContext typTypCtx = ctx.typeType();
typArg.append(this.visitTypeType(typTypCtx));
}
return typArg.toString();
}
我已经用 comment.I 指出了我的代码中的混淆,我正在解析像 <? extends SomeClassIdentifier>
.
这样的 typeArgument
为什么 ctx.getChild(1).getText()
returns "extends" 而不是 "extends SomeClassIdentifier"?
根据规则 '?' (('extends' | 'super') typeType)?
,应该只有两个子上下文,即一个用于 ?
,另一个用于 ('extends' | 'super') typeType'
。请大家帮我解惑!!!
According to rule '?' (('extends' | 'super') typeType)? there should be only two child contexts i.e. one for ? and another for ('extends' | 'super') typeType'.
我认为这是不正确的。在没有看到更多语法的情况下,我认为您应该从这条规则中得到三个 children,假设输入文本中存在可选的 (?) 短语:
?
作为隐式词法分析器标记
extends
或super
作为隐式词法分析器标记
typeType
作为它自己的 child 上下文,可能有它自己的一组 children,因为你的规则是递归的,因为 typeType
本身可以包含一个 typeType
有帮助吗?检查 children 的树,我认为它会有意义。 right-recursive 规则的上下文树可能会变得非常深,具体取决于您的输入文本。
我正在使用 Antlr4 解析 Java.g4 语法文件。我使用的解析器规则是:
typeArgument
: typeType
| '?' (('extends' | 'super') typeType)?
;
我已经通过以下方式实现了此解析器规则的访问者方法:
public String visitTypeArgument(JavaParser.TypeArgumentContext ctx) {
StringBuilder typArg = new StringBuilder();
if(ctx.getChild(0).getText().equalsIgnoreCase("?")){
// '?' (('extends' | 'super') typeType)?
typArg.append("?").append(" ");
TypeTypeContext typTypCtx = ctx.typeType();
if(typTypCtx != null){
typArg.append(ctx.getChild(1).getText()).append(" "); // <- Confusion is here
typArg.append(this.visitTypeType(typTypCtx));
}
}
else{
TypeTypeContext typTypCtx = ctx.typeType();
typArg.append(this.visitTypeType(typTypCtx));
}
return typArg.toString();
}
我已经用 comment.I 指出了我的代码中的混淆,我正在解析像 <? extends SomeClassIdentifier>
.
这样的 typeArgument
为什么 ctx.getChild(1).getText()
returns "extends" 而不是 "extends SomeClassIdentifier"?
根据规则 '?' (('extends' | 'super') typeType)?
,应该只有两个子上下文,即一个用于 ?
,另一个用于 ('extends' | 'super') typeType'
。请大家帮我解惑!!!
According to rule '?' (('extends' | 'super') typeType)? there should be only two child contexts i.e. one for ? and another for ('extends' | 'super') typeType'.
我认为这是不正确的。在没有看到更多语法的情况下,我认为您应该从这条规则中得到三个 children,假设输入文本中存在可选的 (?) 短语:
?
作为隐式词法分析器标记extends
或super
作为隐式词法分析器标记typeType
作为它自己的 child 上下文,可能有它自己的一组 children,因为你的规则是递归的,因为typeType
本身可以包含一个typeType
有帮助吗?检查 children 的树,我认为它会有意义。 right-recursive 规则的上下文树可能会变得非常深,具体取决于您的输入文本。