基于条件删除AST中的java节点
我刚开始使用ANTLR。我有ANTLR语法,它创建了一个AST。我想检查一下,如果ComparisonExpr包含FuzzyExpr,那么我想从AST中删除这个ComparisonExpr节点和这个ComparisonExpr(如果有)前面的连接词(“and”、“or”)。请告诉我怎么做。我不知道我能不能按照ANTLR的常规重写规则来做
比如
Given the input: where $GPA = #high and age = 25
I want the output like this: where age = 25
(delete the conjunction "and" and ComparisonExpr=>"$GPA = #high") because it has the FuzzyExpr=>"#hight")
这是我语法的一部分
grammar Test;
options{
output=AST;
ASTLabelType=CommonTree;
}
WhereClause :="where" ExprSingle;
ExprSingle :OrExpr;
OrExpr :AndExpr ("or" AndExpr)*;
AndExpr :ComparisonExpr ("and" ComparisonExpr)*;
ComparisonExpr :ValueExpr((ValueComp)ValueExpr)?;
ValueExpr :ValidateExpr
|PathExpr
|ExtensionExpr
|FuzzyExpr;
FuzzyExpr :"#" Literal;
谢谢。 潘尼帕
# 1 楼答案
你可以重写规则。下面是一个草图,假设您使用操作符在树上生根:
将语义谓词放在构建树语句的前面。要匹配的第一个谓词将选择要写入的树。如果没有匹配项,将使用最后一个