Did "!", "^" and "$" had a special meaning in Antlr3?

Viewed 114

I dont have any prior knowledge about ANTLR(I recently learned a little bit about ANTLR4), but I have to translate an old grammar to a newer version and eclipse is telling me, that their are no viable alternatives for those characters and shows the syntax error " '!' came as a complete surprise to me". I already deleted those characters and it does not seam to be a problem, but maybe it had a special function in ANTLR3. Thanks in advance.

global_block:
    DATABASE! IDENTIFIER!
  | GLOBALS! define_section!+ END! GLOBALS!
  | GLOBALS! STRING!
;

main_block: MAIN sequence? END em=MAIN
    -> ^(MAIN MAIN '(' ')' sequence? $em)
  ;
1 Answers

^ and -> are related to tree rewriting: https://theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687090/Tree+construction

ANTLR4 does not support it (v4 has listeners and visitors for tree traversal, but no rewriting anymore). Just remove all of these ! and -> ... in parser rules (do not remove the -> ... inside lexer rules like -> channel(...), which is still supported in v4).

So in your case, these rules would be valid in ANTLR4:

global_block
 : DATABASE IDENTIFIER
 | GLOBALS define_section+ END GLOBALS
 | GLOBALS STRING
 ;

main_block
 : MAIN sequence? END MAIN
 ;

The $ can still be used in ANTLR4: they are used to reference sub-rules or tokens:

expression
 : lhs=expression operator=(PLUS | MINUS) rhs=expression
 | NUMBER
 ;

so that in embedded code block, you can do: $lhs.someField.someMethod(). In your case, you can also just remove them because they are probably only used in the tree rewrite rules.

EDIT

kaby76 has a Github page with some instructions for converting grammars to ANTLR4: https://github.com/kaby76/AntlrVSIX/blob/master/doc/Import.md#antlr3

Related