[antlr-interest] ANTLR 3.4: missing parameter in the method generated for a syntactic predicate for a rule with a parameter

Francis ANDRE francis.andre.kampbell at orange.fr
Fri Aug 17 01:44:32 PDT 2012


Hi Jim

I got the same missing reference -- here the 'f' parameter of the fooSpec rule 
-- for the following snippet while there is no syntactic predicate

fooStatement
     returns[FOO f = null]
@init
     { }
     : t = foo { f = new FOO (t); }
       (fooSpec[f])?
;
fooSpec
     [FOO f]
: blablabla


Is this the same problem?

Does it mean that one cannot have a rule with one or more parameter as soon as 
it is embedded in an optional rule?

FA

Le 16/08/2012 18:04, Jim Idle a écrit :
> Hi Francis,
>
> You cannot use a local parameter to a rule in a predicate because the
> predicate can be hoisted in to rules higher up the chain and so the
> predicate is its own function. If you need the local rule variable, then
> place it in a scope and reference it that way - the scope is available
> outside the rule, and you will find that that works.
>
> A question from your snippet here though is what the difference between a
> variable and a function is. For many languages, this can be left factored
> in to something like this:
>
>
> varFuncDecl:  ID
>                (  LPAREN params RPAREN
>                    (   funcBody  // Function
>                      | // Function prototype
>                    )
>                  | // Variable reference
>                )
> ;
>
> I don't know if that is the case for the language you are parsing, but
> keep this kind of technique in mind :)
>
>
> Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Francis ANDRE
>> Sent: Thursday, August 16, 2012 5:02 AM
>> To: antlr ANTLR
>> Subject: [antlr-interest] ANTLR 3.4: missing parameter in the method
>> generated for a syntactic predicate for a rule with a parameter
>>
>> Hi
>>
>> The generation of the method for the syntactic predicate of a rule with
>> a parameter is missing the parameter in its signature, which leads to a
>> compile error. I did not find out a way to fix this problem in the code
>> base of antlr.
>>
>> I would appreciate that someone looks at it and eventually fix this
>> problem because I am stuck
>>
>> Rgds
>>
>> FA
>>
>> Here the CMinus grammar modified with the variable rule having a
>> parameter and conditioned by a syntactic predicate:
>> -----------------------------------------------------------------------
>> -----------------------------------------------------------------------
>> ---------------------------
>> program
>> scope {
>>     List globals;
>>     List functions;
>> }
>> @init {
>>     $program::globals = new ArrayList();
>>     $program::functions = new ArrayList(); }
>>       : _declaration[2]_+
>>           ->
>> program(globals={$program::globals},functions={$program::functions})
>>       ;
>>
>> declaration[int i]
>>       : _(variable[i])=>variable[i]_
>> {$program::globals.add($variable.st);}
>>       |   f=function {$program::functions.add($f.st);}
>>       ;
>>
>>
>> _variable[int i]_
>>       :   type declarator ';'
>>           -> {$function.size()>0 && $function::name==null}?
>>              globalVariable(type={$type.st},name={$declarator.st})
>>           -> variable(type={$type.st},name={$declarator.st})
>>       ;
>>
>>
>> Here the generated code
>> ---------------------------------
>>       // $ANTLR start synpred1_CMinus
>>       public final void synpred1_CMinus_fragment() throws
>> RecognitionException {
>>           //
>> C:\\Users\\FrancisANDRE\\workspace\\zAntlr3\\src\\cminus\\CMinus.g:39:9
>> : ( variable[i] )
>>           //
>> C:\\Users\\FrancisANDRE\\workspace\\zAntlr3\\src\\cminus\\CMinus.g:39:1
>> 0:
>> variable[i]
>>           {
>>           pushFollow(FOLLOW_variable_in_synpred1_CMinus101);
>>           variable(i); _// <== i cannot be resolved to a variable
>> CMinusParser.java    /zAntlr3/src/cminus line 1744    Java Problem_
>>
>>
>>           state._fsp--;
>>           if (state.failed) return ;
>>
>>           }
>>
>>       }
>>       // $ANTLR end synpred1_CMinus
>>
>>
>>
>> _______________________________________________
>> antlr-dev mailing list
>> antlr-dev at antlr.org
>> http://www.antlr.org/mailman/listinfo/antlr-dev
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>



More information about the antlr-interest mailing list