Actions are code blocks in the grammar written in the target language. Grammar actions add code to the generated class.
You can use actions in a number of places within a grammar, but the syntax is always the same: arbitrary text surrounded by curly braces.
You don’t need to escape a closing curly character if it’s in a string or comment: “}” or /*}*/. If the curlies are balanced, you also don’t need to escape: {…}. Otherwise, escape extra curlies with a backslash: \{ or \}. The action text should conform to the target language as specified with the language option.
Embedded code can appear in: @header and @members named actions, parser and lexer rules, exception catching specifications, attribute sections for parser rules (return values, arguments, and locals), and some rule element options (currently predicates).
The only interpretation ANTLR does inside actions relates to grammar attributes. Actions embedded within lexer rules are emitted without any interpretation or translation into generated lexers.
The generated class can be enhance through a (grammar action|code) on three points:
// @header will only affects the generated parser class
@header { ... }
// @lexer::header will affect the generated lexer class
@lexer::header { ... }
// @members will only affects the generated parser class
@members { ... }
// @lexer::members will affect the generated lexer class
@lexer::members { ... }
@rulecatch { catch-blocks }