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

Loring Craymer lgcraymer at yahoo.com
Thu Sep 2 10:52:16 PDT 2010


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