Slide 1

Slide 1 text

jgs Compilers Lecture 17: Error Handling – First Set Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

jgs Test Yourselves

Slide 3

Slide 3 text

Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Are there syntactical errors? class One { void m () { int x; float x; string x; char x; void x; boolean x; } }

Slide 4

Slide 4 text

Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Are there syntactical errors? class Two { void m (void x) { int x; x = 5; x = 05; x = 0x5ff; x = 5.55; x = "five"; x = '5'; x = false; } }

Slide 5

Slide 5 text

Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Are there syntactical errors? class Three{ void something () { x = "hello" + "world" – 'w' * 5 / 3.4; x = y – hello & 0xffff | 05; x = -7; x = !y; x = (cse340 + cse310) / cse101 ; } }

Slide 6

Slide 6 text

Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Are there syntactical errors? class Four { void something () { float a; x = 0; int x; y = 1 + 1; x = (0b11) +(05 – 0xFF34); while (2 == "hi") { a = 2 > (4 + Y); if (true) { if( 2 + 2 ) {} else {} } } print ("hello" + "world"); } }

Slide 7

Slide 7 text

Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Are there syntactical errors? class Five { void something () { if ( if ( if (x – 3) {} ) ) { print ("hello" + "world"); } if (); if (x > 5); ; :) } }

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs 1 •Understand de provided source code (3 rules) 2 •Program the rules PROGRAM, BODY, EXPRESSION, X, Y, R, E, C (11 rules) 3 •Program the full set of rules in the grammar (23 rules) 4 •(one error and stop) •PANIC MODE 5 •Implement error synchronization •ERROR RECOVERY What Next

Slide 11

Slide 11 text

jgs Error Recovery

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Error Recovery Error recovery is about ignoring tokens. ▪ How do we know which tokens should be ignored and how many?

Slide 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 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)

Slide 14

Slide 14 text

jgs Calculating the First Set

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Calculate the FIRST set loop

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs FIRST set PROGRAM FIRST(PROGRAM) = { "{"}

Slide 18

Slide 18 text

Dr. Javier Gonzalez-Sanchez | Compilers | 18 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)

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs FIRST set Define FIRST (C)

Slide 20

Slide 20 text

Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs FIRST set Define FIRST (A) Define FIRST (B)

Slide 21

Slide 21 text

Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs FIRST set → → d → a → ab → ε → c → d → e → → f → ε

Slide 22

Slide 22 text

Dr. Javier Gonzalez-Sanchez | Compilers | 22 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}

Slide 23

Slide 23 text

jgs Test Yourselves Calculate FIRST set

Slide 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs FIRST set | Exercise → | a | b → | c d e | c d e → one → two → three

Slide 27

Slide 27 text

Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Homework Working on your Parser

Slide 28

Slide 28 text

Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Questions

Slide 29

Slide 29 text

jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2025 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.