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

UP Lecture 16

UP Lecture 16

Compilers
First Set
(202603)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

December 19, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs First Attempt

    public void error () { System.out.println(”error”); System.exit(); }
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Second Attempt

    private void error(int error) throws Exception { throw new Exception( "Error " + error + " at word " + tokens.get(currentToken).getValue() ); }
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs 4 •(one

    error and stop) •PANIC MODE 5 •Implement error synchronization •ERROR RECOVERY What Next
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Error Recovery

    Error recovery is about ignoring tokens. ▪ How do we know which tokens should be ignored and how many? RULE What should be in place for me to enter? What should be in place for me to leave?
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Error Recovery

    What should be in place for me to enter? What should be in place for me to leave? T NT T NT What should be in place for me to enter?
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 8 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)
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 10 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}
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 13 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)
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 16 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> → ε
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 17 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}
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 19 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
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 20 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}
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 21 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}
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Next What

    about the FIST set for the rules in our Language?
  15. 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.