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

Terence Parr parrt at cs.usfca.edu
Mon Sep 15 21:55:30 PDT 2008


This is a common "why won't my syntactic predicates work" question...i  
think a search will find results.
Ter
On Sep 15, 2008, at 6:55 PM, Doucette, Charles wrote:

> I forgot to mention that this fails to work on both the newest  
> version of ANTLRworks 1.2 plus the previous version (1.1.7?).
> I believe ANTLRworks 1.2 is bundled with ANTLR 3.1 – but I am not  
> sure.
>
> Chuck
>
> From: Doucette, Charles
> Sent: Monday, September 15, 2008 9:53 PM
> To: Antlr
> Subject: automatically generated syntatic predicates become semantic  
> predicates (fragments) plus arguments generate parser which doesn't  
> compile
>
> 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
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

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


More information about the antlr-interest mailing list