Given the rules:
list: 'LIST' code?;
code: 'CODE' CODE_VALUE;
CODE_VALUE: [0-9]+;
How can I set a default value for optional token code
if not present?
The only similar question I could found is How do I replace a missing optional token with a default?, but it is 10 years old and tagged Antlr3.
Thanks.
In ANTLR v3 there was a possibility to rewrite the parse tree into an abstract (syntax) tree. While doing so, one had the ability to "inject" tokens during this process. Since ANTLR v4 does not have this tree rewriting ability*, the answer to your question is: you cannot set a default value/token. My question to you: why do you need this? What use-case is there to inject a token?