The Ppml if statement, which we can use to compare operator priority rating, has the following syntax:
if <condition1> then <format1> [elsif <condition2> then <format2>]* [else <format3>] end if
where brackets indicate optional arguments and the asterisk indicates zero or more occurrences. The <condition> part may be:
Combining if with the function prec, we produce the following rule for the prod operator:
prod(*exp1,*exp2) -> [<hv> if prec(*exp1) > 2 then [<h> "(" *exp1 ")"] else *exp1 end if "*" if prec(*exp2) >= 2 then [<h> "(" *exp2 ")"] else *exp2 end if] ;
Rather than using the integer 2 explicitly, it would be better to recover the priority value of prod which we have already defined in the precedence function. Since we cannot write prec(prod) in the formatting part of the rule, we need to access the tree in the pattern part of the rule, using the where instruction. Our rule thus becomes:
*x where *x in {prod(*exp1,*exp2)} -> [<hv> if prec(*exp1) > prec(*x) then [<h> "(" *exp1 ")"] else *exp1 end if "*" if prec(*exp2) >= prec(*x) then [<h> "(" *exp2 ")"] else *exp2 end if] ;
Since we know that this priority check is valid for any binary operator, we can use where to group the other operators into one general rule:
*x where *x in {plus(*exp1,*exp2), minus(*exp1,*exp2), prod(*exp1,*exp2), assign(*exp1,*exp2)} -> [<hv> if prec(*exp1) > prec(*x) then [<h> "(" *exp1 ")"] else *exp1 end if getsymbol(*x) if prec(*exp2) >= prec(*x) then [<h> "(" *exp2 ")"] else *exp2 end if] ;
Note that: