Slide 1

Slide 1 text

jgs Compilers Lecture 17: Error Handling – Follow Set 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 5

Slide 5 text

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

Slide 6 text

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

Slide 7

Slide 7 text

jgs Calculating the Follow Set

Slide 8

Slide 8 text

Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs FOLLOW set Definition FOLLOW (a) is the set of tokens that can follow the construction a. Example → {+ } {* } → | → integer FOLLOW(E) = {$} // $ represents end of input, i.e., EOF FOLLOW(A) = {+, $} FOLLOW(B) = {*, +, $} FOLLOW(C) = {*, +, $}

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Dr. Javier Gonzalez-Sanchez | Compilers | 10 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, 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 11

Slide 11 text

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

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs FOLLOW set Define FOLLOW (C)

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16 text

jgs Second Project Our Language

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Our Grammar 1. PROGRAM

Slide 18

Slide 18 text

Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Our Grammar 2. METHODS 3. BODY

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Our Grammar 4. TYPE Add spanish!

Slide 20

Slide 20 text

Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Our Grammar 5. PARAMS 6. ASSIGNMENT 7. EXPRESSION

Slide 21

Slide 21 text

Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Our Grammar 9. Y 10. R 8. X What about >= or <= ?

Slide 22

Slide 22 text

Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Our Grammar 12. A 13. B 11. E

Slide 23

Slide 23 text

Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Our Grammar 14. C What about call a method and use the returned value?

Slide 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Our Grammar 15. VARIABLE 16. WHILE 17. IF

Slide 25

Slide 25 text

Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Our Grammar 19, CALL_METHOD 20. PARAM_VALUES 18. RETURN

Slide 26

Slide 26 text

Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Our Grammar 22. FOR 23. SWITCH 21. DOWHILE

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Homework Calculate FIRST and FOLLOW sets for all your RULES!

Slide 29

Slide 29 text

Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Homework Submit your Parser

Slide 30

Slide 30 text

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.