Share
Embed
UP Lecture 18
Compilers
Error Recovery - Connecting the Dots
(202603)
Javier Gonzalez-Sanchez PRO
December 21, 2023
Transcript
25 slides
-
jgs Compilers Lecture 18: Error Handling – Connecting the Dots
Dr. Javier Gonzalez-Sanchez [email protected] -
Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs Parser |
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) -
Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Case Study
| A Grammar for JSON { "name": "Alice", "age": 30, "address": { "city": "New York", "zip": "10001" }, "phones": ["+1-800-555-1234", "+1-800-555-5678"] } -
Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Case Study
| A Grammar for JSON Object Pair Array Value -
Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Calculate the
FOLLOW set R2 R2 R2 R2 -
Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Prediction Rules
Rule 1. It should always be possible to choose among several alternatives in a grammar rule. FIRST(R1 ) FIRST(R2 ) FIRST(R3 )... FIRST(Rn ) = Ø BODY -
Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Prediction Rules
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 -
Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Error Recovery
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) ) -
Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Parser |
Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or } -
Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Parser |
Error Recovery Line N: expected ; Line N: expected identifier or keyword currentToken++; Searching for FIRST(BODY) or FOLLOW(BODY) BODY -
Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Parser |
Error Recovery ASSIGNMENT Line N: expected = currentToken++; Searching for FIRST(EXPRESSION) or FOLLOW(EXPRESSION) -
Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Parser |
Error Recovery VARIABLE Line N: expected identifier -
Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Parser |
Error Recovery WHILE Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM) -
Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Parser |
Error Recovery IF Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM) -
Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Parser |
Error Recovery PRINT Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) -
Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Parser |
Error Recovery EXPRESSION X Y R E A B -
Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Parser |
Error Recovery C Line N: expected value, identifier or ( Line N: expected ) -
Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Error Recovery
PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B -
Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Homework Working
on your Parser V 2.0 -
jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2026 Copyright. These
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.