Slide 1

Slide 1 text

jgs Compilers Lecture 14: Error Handling II Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

jgs Error Handling Version 2

Slide 3

Slide 3 text

Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Error Handling a) Panic One error, then Stop a) Simple Either ignore or increase the current token. a) First and Follow Each rule is responsible for validating the start and end.

Slide 4

Slide 4 text

Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Error Recovery At this point, Error recovery is about ignoring tokens. § How do we know which tokens should be ignored and how many?

Slide 5

Slide 5 text

Dr. Javier Gonzalez-Sanchez | Compilers | 5 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 6

Slide 6 text

jgs Calculating the First Set

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Calculate the FIRST set 1.FIRST(X) = {X} if X is a terminal 2. FIRST(ε) = {ε}. note that this is not covered by the first rule because ε is not a terminal. 3.If A → Xα, add FIRST(X) − {ε} to FIRST(A) 4.If A → A1 A2 A3 ...Ai Ai+1 ... Ak and ε ∈ FIRST (A1 ) and ε ∈ FIRST (A2 ) and . . . and ε ∈ FIRST (Ai ), then add FIRST (Ai+1 ) − {ε} to FIRST (A). 5.If A → A1 A2 A3 ...Ak and ε ∈ FIRST(A1 ) and ε ∈ FIRST(A2 ) and... and ε ∈ FIRST(Ak ), then add ε to FIRST(A).

Slide 9

Slide 9 text

Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs FIRST set Definition FIRST (a) is the set of tokens that can begin the construction a. Example → {+ } {* } → - | → integer FIRST(E) = {-, integer} FIRST(A) = {-, integer} FIRST(B) = {-, integer} FIRST(C) = {integer}

Slide 10

Slide 10 text

Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs FIRST set Define FIRST (BODY) FIRST(BODY) = FIRST (PRINT) U FIRST (ASSIGNMENT) U FIRST(VARIABLE) U FIRST(WHILE) U FIRST(IF) U FIRST(RETURN)

Slide 11

Slide 11 text

Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs FIRST set Define FIRST (C)

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs FIRST set Define FIRST (A) Define FIRST (B)

Slide 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs FIRST set → → d → a → ab → ε → c → d → e → → f → ε

Slide 14

Slide 14 text

Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs FIRST set S → ABC S → F A → EFd A → a B → aBb B → ε C → cC C → d E → eE E → F F → Ff F → ε rule FIRST set - evolution S ø {a, ε} {a, ε, e, f} {a, ε, e, f, d} A ø {a} {a, e} {a, e, f, d} B ø {a, ε} C ø {c, d} E ø {e} {e, ε} {e, ε, f} F ø {ε} {ε, f}

Slide 15

Slide 15 text

Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs FIRST set | Exercise → | a | b → | c d e | c d e → one → two → three

Slide 18

Slide 18 text

Dr. Javier Gonzalez-Sanchez | Compilers | 50 jgs Homework Working on your Parser (final version)

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2024 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.