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

UP Lecture 14

UP Lecture 14

Compilers
Error Handling II
(202403)

Javier Gonzalez-Sanchez

December 17, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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.
  2. 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?
  3. 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)
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Calculate the

    FIRST set 1.FIRST(X) = {X} if X is a terminal 2. FIRST(ε) = {ε}. note that this is not covered by the first rule because ε is not a terminal. 3.If A → Xα, add FIRST(X) − {ε} to FIRST(A) 4.If A → A1 A2 A3 ...Ai Ai+1 ... Ak and ε ∈ FIRST (A1 ) and ε ∈ FIRST (A2 ) and . . . and ε ∈ FIRST (Ai ), then add FIRST (Ai+1 ) − {ε} to FIRST (A). 5.If A → A1 A2 A3 ...Ak and ε ∈ FIRST(A1 ) and ε ∈ FIRST(A2 ) and... and ε ∈ FIRST(Ak ), then add ε to FIRST(A).
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs FIRST set

    Definition FIRST (a) is the set of tokens that can begin the construction a. Example <E> → <A> {+ <A>} <A> → <B> {* <B>} <B> → -<C> | <C> <C> → integer FIRST(E) = {-, integer} FIRST(A) = {-, integer} FIRST(B) = {-, integer} FIRST(C) = {integer}
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs FIRST set

    Define FIRST (BODY) FIRST(BODY) = FIRST (PRINT) U FIRST (ASSIGNMENT) U FIRST(VARIABLE) U FIRST(WHILE) U FIRST(IF) U FIRST(RETURN)
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 13 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> → ε
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 14 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}
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs FIRST set

    | Exercise <X> → <A> | <A> a <A> → <B> | <B> b <B> → <C><D><E> | c d e | <C> c <D> d <E> e <C> → one <D> → two <E> → three
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 16 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}
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 17 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 <C> → ε <D> → two <D> → ε <E> → three FIRST(X) = {c, one, three, two} FIRST(A) = {c, one, three, two} FIRST(B) = {c, one, three, two} FIRST(C) = {one, ε} FIRST(D) = {two, ε} FIRST(E) = {three}
  12. 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.