[antlr-interest] Rewrite rules with context-sensitive values

dhjdhj dhjdhj at gmail.com
Tue May 12 10:17:57 PDT 2009


Below is a partial (pretty standard, I  would assume) grammar that  
creates a tree representing a boolean comparison.   The TAttributeType  
is essentially a constant that represents the type of the found  
expressions, e.g, TAttributeType.atString, TAttributeType.atInteger,  
TAttributeType.atBoolean

If you feed it      a = b   you will get back   (EQ a b)   as expected  
and obviously if a and b are actually expressions, you get the  
appropriately nested subtrees

However, I need to capture the TYPE information so that I can generate  
a tree that looks something like
      (EQ atInteger a b)
or

     (EQ atString a b)

so that I can generate the correct code from the tree.

I suspect I need to use rewrite rules to do this but I have not been  
able to figure out how to include context-sensitive information in the  
rewrite rule

How is this problem normally handled?

Thanks,
David

------------------------------------------------------------




expression returns [TAttributeType type]
                 :
                 optionalSign
                 lhs = simpleExpression
                    {
                       $type = $lhs.type;
                    }
                    (
                     (comparisonOperator^ rhs = simpleExpression)	
                     {
                        $type =  
TErrorHandling.Compatible($comparisonOperator.tree.token,  
$comparisonOperator.token, $lhs.type, $rhs.type);
                     }
                    )* 	
	        ;





comparisonOperator returns [int token]
               : EQ { $token = EQ; }
               | NE { $token = NE; }
               | LT { $token = LT; }
               | LE { $token = LE; }
               | GT { $token = GT; }
               | GE { $token = GE; }
	      ;	


More information about the antlr-interest mailing list