0
votes

I am new to use the ANTLR. I have the ANTLR grammar which creates an AST. I want to check that if ComparisonExpr contains the FuzzyExpr then I want to delete this ComparisonExpr node and conjunction (“and”, “or”) in front of this ComparisonExpr(if it has) from the AST. Please suggest me how to do it. I don’t know that I can do by normal rewrite rule of ANTLR or not?

For example

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")

This is some part of my grammar.

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;

Thank you. Pannipa

1
Which version of ANTLR are you using? It will make a difference for the answer. - monty0
@monty0, the occurrence of output=AST in the options suggests the OP is using v3. - Bart Kiers
@Bart Kiers, I believe that it could also be ANTLR 2 with those options. - monty0
@monty0, no, in the old v2-times, it was a different syntax: antlr2.org/doc/options.html - Bart Kiers

1 Answers

0
votes

You can do this rewrite rules. Here's a sketch assuming you root your trees with the operator:

^(OR e1=expr e2=expr) 
 -> {isFuzzy($e1) && isFuzzy($e2)}? /* empty */
 -> {isFuzzy($e1)}?                 $e2
 -> {isFuzzy($e2)}?                 $e1
 ->                                 ^(OR $e1 $e2)
;

You put semantic predicates in front of your tree building statements. The first predicate to match will choose which tree is written. If nothing matches the last one will be used.