Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 17

UP Lecture 17

Compilers
Error Handling (FOLLOW set)
(202603)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

December 20, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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)
  2. 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}
  3. 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"] }
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Case Study

    | A Grammar for JSON Object Pair Array Value
  5. 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) = {*, +, $}
  6. 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)
  7. 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) = {$, ),+, * }
  8. 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> → ε
  9. 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}
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Our Grammar

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

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

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

    19, CALL_METHOD 20. PARAM_VALUES 18. RETURN
  14. 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.