Errors are often the bane of a novice programmer's existence. You write what you think is a perfectly fine program, and the compiler chokes on it. Or worse, the compiler compiles it and the operating system chokes on it. Error messages can range anywhere from highly descriptive like "Line 5: Identifier not found" to completely unintelligible like "Access violation at address 0xFFAB23D9". As you become more comfortable with programming, you will develop an intuition about what less-than-helpful error messages probably mean, and modern integrated development environments can generally also point you in the right direction. Here we will cover a few of the most common errors and describe the differences between compiler errors like "Line 5: Identifier not found" and runtime errors like "Access violation at address 0xFFAB23D9".
Compiler errors can be only one of a few problems. First, you may have mangled the syntax of the language you are using which results in a syntax error. For example, try compiling the following program:
program Workspace;
var Str : String;
begin
Str = 'test';
end.
This program fails with an error on line 4. Before reading on, can you spot the error? The actual compiler error message displayed depends completely on the quality of the compiler. The Free Pascal Compiler reports Line 4: Error: Illegal expression. Unfortunately, this error message is only somewhat helpful. At least we know the line that the error occured on, but the compiler does not tell us exactly what is wrong. On the other hand, Delphi (a commercial Object Pascal compiler) reports Line 4: ':=' expected but '=' found. This is a compiler syntax error. A syntax error is analogous to a gramatical punctuation mistake in an English sentence. The statement above was intended to be an assignment of 'test' to the variable Str, and the way it is written uses the relational equals (=) operator which results in a Boolean expression. This is akin to using a colon in an English sentence when you needed a semicolon. If you did not spot that error on your own, don't worry, the difference between := and = is so subtle that many professional programmers can miss it at first glance.
Now let's consider another common error. See if you can spot this one and then compile it to see what happens:
program Workspace;
var
Num : Integer;
Str : String
begin
Str := 'test'; Num := 12;
end.
This program fails with an error on line 5. A semicolon was left off of the end of line 4. This is once again a compiler syntax error. It is important to remember that if the compiler fails dues to an omitted semicolon, the error is on the previous token. This means that in almost all cases the actual error is on the previous line.
Syntax errors are only one type of compiler error. While syntax deals with actual structure (punctuation, etc), semantics deal with the meaning of what is written. Consider the following code:
program Workspace;
var
Num : Integer;
begin
Num := '12';
end.
Since Object Pascal is a strongly typed language, it is semantically nonsensical to assign a string expression to an integer variable. This is similar to a sentence in English that reads "The car swam across the lake." Syntactically this sentence is correct because it has a noun for a subject, a verb expressing action, and a properly constructed prepositional phrase. Semantically, this sentence is obviously nonsensical since cars don't swim (for exceptions see Bond, James). If you try to compile the code above you will get a compiler error something like "Incompatible types." There are of course times that you would want to convert a string into a number, but we will deal with these situations later.
Aside from mismatched types, the other common semantic error is an undeclared identifier. Unlike most languages, Object Pascal is not case sensitive. This means that the identifiers Ident and ident are considered the same. For consistency, if you capitalize an identifier when it is declared you should also capitalize it when you use it. If you misspell the identifier, however, the compiler will certainly complain.
Excercise 2-10. Identify the compiler errors in the following code (feel free to compile them to find the errors but also try to find them first without any help).
{$APPTYPE CONSOLE}
program Workspace
var
X,Y,Z : Integer;
Str : String;
begin
Str := test;
X := 3; Y := 5; Z := 7;
Writeln((12*X+3Y*Z)/(2+Z);
end;
There are 5 syntax errors in this program. Line 1 needs to be terminated with a semicolon. On line 6, test was meant to be a string so it should be enclosed by single quotes. As it is written, the compiler thinks it is an identifier. To correct this error it needs single quotes around it. On line 8 there are two errors. The first is in the subexpression 3Y*Z. The Object Pascal compiler does not treat math as you might expect from algebra class. 3Y is not valid because every factor in an expression must have a valid operator between it. 3*Y*Z is valid syntax. Also on this line, there needs to be an extra right parenthesis after 2+Z in order for everything to be balanced. Finally, on the last line there should be a period instead of a semicolon after end.
{$APPTYPE CONSOLE}
program Workspace;
var
X,Y,Str : Srting;
Begin
Str := 'test';
X := '3'; Y := '5; Z := '7';
Writeln(12*X+Y+Z);
end.
There is 1 syntax error and 3 semantic errors in this program. The syntax error is on Line 6 where Y := '5 contains an unterminated string. Note however, that the compiler thought '5; Z := ' was the string and so 7 is the token where the error occurrs. The rest are semantic errors. On Line 3, Srting is an undeclared identifier because it should have been spelled String. Line 4 is correct because Object Pascal is not case sensitive so Begin and begin are the same. On Line 5, Z is an undeclared identifier because it simply was not declared. On Line 7, 12*X is nonsensical because one cannot multiply a number by a string in Object Pascal even if the string represents a number like '3' (remember that the compiler interprets strings literally as the character '3' rather than the number 3). If you remove the 12* part you will see that the program compiles successfully. This is because it is valid to concatenate strings using the + operator.
Although we describe syntax and semantic compiler errors here, it does not actually matter in practice for you to be able to determine which is which (you will be able to do this automatically with experience later on). All that really matters is that you are able to correct a compiler error when one occurs.