• Keine Ergebnisse gefunden

SYNTACTICAL ERROR DIAGNOSTICS

Im Dokument L THURN AU • JOHNSON • HAM (Seite 129-132)

rt rue ~I rtrue~f +

CHAPTER 9 - MORE ABOUT PROCEDURES AND BLOCKS

10.2 SYNTACTICAL ERROR DIAGNOSTICS

An ALGOL program must be free of syntactical errors before it can be successfully translated into machine instructions by the com-piler. Since errors are not entirely avoidable, the compiler is desigped to supply messages concerning any constructs that it is unable to translate.

Error messages are given in the form of an error code and a reference to that entity which appears to be incorrect. For example, an identifier which was not declared or which is misspelled is meaningless to the compiler. There is noway to correctly translate a statement in which such an identifier occurs. Note that a mis-spelled reserved word must be assumed to be an identifier (which may or may not have been declared). Error messages pointing out undeclared identifiers occur rather frequently.

In general, any violation of the syntax rules of ALGOL will cause diagnostic error messages to be given. The message will reference

that entity in the program which gave the first indication of a syntax violation. Often the actual error will be found at some distance to

the left of that entity (considering the program as one long string that is read from left to right).

For example, the syntactical inconsistency of To-ABS ((l-J/(l+J)+8XT)+ A*2-COS(PHI);

is not detectable until the compiler encounters the semicolon. It then refers to the semicolon and provides the error code for the message

MISSING RIGHT PARENTHESIS.

It is unable to determine that the parenthesis was omitted between the J and the slash, since one could legally occur in several places.

In some cases the message can only tell what seems to be syn-tactically wrong, since the actual error may not be synsyn-tactically incorrect in itself. For example, suppose I- were punched for IM. Then,

FOR I - 1 STEP 1 UNTIL I- DO etc.

results in referencing the DO and the message

PRIMARY MAY NOT BEGIN WITH A QUANTITY OF THIS TYPE.

Here is another place where a knowledge of ALGOL syntax is vital to the programmer. When the cause of an error message is not immediately obvious, the programmer may need to use the syntax to deduce the cause. Since he can regard the compiler as a perfect embodiment of the syntax rules, he can work backward from the diagnostics it provides.

lt can happen that, after encountering one or more errors, the compiler will appear to become confused and begin emitting spurious error diagnostics. These need not be troublesome if the programmer proceeds as follows:

1. Find and correct the error that caused the first message. fhis message is never spurious.

2. Attempt to find the error that caused the next message. Correct any error that is found. If one is not readily found, pass on to

step 3.

3. Repeat step 2 for all succeeding error messages.

4. Even if some of the indicated errors below the first one are not identified, compile the program again and repeat these steps until all syntax errors are corrected.

129

There is a good reason for following these steps, rather than insisting on finding and correcting all indicated errors. This has to do with the manner in which the compiler processes the pro-gram. The program string is scanned from left to right, and every basic element of the language is immediately translated if it fits correctly into the syntax. At the first violation of syntax, an error message describing the violation is issued. Depending on the type of violation, the compiler may be forced to skip ahead to the next semicolon, where a new statement or declaration begins, in order to restart its analysis of the program string. Such a skip may cause a syntactically vital program element (such as a BEGIN or an END, or the latter portion of a declaration) to be ignored. This precipitates the same series of error messages that would appear, if the skipped portion of the program were inadvertently omitted by the program-mer. Thus, some of the errordiagnostics afterthe first one may be spurious in that they can be a consequence of a previous error. In most cases, spurious messages do not persist beyond the first or second attempt to compile an ALGOL program.

A final comment is in order on the role of the semicolon in ALGOL.

The presence of an extraneous semicolon can cause syntactical or logical errors that are sometimes obscure unless the rules of syntax are carefully applied. Recall that the semicolon is classed as a separator which is used to separate declarations from declara-tions, declarations from statements, and statements from state-ments. Recall further the existence of the dummy statement, which is nothing but empty space. Often an extraneous semicolon creates an unwanted dummy statement.

The following illustrations will complete this discussion.

FOR I+- 0 STEP 1 UNTIL 40 DO; A [Il .... 1

There is no syntactical error here, but, at run time, there would be 41 executions of the dummy statement between the DO and the semicolon. The assignment statement would be executed only once.

BEGIN;D;D;S;S END

Here there is a syntactical error, namely, that of a statement preceding a declaration in a block. The offending statement is the dummy statement between the BEGIN and the semicolon.

BEGIN D;;D;S;S END

This extra semicolon causes the same syntactical error as in the previous case.

IF BE THEN BEGIN S;S END; ELSE S

The semicolon after the END is syntactically wrong because it causes ELSE to appear as the beginning of an illegal statement.

Im Dokument L THURN AU • JOHNSON • HAM (Seite 129-132)