gpt4 book ai didi

c++ - Antlr4 C++访问歧义分支

转载 作者:行者123 更新时间:2023-12-02 10:02:38 27 4
gpt4 key购买 nike

所以说我有这样一条规则:

rule : '(' rule ')' | '!' rule '!';

现在在我的运行时中,我有以下方法:
antlrcpp::Any runtimeVisitor::visitRule(tinycParser::RuleContext *ctx) { 
...
}

如何检查自己是拳头还是第二种情况?这样的东西行得通吗?
if(ctx->rule(0)) visitRule(ctx->rule(0))

最佳答案

您可以像这样标记替代品:

rule
: '(' rule ')' #ParenthesizedRule
| '!' rule '!' #ExclamationMarkRule
| ...
;

然后,您可以为每个替代项(即 visitParenthesizedRulevisitExclamationMarkRule等)定义特定的访问者方法,而不是 visitRule

如果您不想在语法中添加任何内容,也可以只检查规则的第一个子项是开括号还是感叹号:
if (ctx.children[0].getText() == "(") {
...
} else if (ctx.children[0].getText() == "!") {
...
} else {
...
}

关于c++ - Antlr4 C++访问歧义分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61838347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com