Error Recovery Rule FIRST set FOLLOW set PROGRAM { EOF BODY FIRST (PRINT) U FIRST (ASIGNMENT) U FIRST(VARIABLE) U FIRST (WHILE) U FIRST(IF) U FIRST (RETURN) } PRINT print ; ASSIGNMENT identifier ; VARIABLE int, float, boolean, void, char, string ; WHILE while } U FIRST(BODY) IF if } U FIRST(BODY) RETURN return ; EXPRESSION FIRST(X) ), ; X FIRST(Y) | U FOLLOW(EXPRESSION) Y ! U FIRST(R) & U FOLLOW(X) R FIRST(E) FOLLOW(Y) E FIRST (A) !=, ==, >, < U FOLLOW(R) A FIRST (B) -, + U FOLLOW(E) B - U FIRST (C) *, /, U FOLLOW(A) C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
1 It should always be possible to choose among several alternatives in a grammar rule, i.e., To implement a single-symbol look-ahead predictive parser, the FIRST sets of any two choices in one rule must not have tokens in common. i.e., FIRST(R1 ) FIRST(R2 ) FIRST(R3 )... FIRST(Rn ) = Ø
Complete your Grammar 1. variable declaration with initialization 2. Global/Local 3. do/while 4. for loop 5. switch-case 6. method declaration 7. with parameters 8. return values 9. read from the keyboard (we have print)
Implement Full Error Recovery § It is time to complete that error() method and ”recover from errors.” § Recover from an error: make the current token point to an expected token. In summary, it will be as follows: § A token in the FIRST set of the rule to be called next § A token in the FOLLOW set on the rule called before Details are in the following slides.
Error Recovery Rule FIRST set FOLLOW set PROGRAM { EOF BODY FIRST (PRINT) U FIRST (ASIGNMENT) U FIRST(VARIABLE) U FIRST (WHILE) U FIRST(IF) U FIRST (RETURN) } PRINT print ; ASSIGNMENT identifier ; VARIABLE int, float, boolean, void, char, string ; WHILE while } U FIRST(BODY) IF if } U FIRST(BODY) RETURN return ; EXPRESSION FIRST(X) ), ; X FIRST(Y) | U FOLLOW(EXPRESSION) Y ! U FIRST(R) & U FOLLOW(X) R FIRST(E) FOLLOW(Y) E FIRST (A) !=, ==, >, < U FOLLOW(R) A FIRST (B) -, + U FOLLOW(E) B - U FIRST (C) *, /, U FOLLOW(A) C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
{ x = a; x = 1 + ( x = (y;) if (a < b + ) {} else {} if (a < b) { if (a < b) { } else { } Input: We will not allow multi- line expressions; lines 3 and 4 should not be considered as follows: x= 1 + ( x = ( y;)
Line 3: expected value, identifier, ( Line 3: expected ) Line 3: expected ; // move to the next line Line 4: expected ) Line 4: expected identifier or keyword // infinite loop or end Line 5: expected value, identifier, ( // simple Line 12: expected } // reported by program Output:
At this point: § Errors do not increment currentToken. § currentToken increase when the token is used (added to the tree). § Error recovery is about ignoring tokens § How do we know which tokens should be ignored?
Error Recovery WHILE Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM)
Error Recovery IF Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM)
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.