1

I am upgrading my Antlr grammar file to latest Antlr4.

I have converted most of the file but stuck in syntax difference that I can't figure out. The 3 such difference is:

equationset:    equation* EOF!;
equation:   variable ASSIGN expression -> ^(EQUATION variable expression)
;
orExpression
:   andExpression ( OR^ andExpression )* 
;

In first one, the error is due to !. I am not sure whether EOF and EOF! is same or not. Removing ! resolves the error, but I want to be sure that is the correct fix.

In 2nd rule, -> and ^ is giving error. I am not sure what is Antlr4 equivalent.

In 3rd rule, ^ is giving error. Removing it fixes the error, but I can't find any migration guide that explains what should be equivalent for this.

Can you please give me the Antrl4 equivalent of these 3 rules and give some brief explanation what is the difference? If you can refer to any other resource where I can find the answer is OK as well.

Thanks in advance.

1 Answers1

3

Many of the ANTLR3 grammars contain syntax tree manipulations which are no longer supported with ANTLR4 (now we get a parse tree instead of a syntax tree). What you see here is exactly that.

  • EOF! means EOF should be matched but not appear in the AST. Since there is no AST anymore you cannot change that, so remove the exclamation mark.
  • The construct -> ^(EQUATION variable expression) rewrites the AST created by the equation rule. Since there is no AST anymore you cannot change that, so remove that part.
  • OR^ finally determines that the OR operator should become the root of the generated AST. Since there is no AST anymore ..., you got the point now :-)
Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • In v4 if you need an AST you'll have to generate it yourself (which is not that bad, I'd argue it gives you more freedom), I wrote an example of how to do that [here](http://stackoverflow.com/a/29996191/3764814). – Lucas Trzesniewski Apr 14 '17 at 11:02