[fpc-pascal] case statement

Gerhard Scholz gs at g--s.de
Sat Dec 16 20:07:45 CET 2023


 statement list = statement { ";" statement }

statementlist itself is not explained; I assume "statementlist" to be the 
same as "statement list"

It is included in the definition of the try...except statement.



ELSE/OTHERWISE

I assume that came historically; the first implementation of a PASCAL 
compiler I have seen had no else or otherwise in the case startement. Some 
ater dialects introduced ELSE, other dialect(s) used OTHERWISE, FPC then 
allowed both.



Ambiguity:

It is so. I was a bit astonished.

   i := 4 ;
   case i of
     1,
     2: if odd ( i )
          then write ( 'odd' )
         else write ( 'even' ) ;
    end ;

produces nothing, else is part of the if statement, in case of ambiguity the 
nearest possibility is used.

   i := 4 ;
   case i of
     1,
     2: if odd ( i )
          then write ( 'odd' )
    ;     else write ( 'even' ) ;
    end ;

produces the print 'even', else is part of the case statement

As I read the syntax of the case statement in ref.pdf, the semicolon before 
the else is not even allowed.

the syntax is (see below)

> case-statement = "CASE" expression "OF" case { ";" case } [ else-part ] 
> [ ";" ] .


if you change that to

> case-statement = "CASE" expression "OF" case { ";" case } [ ";" ] [ 
> else-part [ ";" ] ] .


then you have what the compiler does.



I never had the problem because before the else I always inserted a ";"

----- Original Message ----- 

From: "Adriaan van Os via fpc-pascal" <fpc-pascal at lists.freepascal.org>
To: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
Cc: "Adriaan van Os" <adriaan at adriaan.biz>
Sent: Thursday, December 14, 2023 4:53 PM
Subject: [fpc-pascal] case statement


>
> I am looking in detail at the syntax diagrams in the Freepascal Language 
> Reference (version 3.2.0)
>
> Section 13.2.2 discusses the case-statement. Translated to EBNF (WSN) the 
> syntax is
>
> case-statement = "CASE" expression "OF" case { ";" case } [ else-part ] 
> [ ";" ] .
> case = constant [ ".." constant ] { "," constant [ ".." constant ] } ":" 
> statement .
> else-part = [ "ELSE" | "OTHERWISE" ] statementlist .
>
> If this is correct (and the compiler really allows it) then a semicolon 
> between <case> and <else-part> is not required. Consequently, there is an 
> ambiguity between an if-then-else statement (as last statement of the 
> <case>) and an if-then statement (as last statement of the <case>) and an 
> <else-part>. This is extremely dangerous and I feel that at least the 
> Language Reference should warn against it.
>
> Even with an obliged semicolon, I always use "OTHERWISE instead of ELSE, 
> but that's my personal preference.
>
> By the way, the Language Reference doesn't specify what a <statementlist> 
> is.
>
> Regards,
>
> Adriaan van Os
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 



More information about the fpc-pascal mailing list