[antlr-interest] if/else if/else vs. if/elif/else

Serghei A stempuro2 at gmail.com
Thu Sep 2 12:14:34 PDT 2010


Ok - my last post was rushed.

I think this should be the correct IF/ELSE IF/ELSE statement:

ifExpr    :    'if' '(' rightExpr ')' block ('else' (ifExpr | block) )?
            ;



On Thu, Sep 2, 2010 at 2:33 PM, Serghei A <stempuro2 at gmail.com> wrote:

> Thank you Guys!
>
> I think I get the point now - I will re-implement the statement as follows:
>
> ifClause    :     'if' LRND ifCond=rightExpr RRND ifBlock=block
>                 ;
>
> ifExpr        :    ifClause ('else' ifClause)* ('else' elseBlock=block)?
>                 ;
>
>
> does it look reasonable to you as above?
>
>
>
>
> On Thu, Sep 2, 2010 at 1:52 PM, Loring Craymer <lgcraymer at yahoo.com>wrote:
>
>> The problem is that you are considering 'else if' as a single token
>> instead of
>> thinking of the second 'if' as being part of its own statement.  Just
>> delete the
>> 'else if' part of your ifExpr production and make sure that block can be
>> an
>> ifExpr.  That is,
>>
>> if (cond1) block1
>> else if (cond2) block2
>> else block3
>>
>> is interpreted as
>>
>> if (cond) block1
>> else { if (cond2) block2
>>          else block3
>> }
>>
>> which, you should find, is what you really want.
>>
>> --Loring
>>
>>
>>
>>
>>
>> ----- Original Message ----
>> > From: st3 <stempuro2 at gmail.com>
>> > To: antlr-interest at antlr.org
>> > Sent: Thu, September 2, 2010 9:45:54 AM
>> > Subject: [antlr-interest] if/else if/else vs. if/elif/else
>> >
>> >
>> > Hi,
>> >
>> > I have defined a simple if/else if/else rule  (below):
>> >
>> > ifExpr    :    'if' LRND  ifCond=rightExpr RRND ifBlock=block
>> >          ('else if' LRND elifCond+=rightExpr RRND  elifBlock+=block)*
>> >         ('else'  elseBlock=block)?
>> >
>> > this throws the following error:
>> >
>> > mismatched  character '{' expecting 'i'
>> > extraneous input '}' expecting 'return '
>> >
>> > I  can solve it by changing 'else if' to 'elif' - as clearly 'else if'
>>  and
>> > 'else' are confusing the lexer/parser.
>> >
>> > However, I was hoping to  have the easer-to-read 'else if'.
>> >
>> > Can someone suggest how I can  accomplish that?
>> >
>> > I tried the syntactic predicate ('else if')=> - but  that did not work.
>> >
>> > Thank you!
>> > --
>> > View this message in context:
>> >
>> http://antlr.1301665.n2.nabble.com/if-else-if-else-vs-if-elif-else-tp5491890p5491890.html
>> >
>> > Sent  from the ANTLR mailing list archive at Nabble.com.
>> >
>> > 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