

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Week 7 Error Recovery: the steps performed by a compiler immediately after the dis covery of a syntax error.
Two goals: find errors as many as possible in a single compilation and report them once only.
The first approach simply reports error without consuming the error token. Procedure SyntaxError() Begin Error(“Syntax Error”); End;
Example: 1 program testError; 2 const 3 a := 1; //wrong token := 4 b = 2; 5 c = ; //missing numeral 6 d = 4; 7 type 8 S = record f, g: integer end; //missing begin 9 T = array [1..2] of integer; 10 var 11 x: integer; 12 begin 13 if = 2 then //missing name 14 x := 1 15 end.
Only the error in line 3 can be found and the parser terminates! The token “:=” makes terminated the procedures: ConstantDefinition, ConstantDefinitionPart, BlockBody and Programx.
The second approach tries to skip the unrecognized token. Procedure SyntaxError() Begin Error(“Syntax Error”); nextToken(); end;
Errors found are line 3 and line 5 but line 6 is reported incorrectly. In line 5, a constant is expected but “;” is found instead. The parser skips “;” and expects “;” but instead found name “d” in line 6! If a sequence of unrecognized tokens is long, the parser eventually terminates.
The third approach will be to skip zero or more symbols until the parser is certain to recognize tokens at some level. These tokens are called stop symbols. Let StopSymbols := Symbols[ Const1, Type1, Var1, Procedure1, Begin1, If1, While1]. Procedure SyntaxError() Begin Error(“Syntax Error”); While currToken not in StopSymbols do nextToken(): end;
Unfortunately, this approach is not working! The parser reaches EOF for the error in line 13 because there is no stop symbol later on. Errors are found in line 3, 8 and 13 but line 5 is ignored. Errors are also found incorrectly in line 7, 10 and 16.
The idea of skipping tokens is fine but the stop symbols should be locally defined!
Rule 1. For every BNF rule D = R, the parser defines a procedure of the same name: proc D(stop: symbols) begin a(R, stop); end;
where a(R, stop) checks if input tokens form a sentence described by expression R and check that the sentence is followed by a well-defined stop symbol. If a valid sentence is found, the parse reads out the whole sentence plus the stop symbol; otherwise a syntax error is reported and tokens are skipped until it reaches one of the stop symbols.
A parsing algorithm is sound if the assertion “symbol in stop” is true after the execution of the algorithm.
Rule 2. A sequence of sentences of the forms F1, F2, …, Fn is recognized by scanning the individual sentences one at a time in the order written: a(F1 F2 … Fn, stop) = a(F1, First(F2, …, Fn)+stop); a(F2, First(F3, …, Fn)+stop); … a(Fn, stop);
Rule 3. A single token is recognized by the procedure expect(s, stop).
Rule 4. To recognize a sentence described by a BNF rule named N the parser calls the corresponding procedure named N: a(N, stop) = N(stop)