I am writing a compiler by translating JavaCC to ANTLR4 and one of the rules involve passing parameters and getting return values from it.
I have to do something like the following for a rule 'term':
Term term(ReadOptions options, int priority):
{
int p = options.operatorSet.getNextLevel(priority);
Term t;
}
{
(
LOOKAHEAD({p==0})
t = simpleTerm(options)
|
LOOKAHEAD(<NAME_TOKEN>,{priority==1201 && is1201Separator(2)})
t = name()
|
t = operatorTerm(options, p)
)
{return t;}
}
The problem is that how do I match sub-rules on the basis of the value of 'p'. In the previous versions of ANTLR I could have used => and my problem would have solved but what do I do in ANTLR4 ?
The => operator in previous versions of ANTLR is no longer necessary in ANTLR 4.
(x) => y previously, in ANTLR 4 you can simply use y.{x}? => y previously, then in ANTLR 4 you can simply use {x}? y.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With