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.
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)
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).
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}
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}
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.