[antlr-interest] Code Generation Error in 3.0b5

Randall R Schulz rschulz at sonic.net
Thu Nov 23 07:57:53 PST 2006


Hi,

This rule:

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
literal
returns [ Formula lf ]
    :   atomicFormula
    {
        $lf = $atomicFormula.af;
    }

    |   '~' atomicFormula
    {
        $lf = fFF.negateFormula($atomicFormula.af);
    }
    ;
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

Produces the following Java parser code. Note that in the second case,
the one with the '~', that the value of the call to the atomicFormula
rule is discarded instead of being assigned to the local variable
atomicFormula18, causing null to be passed to negateFormula().

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
    // $ANTLR start literal
    // TSTP.g:772:1: literal returns [ Formula lf ] : ( atomicFormula | '~' atomicFormula );
    public Formula literal() throws RecognitionException {
        Formula lf = null;

        Formula atomicFormula18 = null;

        try {
            // TSTP.g:774:4: ( atomicFormula | '~' atomicFormula )
            int alt18 = 2;
            int LA18_0 = input.LA(1);

            if (((LA18_0 == DistinctObject) ||
                        ((LA18_0 >= UpperWord) && (LA18_0 <= SignedInteger)) ||
                        ((LA18_0 >= 72) && (LA18_0 <= 73)))) {
                alt18 = 1;
            } else if ((LA18_0 == 69)) {
                alt18 = 2;
            } else {
                NoViableAltException nvae = new NoViableAltException(
                        "772:1: literal returns [ Formula lf ] : ( atomicFormula | '~' atomicFormula );",
                        18, 0, input);

                throw nvae;
            }

            switch (alt18) {
            case 1:
            // TSTP.g:774:4: atomicFormula
            {
                pushFollow(FOLLOW_atomicFormula_in_literal1226);
                atomicFormula18 = atomicFormula();
                _fsp--;

                lf = atomicFormula18;
            }
            break;

            case 2:
            // TSTP.g:779:4: '~' atomicFormula
            {
                match(input, 69, FOLLOW_69_in_literal1235);
                pushFollow(FOLLOW_atomicFormula_in_literal1237);
                atomicFormula();
                _fsp--;
                lf = fFF.negateFormula(atomicFormula18);
            }
            break;

            }
        } catch (RecognitionException re) {
            reportError(re);
            recover(input, re);
        } finally {
        }

        return lf;
    }
    // $ANTLR end literal
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-


Recasting the rule like this corrects the problem:

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
literal
returns [ Formula lf ]
    :   af = atomicFormula
    {
        $lf = $af.af;
    }

    |   '~' af = atomicFormula
    {
        $lf = fFF.negateFormula($af.af);
    }
    ;
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-


I'm using ANTLR 3.0b5 with Java 1.5.0_07 on Linux.


Randall Schulz


More information about the antlr-interest mailing list