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

UP Lecture 18

UP Lecture 18

Compilers
Error Recovery - Follow Set
(202503)

Javier Gonzalez-Sanchez

December 21, 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

    <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> → ε
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 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}
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 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) = {*, +, $}
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 9 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)
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 12 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> → ε
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 13 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}
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 14 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) = {$, ),+, * }
  9. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2025 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.