Slide 1

Slide 1 text

jgs Compilers Lecture 15: Error Handling III 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 Follow Set

Slide 7

Slide 7 text

Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs FOLLOW set Define FOLLOW (BODY) FIRST(BODY) = }

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Calculate the FOLLOW set 1.First put $ (the end of input marker) in Follow(S) (S is the start symbol) 2.If there is a production A → aBb, (where a can be a whole string) then everything in FIRST(b) except for ε is placed in FOLLOW(B). (apply the rule 4 in calculate FIRST set) 3.If there is a production A → aB, then add FOLLOW(A) to FOLLOW(B) 4.If there is a production A → aBb, where FIRST(b) contains ε, then add FOLLOW(A) to FOLLOW(B)

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Another Example → {+} → {*} → () | integer FIRST (E) = {(, integer} FIRST (T) = {(, integer} FIRST (F) = {(, integer} FOLLOW(E) = {$, )} FOLLOW(T) = {$, ),+ } FOLLOW(F) = {$, ),+, * }

Slide 13

Slide 13 text

jgs Prediction Rules

Slide 14

Slide 14 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 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 15

Slide 15 text

Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Prediction Rules Rule 1.1 The FIRST sets of any two choices in one rule must not have tokens in common in order to implement a single-symbol look ahead predictive parser.

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Prediction Rules Rule 2. For any optional part, no token beginning the optional part can also come after the optional part. FIRST(RULE) != FOLLOW(RULE) BODY PROGRAM

Slide 17

Slide 17 text

jgs Rules

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Parser | Error Recovery Line N: expected ; Line N: expected identifier or keyword currentToken++; Searching for FIRST(BODY) or FOLLOW(BODY) BODY

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Dr. Javier Gonzalez-Sanchez | Compilers | 25 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 23

Slide 23 text

Dr. Javier Gonzalez-Sanchez | Compilers | 26 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 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Parser | Error Recovery RETURN

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Dr. Javier Gonzalez-Sanchez | Compilers | 31 jgs Error Recovery PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B

Slide 29

Slide 29 text

Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Homework Submit Parser 1 Submit Parser 2 Grading Work on Parser 3

Slide 30

Slide 30 text

Dr. Javier Gonzalez-Sanchez | Compilers | 33 jgs Homework Let us do an Exam Lexical + Syntax When is a good date?

Slide 31

Slide 31 text

jgs Test Yourselves

Slide 32

Slide 32 text

Dr. Javier Gonzalez-Sanchez | Compilers | 35 jgs Error Recovery { x = a; x = 1 + ( x = (y;) if (a < b + ) {} else {} if (a < b) { if (a < b) { } else { } } Input: We will not allow multi- line expressions; line 3 and 4 should not be considered as follow: x= 1 + ( x = ( y;)

Slide 33

Slide 33 text

Dr. Javier Gonzalez-Sanchez | Compilers | 36 jgs Error Recovery Line 3: expected value, identifier, ( Line 3: expected ) Line 3: expected ; // move to the next line Line 4: expected ) Line 4: expected identifier or keyword // infinite loop or end Line 5: expected value, identifier, ( // simple Line 12: expected } // reported by program Output:

Slide 34

Slide 34 text

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

Slide 35

Slide 35 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.