Reputation: 1188
I don't understand how ANTLR4 being able to use direct let-recursion actually help us. Ok, you can write a grammar that will recognize a language, alright. But you rarely stop here. Usually you want to do something with parsed tree. And having to collapse several rules in one just to get a direct left-recursion does not help here.
An example. Say I want to define + and - operators and want them to have the same priority. In GOLD I would write something like this:
expression
: binaryExpression
| unaryExpression
;
binaryExpression
: plusExpression
| minusExpression
;
plusExpression
: expression '+' unaryExpression
minusExpression
: expression '-' unaryExpression
Surely in ANTLR4 I can do
expression
: expression '+' unaryExpression
| expression '-' unaryExpression
| unaryExpression
;
But when in generated visitor I will only have methods for expression
and unaryExpression
. How am I supposed to know what to do when I visit ExpressionContext
? Is it an addition or substruction?
Upvotes: 3
Views: 1228
Reputation: 1188
I found an answer here.
One can "label" alternatives like this:
expression
: expression '+' unaryExpression # plusExpr
| expression '-' unaryExpression # minusExpr
| unaryExpression # unaryExpr
;
After doing so you will find corresponding methods generated in base visitor.
Upvotes: 2