/
Special symbols in actions

Special symbols in actions

This table describes the complete set of special symbols you can use in actions within your grammar. These are translated by the codegen/action.g ANTLR v3 grammar (in filter mode). The rules mentioned below are found in action.g

Syntax

Description

$enclosingRule.attr

x is enclosing rule, y is a return value, parameter, or predefined property. Rule ENCLOSING_RULE_SCOPE_ATTR.

r[int i] returns [int j]
  :    {$r.i, $r.j, $r.start, $r.stop, $r.st, $r.tree}
  ;

$tokenLabel.prop
$tokenRef.prop

token scope attribute. Rule TOKEN_SCOPE_ATTR.

$rulelabel.attr
$ruleref.attr

Rule RULE_SCOPE_ATTR.

$label

either a token label or token/rule list label like label+=expr. Rule LABEL_REF.

$tokenref

in a non-lexer grammar ISOLATED_TOKEN_REF

$lexerruleref

Yields a Token object created from that rule or fragment rule. Rule ISOLATED_LEXER_RULE_REF.

$y

return value, parameter, predefined rule property, or token/rule
reference within enclosing rule's outermost alt.
y must be a "local" reference; i.e., it must be referring to
something defined within the enclosing rule. Rule LOCAL_ATTR.

r[int i] returns [int j]
  :    {$i, $j, $start, $stop, $st, $tree}
  ;

$x::y

the only way to access the attributes within a dynamic scope
regardless of whether or not you are in the defining rule. Rule DYNAMIC_SCOPE_ATTR.

            scope Symbols { List names; }
            r
            scope {int i;}
            scope Symbols;
                :    {$r::i=3;} s {$Symbols::names;}
                ;
            s    :    {$r::i; $Symbols::names;}
                ;

$x[-1]::y

previous (just under top of stack). Rule DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR.

$x[-i]::y

top of stack - i where the '-' MUST BE PRESENT;
i.e., i cannot simply be negative without the '-' sign! Rule DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR.

$x[i]::y

absolute index i (0..size-1). Rule DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR.

$x[0]::y

is the absolute 0 indexed element (bottom of the stack). Rule DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR.

$x.size()

returns the size of the current stack of the scope. Note: This particular syntax is target-dependent. Look at the target page for other targets than Java.

$r

r is a rule's dynamic scope or a global shared scope.
Isolated $rulename is not allowed unless it has a dynamic scope and there is no reference to rulename in the enclosing alternative, which would be ambiguous. Rule ISOLATED_DYNAMIC_SCOPE.

The following symbols relate to StringTemplate templates.

Syntax

Description

%foo(a={},b={},...)

Create instance of template foo, setting attribute arguments. Rule TEMPLATE_INSTANCE.

%({name-expr})(a={},...)

indirect template ctor reference. Rule INDIRECT_TEMPLATE_INSTANCE.

%x.y = z;

set template attribute y of x (always set never get attr)
to z [languages like python without ';' must still use the
';' which the code generator is free to remove during code gen]. Rule SET_ATTRIBUTE.

%{expr}.y = z;

template attribute y of StringTemplate-typed expr to z. Rule SET_EXPR_ATTRIBUTE.

%{string-expr}

anonymous template from string expr. Rule TEMPLATE_EXPR.