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)
Rule 2. For any optional part, no token that can begin the optional part should also be able to appear immediately after it. FIRST(RULE) != FOLLOW(RULE) BODY PROGRAM
When parsing A: - The parser expects the current token to be in FIRST(A). Remember FIRST (terminal) = {terminal} - If it’s not, the parser knows that a syntax error has occurred. Report it. To recover: - The parser skips tokens until it finds a token in FOLLOW(A). ( or the expected FIRST(A) )
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.