[fpc-pascal] Very odd case parsing problem

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sun Dec 29 16:08:37 CET 2013


Martin Frb wrote:

>> Drat- a dangling else in a case! I should have spotted that, but 
>> instead spent an hour or so picking at it.
> 
> 1) If you use Lazarus then you can have case-labels highlighted (e.g add 
> underline/border, blend foreground by mixing in another color, bold, 
> ...) It is in the color config.
> 
> That will (in this case) not highlight the else, so you will know the 
> else is part of the if.

Thanks, noted.

> 2) I tried your code and it compiles (2.6.2 win32)
> 
> {$mode objfpc}{$H+}
> 
> 
> procedure TForm1.FormCreate(Sender: TObject);
> var
>   a: (help, help_, quit_);
>   b: Integer;
> begin
>    try
>     case a of
>       help:        begin
>                    end;
>       help_:       begin
>                    end;
>       quit_:       if b = 1 then begin
>                    end
>     else
>     end // line 323
>   finally
>   end;
> end;

There were additional statements in between the  else  and the final 
end. Since the  else  associated with a  case  accepts multiple 
statements while the  else  associated with an  if  doesn't, the 
compiler was getting confused by what it thought was a spurious  end 
statement. I'm sure I've been bitten by this one before, and recently 
enough for it to be embarrassing.

I find myself habitually putting in an explicit  else  part in each 
case  statement, even if it's empty. That's a habit from Modula-2 days, 
I can't remember what the spec said but some (if not all) compilers 
insisted on it. In view of the potential for a dangling else, I'm not 
sure whether that's a good or a bad habit.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list