I'm currently adapting several ANTLR3 grammar files for ANTLR4. Some of the old ANTLR3 rules use the returns keyword. This rule is valid in both ANTLR3 and ANTLR4:
aRule:
returns [String s]
anotherRule AND (aRule)?
{
$s = "foo";
}
;
...
Calling parser.aRule() on an ANTLR3 parser returns a String. Calling parser.aRule() on an ANTLR4 parser returns a RuleContext. Is the returns keyword in ANTLR4 purely for backward compatibility purposes? Obviously in both situations returns causes a public field String s to be added to the generated ARuleContext.
Would I be correct to assume ideally the returns keyword should no longer be used, and any sort of context state should be handled by a parse tree visitor?
returnsis still used/valid. In most cases I traverse and transform the parse tree manually, but a parse tree visitor may also do in cases where you don't need the extra flexibility provided by custom walking the parse tree. So in short: forget about thereturnsand do post processing in code afterwards based on the parse tree. - Onur