[antlr-interest] Rewrite action causing error in parser?

Douglas Godfrey douglasgodfrey at gmail.com
Sun Oct 16 19:21:38 PDT 2011


The reason why you cannot reference a local rule variable from a predicate
is because the predicate MAY be "hoisted" to a parent rule for lookahead.
This only happens for a predicate that occurs before the first token or
alternative selection. The predicate executes in the context of the parent
rule and does not have addressability to any local variables from the rule
where it was defined.

This situation should cause antlr to automatically suppress hoisting of
the affected predicate to a parent rule but it does not.




On 10/16/11 6:53 PM, "Maximilien Colange" <macolange at gmail.com> wrote:

>It appears that this "bug" is frequently reported.
>It would be nice if ANTLR raised an error (or a warning) when a token is
>given a reference in a syntactic predicate.
>
>However, I do not know whether it is easy to detect. I already
>encountered this problem, and it occured in a "hidden" ANTLR-generated
>syntactic predicate. I am afraid the error is difficult to detect in
>such cases.
>
>And just for curiosity, why is not it possible to reference local
>variables or to assign from token in a syntactic predicate ?
>
>-- 
>Maximilien
>
>Le 10/15/11 11:34 PM, Jim Idle a écrit :
>> Your problem does not look to be the rewrite rule, but the fact that you
>> are referencing a local variable in a predicate, or have tried to assign
>> from a token in a predicate.
>>
>> Look for something like this
>>
>> ((id=IDENTIFIER)=>  id=IDENTIFIER)? ....
>>
>>
>> But regardless, this is the rewrite rule that is the problem as far as I
>> can see. Try commenting it out for instance.
>>
>> Jim
>>
>>> -----Original Message-----
>>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>>> bounces at antlr.org] On Behalf Of Ross Bamford
>>> Sent: Saturday, October 15, 2011 5:40 AM
>>> To: antlr-interest at antlr.org
>>> Subject: [antlr-interest] Rewrite action causing error in parser?
>>>
>>> Hi all,
>>>
>>> I have a grammar I'm currently working on (posted in another thread the
>>> other day), which has the following rule:
>>>
>>> meth_call_expr
>>>    :   (id = IDENTIFIER DOT)? func_call_expr ->  ^(METHOD_CALL {
>>> ($id==null) ?
>>> adaptor.create(SELF, "SELF") : adaptor.create(IDENTIFIER,
>>> $id.getText()) }
>>> func_call_expr)
>>>    ;
>>>
>>> As you can see, I'm using an action in the rewrite rule to insert
>>> either the
>>> (optional) IDENTIFIER, or an imaginary SELF node if IDENTIFIER is not
>>> specified. The problem I'm having is that this generates a parser that
>>> won't compile. Specifically, it generates the following bit of code
>>> (edited by hand for brevity and to highlight the error):
>>>
>>> /* **** [ CODE ] **** */
>>>      // $ANTLR start synpred6_BasicLang
>>>      public final void synpred6_BasicLang_fragment() throws
>>> RecognitionException {
>>>
>>>          Token =null; //<-- ERROR HERE
>>>
>>>          /* ... later on ... */
>>>
>>>          switch (alt23) {
>>>              case 1 :
>>>                  //
>>> C:\\Users\\chantelle\\workspace\\basiclang\\src\\com\\roscopeco\\basicl
>>> ang\\parser\\BasicLang.g:99:8:
>>> id= IDENTIFIER DOT
>>>                  {
>>>
>>> id=(Token)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_synpred6_BasicLan
>>> g232);
>>> if (state.failed) return ; //<-- AND HERE
>>>
>>>                  match(input,DOT,FOLLOW_DOT_in_synpred6_BasicLang234);
>>> if
>>> (state.failed) return ;
>>>
>>>                  }
>>>                  break;
>>>
>>>          }
>>> /* **** [ END ] **** */
>>>
>>> Obviously the problem is the "Token =null" line, which should be "Token
>>> id = null". Changing it by hand fixes the errors and makes the parser
>>> work as expected.
>>>
>>> So I have two questions - is this the right way to go about inserting
>>> an imaginary token if an optional token isn't in the input, and if so,
>>> what am I doing wrong to cause the error above?
>>>
>>> Thanks in advance,
>>> Ross
>>>
>>> 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
>
>
>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