Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 19

UP Lecture 19

Compilers
Error Handling - Connecting the Dots
(202504)

Javier Gonzalez-Sanchez

December 22, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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)
  2. 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"] }
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Case Study

    | A Grammar for JSON Object Pair Array Value
  4. 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
  5. 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
  6. 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) )
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Parser |

    Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or }
  8. 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
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Parser |

    Error Recovery ASSIGNMENT Line N: expected = currentToken++; Searching for FIRST(EXPRESSION) or FOLLOW(EXPRESSION)
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Parser |

    Error Recovery VARIABLE Line N: expected identifier
  11. 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)
  12. 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)
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Parser |

    Error Recovery PRINT Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or )
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Parser |

    Error Recovery EXPRESSION X Y R E A B
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Parser |

    Error Recovery C Line N: expected value, identifier or ( Line N: expected )
  16. 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
  17. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2025 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.