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

UP Lecture 15

UP Lecture 15

Compilers
Error Handling III
(202403)

Javier Gonzalez-Sanchez

December 18, 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 | 9 jgs FOLLOW set

    Define FOLLOW (BODY) FIRST(BODY) = }
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 11 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> → ε
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 12 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)
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 14 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 | 15 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. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Prediction Rules

    Rule 1. It should always be possible to choose among several alternatives in a grammar rule. FIRST(R1 ) FIRST(R2 ) FIRST(R3 )... FIRST(Rn ) = Ø BODY
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Prediction Rules

    Rule 1.1 The FIRST sets of any two choices in one rule must not have tokens in common in order to implement a single-symbol look ahead predictive parser.
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Prediction Rules

    Rule 2. For any optional part, no token beginning the optional part can also come after the optional part. FIRST(RULE) != FOLLOW(RULE) BODY PROGRAM
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Parser |

    Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or }
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Parser |

    Error Recovery Line N: expected ; Line N: expected identifier or keyword currentToken++; Searching for FIRST(BODY) or FOLLOW(BODY) BODY
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Parser |

    Error Recovery ASSIGNMENT Line N: expected = currentToken++; Searching for FIRST(EXPRESSION) or FOLLOW(EXPRESSION)
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Parser |

    Error Recovery VARIABLE Line N: expected identifier
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Parser |

    Error Recovery WHILE Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM)
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Parser |

    Error Recovery IF Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or ) currentToken++; Searching for FIRST(PROGRAM) or FOLLOW(PROGRAM)
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Parser |

    Error Recovery PRINT Line N: expected ( Line N: expected ) currentToken++; Searching for FIRST(EXPRESSION) or )
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Parser |

    Error Recovery EXPRESSION X Y R E A B
  20. Dr. Javier Gonzalez-Sanchez | Compilers | 30 jgs Parser |

    Error Recovery C Line N: expected value, identifier or ( Line N: expected )
  21. Dr. Javier Gonzalez-Sanchez | Compilers | 31 jgs Error Recovery

    PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B
  22. Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Homework Submit

    Parser 1 Submit Parser 2 Grading Work on Parser 3
  23. Dr. Javier Gonzalez-Sanchez | Compilers | 33 jgs Homework Let

    us do an Exam Lexical + Syntax When is a good date?
  24. Dr. Javier Gonzalez-Sanchez | Compilers | 35 jgs Error Recovery

    { x = a; x = 1 + ( x = (y;) if (a < b + ) {} else {} if (a < b) { if (a < b) { } else { } } Input: We will not allow multi- line expressions; line 3 and 4 should not be considered as follow: x= 1 + ( x = ( y;)
  25. Dr. Javier Gonzalez-Sanchez | Compilers | 36 jgs Error Recovery

    Line 3: expected value, identifier, ( Line 3: expected ) Line 3: expected ; // move to the next line Line 4: expected ) Line 4: expected identifier or keyword // infinite loop or end Line 5: expected value, identifier, ( // simple Line 12: expected } // reported by program Output:
  26. 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.