Search code examples
antlr4antlr3natty

Natty converting from anlr3 to antlr 4


as I'm new to antlr I have plenty of problems with syntactic predicates. I'v been trying to convert this grammar,which is part of natty grammar, in order to parse it with antlr4,I really got confused how to change it in a meaningful way.

date_time
 : (
      (date)=>date (date_time_separator explicit_time)?
      | explicit_time (time_date_separator date)?
    ) -> ^(DATE_TIME date? explicit_time?)
  | relative_time -> ^(DATE_TIME relative_time?)
  ;`

Solution

  • Syntactic predicates and re-write rules are no longer supported in ANTLR4. ANTLR4's parsing algorithm should be powerful enough for not needing syntactic predicates, and if you want to traverse the parse tree, have a look at these links:

    So, the rule you posted would look like this in ANTLR4:

    date_time
     : date ( date_time_separator explicit_time )?
     | explicit_time ( time_date_separator date )?
     | relative_time
     ;