[antlr-interest] automatically generated syntatic predicates become semantic predicates (fragments) plus arguments generate parser which doesn't compile

Doucette, Charles cdoucette at vaultus.com
Mon Sep 15 18:52:37 PDT 2008


Many of my grammar rules have arguments.
Very few of my productions use explicit syntactic predicates or semantic predicates.
Almost all of them seem to generate implicit syntactic/semantic predicates (except the last production of each rule).

Here's the one that is giving me trouble:

dataObjectBodyElement[DataObjectDeclaration dataObj]
            // technically we only allow one trigger definition per file -
            // but let's not limit them in the grammar because that would
            // predefine an order (i.e. trigger always at the beginning or always at the end)
            @init{
                        SubTypeDeclaration stDecl = dataObj;
            }
            :(TRIGGER)=>tr=trigger
                        {
                                    if (tr != null) {
                                                dataObj.getBody().addStatement(tr);
                                    }
                        }
            | commonObjectBodyElement[dataObj]
            |  fieldDeclarations[stDecl]
            ;

In the method dataObjectBodyElement, the generated code looks great, and it gets the argument to pass to that rule from the argument to this rule.

When I look at this rule in ANTLRworks, it says that the production for commonObjectBodyElement corresponds to the semantic predicate {synpred20_vscript}?.

That generated code looks like this:

    // $ANTLR start synpred20_vscript
    public final void synpred20_vscript_fragment() throws RecognitionException {
        // C:\\software\\trunk\\vsw\\source\\com\\vaultus\\studio\\vscript\\parser\\antlr\\vscript.g:501:4: ( commonObjectBodyElement[dataObj] )
        // C:\\software\\trunk\\vsw\\source\\com\\vaultus\\studio\\vscript\\parser\\antlr\\vscript.g:501:4: commonObjectBodyElement[dataObj]
        {
        pushFollow(FOLLOW_commonObjectBodyElement_in_synpred20_vscript1392);
        commonObjectBodyElement(dataObj);

        state._fsp--;
        if (state.failed) return ;

        }
    }
    // $ANTLR end synpred20_vscript

This obviously doesn't compile since it refers to an argument of a parent rule (which is not part of the current context).

Is this an ANTLR bug or a user bug?

In any case, how can I refactor my grammar to avoid this situation (and generate a parser which will compile)?

For the record, here is the rule for commonObjectBodyElement:

commonObjectBodyElement[DataObjectDeclaration dataObj]
            : dom=dataObjectNamedMethod
                        {
                                    if (dom != null) {
                                                dataObj.getBody().addStatement(dom);
                                    }
                        }
            | idx=index
                        {
                                    if (idx != null) {
                                                dataObj.getBody().addStatement(idx);
                                    }
                        }
            ;


Thanks,
Chuck

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080915/dceb6d48/attachment.html 


More information about the antlr-interest mailing list