In my grammar I have rules as below:
set_stmt
:SET ID (DOT ID)? TO setExpr NEWLINE+
;
setExpr
: arithExpr
| ID (DOT ID)?
| STRINGLITERAL
;
For the different input types as below,
set id to id
set id to ""
set id to id.id
set id to arithExpr
set id.id to id
set id.id to ""
set id.id to id.id
set id.id to arithExpr
I must implement different logics in my visitor class. What's the easiest way for me to do that?
First decompose your grammar:
set_stmt
:SET ID TO setExpr NEWLINE+ #setvar
|SET ID DOT ID TO setExpr NEWLINE+ #setatt
;
setExpr
: arithExpr # number
| STRINGLITERAL # string
| ID # getvar
| ID DOT ID #getatt
;
This will generate a visitor with a visit method for each labeled alternative. This enables you to write different code in each visitor method.