Share
Embed
UP Lecture 17
Compilers
Error Handling (FOLLOW set)
(202603)
Javier Gonzalez-Sanchez PRO
December 20, 2023
Transcript
30 slides
-
jgs Compilers Lecture 17: Error Handling – Follow Set Dr.
Javier Gonzalez-Sanchez [email protected] -
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) -
Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs FIRST set
| Exercise <X> → <A> <X> → <A> a <A> → <B> <A> → <B> b <B> → <C><D><E> <B> → c d e <B> → <C> c <D> d <E> e <C> → one <D> → two <E> → three OPTION 1: FIRST(X) = {c, one} FIRST(A) = {c, one} FIRST(B) = {c, one} FIRST(C) = {one} FIRST(D) = {two} FIRST(E) = {three} OPTION 2: FIRST(X) = {c, one, ε} FIRST(A) = {b, c, one} FIRST(B) = {c, one} FIRST(C) = {one} FIRST(D) = {two} FIRST(E) = {three} -
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"] } -
Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Case Study
| A Grammar for JSON Object Pair Array Value -
Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs FOLLOW set
Definition FOLLOW (a) is the set of tokens that can follow the construction a. Example <E> → <A> {+ <A>} <A> → <B> {* <B>} <B> → <C> | <C> <C> → integer FOLLOW(E) = {$} // $ represents end of input, i.e., EOF FOLLOW(A) = {+, $} FOLLOW(B) = {*, +, $} FOLLOW(C) = {*, +, $} -
Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Calculate the
FOLLOW set R2 R2 R2 R2 -
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) -
Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs FOLLOW set
Define FOLLOW (BODY) FIRST(BODY) = } -
Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs FOLLOW set
Define FOLLOW (C) -
Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Another Example
<E> → <T> {+<T>} <T> → <F> {*<F>} <F> → (<E>) | integer FIRST (E) = {(, integer} FIRST (T) = {(, integer} FIRST (F) = {(, integer} FOLLOW(E) = {$, )} FOLLOW(T) = {$, ),+ } FOLLOW(F) = {$, ),+, * } -
Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs FOLLOW set
<S> → <A><B><C> <S> → <F> <A> → <E><F>d <A> → a <B> → a<B>b <B> → ε <C> → c<C> <C> → d <E> → e<E> <E> → <F> <F> → <F>f <F> → ε -
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} -
Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Our Grammar
2. METHODS 3. BODY -
Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Our Grammar
4. TYPE Add spanish! -
Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Our Grammar
5. PARAMS 6. ASSIGNMENT 7. EXPRESSION -
Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Our Grammar
9. Y 10. R 8. X What about >= or <= ? -
Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Our Grammar
12. A 13. B 11. E -
Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Our Grammar
14. C What about call a method and use the returned value? -
Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Our Grammar
15. VARIABLE 16. WHILE 17. IF -
Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Our Grammar
19, CALL_METHOD 20. PARAM_VALUES 18. RETURN -
Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Our Grammar
22. FOR 23. SWITCH 21. DOWHILE -
Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Homework Calculate
FIRST and FOLLOW sets for all your RULES! -
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.