Share
Embed
Transcript
25 slides
-
jgs Compilers Lecture 16: Error Handling – First Set Dr.
Javier Gonzalez-Sanchez [email protected] -
Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs First Attempt
public void error () { System.out.println(”error”); System.exit(); } -
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() ); } -
Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs 4 •(one
error and stop) •PANIC MODE 5 •Implement error synchronization •ERROR RECOVERY What Next -
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? -
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? -
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) -
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} -
Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs FIRST set
PROGRAM FIRST(PROGRAM) = { "{"} -
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) -
Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs FIRST set
Define FIRST (C) -
Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs FIRST set
Define FIRST (A) Define FIRST (B) -
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> → ε -
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} -
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 -
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} -
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} -
Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Next What
about the FIST set for the rules in our Language? -
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.