Pascal’s Achilles Heel

The Pascal programming language was designed for teaching. Anyone who learned programming in the 1970s and 80s likey did so using Pascal. One of the main idiosyncrasies with the design of Pascal is the use of semicolons. In C, semicolons perform the task of terminating statements, so it is hard to use them in the wrong context. In Pascal, semicolons are statement separators adopted from the syntax of ALGOL. This basically means that they do not exist in places where the layout of the program would make them redundant. For example, consider this piece of code in C:

1 while (!odd(y))
2 {
3    y = y / 2;
4    x = sqrt(x);
5 }

whereas in Pascal the code would look like this:

1 while not odd(y) do
2 begin
3    y := y div 2;
4    x := sqr(x)
5 end;

The two statements on lines 3 and 4 are separated by a semicolon. The semicolon after the end on line 5 separates the while loop from the next statement. Most Pascal compilers will also accept the following:

1 while not odd(y) do
2 begin
3    y := y div 2;
4    x := sqr(x);
5 end;

But failure to add the semicolon at the end of line 5, as in:

1 while not odd(y) do
2 begin
3    y := y div 2;
4    x := sqr(x);
5 end
6 y := y - 1;
7 z := x * z;

This will result in an an error of the form:

Fatal: Syntax error, ";" expected but "identifier Y" found
Fatal: Compilation aborted

Similarly, a semicolon before an else statement will effectively chop the if statement in two, causing an error. The following is the correct way:

if i > j
then maxi := i
else maxi := j;

Basically if you are writing programs in Pascal, remember the following two rules:
a semicolon before ELSE is wrong;
a semicolon before END is unnecessary. 

Advertisements

One thought on “Pascal’s Achilles Heel

  1. codeinfig says:

    not to negate anything you said here, though i think bash is the same way (and certainly basic with the colon, but its already a redheaded stepchild, alas. out of the languages mentioned though, i think its the hardest language to break with spare colons, per some popular dialects.)

    as for languages where ; terminates a line, javascripts syntax occasionally requires a lack of semicolon where a semicolon is reasonably expected as well, in some block statements. youll find more examples in pascal im sure– which perhaps originated this issue, but its not alone.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s