Forum > Beginners

If-then-else inside try-finally; "EXCEPT" expected but "ELSE" found... ?

<< < (3/3)

Leledumbo:
Error diagnostics in parsing is still a good research topic. Most commercial compilers don't even produce readable one. FPC already does a good job, as it produces much more readable error messages than Turbo Pascal/Delphi (which only consists of line number info + extremely short error message). In C family world, only Java and Clang has managed to a better level, GCC is one step behind though already a lot better than MSVC.

If you ever learned about programming language parsing, you'll know that what the compiler sees at one point is not what we see (we always see as a whole). Yes, it can be improved, but you need to be a parser writer expert. There are times when multiple possibilities exist, so the compiler can't tell you exactly what's wrong. In the OP case:

--- Code: ---try
    writeln(somevar[1]);
    if somevar[1] = 'shut up' then
      writeln('take my money');
    else
      writeln('its a trap');
  finally
  somevar.free;
  end;
--- End code ---
When the parser encounters semicolon in the if body after writeln, it considers the if statement has finished, as the syntax says. Next, since the parser knows it's inside a statement list after a try statement, after the semicolon it expects either another statement or an except keyword or a finally keyword. But instead of either of those 3, the compiler meets else keyword, which is why it emits the error message. Certainly the error message can be extended to write all 3 possibilities (feel free to do so for those who can and is willing), or even suggests to remove the semicolon since it meets else right after it finishes parsing an if statement (heuristic error message). Just to tell you there are cases when the possibilities are 6 or perhaps more, and this could make the error message superfluous and suggestions are too long to be written.

This is how the compiler sees and parses your code.

MathMan:

--- Quote from: Leledumbo on December 24, 2014, 06:33:25 pm ---
--- Quote from: TinSoldier on December 24, 2014, 04:02:19 pm ---It's good to know that even Pascal can suffer from the same mis-design as C.

--- End quote ---
Both are born in the same era, and can't escape some of its original sins easily.

--- End quote ---

Well yes one can call that a sin from the past - but considering the alternative for a still parsable Pascal


--- Code: ---if( humblebee ) then
begin
  singlestatement;
end else begin
  anothersinglestatement;
end;

--- End code ---

I personally am convinced that more users would complain  ;)

So, yes, not a good design choice but probably better than the alternative ...

Leledumbo:
Actually

--- Code: ---if( humblebee ) then begin
  singlestatement;
end else begin
  anothersinglestatement;
end;
--- End code ---
Is my preference ;)

--- Quote from: MathMan on December 25, 2014, 11:18:19 am ---I personally am convinced that more users would complain  ;)

--- End quote ---
Certainly, but for maintenance reason, those who are experienced will do it. Why? Statements get added and removed by time. If you keep adding and removing that begin-end pair as well, it would be a hell. Keeping it there from the start will save you from silly mistyping.

serbod:
Maybe, simple compiler hint (or warning) on semicolon followed by 'else' can solve this problem.

Navigation

[0] Message Index

[*] Previous page

Go to full version