[antlr-interest] Node types, versus node classes

Bryan Ewbank ewbank at gmail.com
Thu Jan 20 06:15:42 PST 2005


I'm working with tree parsers quite a bit, and have noticed that there
is quite a bit of repeated code when there are classes of related
operators.  For example, the six binary comparison operators (==, !=,
>=, <=, >, <) have very similar characteristics.

I'm considering folding these into a single BINARY_COMPARISON node
with different texts, but that requires the actions decode the
specific operator.

What is the "ANTLR way" (or "ways" :-) here?

   // one operator class, then decode operator in action
   bin_compare
     : #(BINARY_COMPARISON lhs:root rhs:root) { ... decode operator field ... }
     ;

   // distinct operators, possibly duplicate actions.
   bin_compare : eq | ne | ... ;
   eq: #(EQ lhs:root rhs:root) { ... code ... } ;
   ne: #(NE lhs:root rhs:root) { ... code ... } ;

   // another option, using syntactic predicates
   // here too I would need to decode the operator field in the action
   bin_compare : ( compare_op ) => #(.:op lhs:root rhs:root) { ... } ;
   compare_op : EQ | NE | GE | LE | GT | LT ;


More information about the antlr-interest mailing list