Question

I'm trying to compile a grammar and get it works in ANTLR4. The grammar was written for ANTLR3 and I'm getting a lot of compilation errors. Here is one of them:

syntax error: '->' came as a complete surprise to me while looking for rule element

This is the rule causing the error

tokens {
  ZONE;
  ZONE_OFFSET; 
}

time_zone_abbreviation
  : UTC  -> ZONE["UTC"]
  | EST  -> ZONE["America/New_York"]
  | CST  -> ZONE["America/Chicago"]
  | PST  -> ZONE["America/Los_Angeles"]
  | MST  -> ZONE["America/Denver"]
  | AKST -> ZONE["America/Anchorage"]
  | HAST -> ZONE["Pacific/Honolulu"]
  ;

I know the -> operator can be used to specify lexer command in ANTLR4. But what does the operator mean in ANTLR3?

Was it helpful?

Solution

Inside parser rules, the -> denotes a rewrite rule in ANTLR 3 that constructs an AST from the parser rule. More about this in this Q&A: How to output the AST built using ANTLR?

In ANTLR 4, the -> is only used in lexer rules to put certain tokens on a different channel (or skip them all together from the token stream). Since ANTLR 4 does not work with AST's, the -> no longer has an equivalent. When translating an ANTLR v3 grammar to a v4 grammar, simply remove the -> and everything on the right hand side of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top