Slide 1

Slide 1 text

jgs Compilers Lecture 19: Error Handling – Connecting the Dots Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

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)

Slide 3

Slide 3 text

jgs Test Yourselves Calculate FIRST set

Slide 4

Slide 4 text

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"] }

Slide 5

Slide 5 text

Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Case Study | A Grammar for JSON Object Pair Array Value

Slide 6

Slide 6 text

Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Calculate the FOLLOW set R2 R2 R2 R2

Slide 7

Slide 7 text

Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Calculate the FIRST set loop

Slide 8

Slide 8 text

jgs Prediction Rules

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

jgs Error Recovery

Slide 12

Slide 12 text

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) )

Slide 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Parser | Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or }

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Parser | Error Recovery VARIABLE Line N: expected identifier

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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)

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Parser | Error Recovery C Line N: expected value, identifier or ( Line N: expected )

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Homework Working on your Parser V2

Slide 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Questions

Slide 25

Slide 25 text

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.