...
In general, the static context language is regular and rewrite rule candidates could be found for a given stack using a DFA looking from right to left on the stack. In other words, If the stack top has rule var, then the DFA would rule out any context do not end in var or "...".
Note that the rewrite matches only have to match starting at the left edge of the specified context rule. They do not have to match an exact alternative within that rule nor a complete alternative. You might only want to alter the first or second token:
No Format |
---|
classDef:
'class' ID -> "class _<ID>" // prefix with '_'
|
Arbitrary semantic content
Sometimes grammatical context is not enough. For example, you might want to change an identifier reference in an expression depending on symbol table information:
No Format |
---|
expr:
{is local or arg}? ID -> "locals.<ID>"
|
What if the right-hand side needs to reference something other than an element matched on the left-hand side? In this case, we need to allow actions to set attributes.
No Format |
---|
expr:
{is local or arg}? ID -> (fname={currentFunc.name}) "<fname>_locals.<ID>"
|
What is currentFunc, though? Normally that is a parser class instance variable set to the currently matched function by an action in the parser grammar. For constructs that can nest such as classes in some languages, the "current class" pointer needs to be treated like a stack. ANTLR as these