[antlr-interest] References to Non-Terminals in Actions

Randall R Schulz rschulz at sonic.net
Tue Nov 21 22:57:04 PST 2006


Hi,

I have these parser rules in my grammar:

term
returns [ Term t ]
  : ( functionTerm  { $t = $functionTerm.ft; } )
  | ( variable    { $t = fFF.variableTerm($variable.v); } )
  ;


variable
returns [ Variable v ]
  : UpperWord
    {
      $v = fFF.individualVariable($UpperWord);
    }
  ;


The code this generates is correct for the first case (functionTerm) but 
not for the second (variable). Specifically, the generated code for the 
second case contains literally "$variable.v".

Why is $functionTerm handled correctly when $variable not?

Here's the code the "term:" rule generates:

switch (alt26) {
    case 1 :
        // TSTP.g:932:4: ( functionTerm )
    {
        // TSTP.g:932:4: ( functionTerm )
        // TSTP.g:932:6: functionTerm
        {
        pushFollow(FOLLOW_functionTerm_in_term1747);
        functionTerm22=functionTerm();
        _fsp--;
         t = functionTerm22;

        }
    }
    break;

    case 2 :
        // TSTP.g:933:4: ( variable )
    {
        // TSTP.g:933:4: ( variable )
        // TSTP.g:933:6: variable
        {
        pushFollow(FOLLOW_variable_in_term1758);
        variable();
        _fsp--;
         t = fFF.variableTerm($variable.v);
        }
    }
    break;
}


This is the header of the "variable:" rule's code:

    // $ANTLR start variable
    // TSTP.g:1017:1: variable returns [ Variable v ] : UpperWord ;
    public Variable variable() throws RecognitionException {



Thanks.

Randall Schulz


More information about the antlr-interest mailing list