Slide 1

Slide 1 text

jgs Compilers Lecture 17: Our Parser Dr. Javier Gonzalez-Sanchez jgonzalezs@up.edu.mx

Slide 2

Slide 2 text

jgs Previously …

Slide 3

Slide 3 text

Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Error Recovery PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B

Slide 4

Slide 4 text

Dr. Javier Gonzalez-Sanchez | Compilers | 4 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 5

Slide 5 text

jgs Prediction Rules

Slide 6

Slide 6 text

Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Prediction Rule 1 It should always be possible to choose among several alternatives in a grammar rule, i.e., To implement a single-symbol look-ahead predictive parser, the FIRST sets of any two choices in one rule must not have tokens in common. i.e., FIRST(R1 ) FIRST(R2 ) FIRST(R3 )... FIRST(Rn ) = Ø

Slide 7

Slide 7 text

Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Prediction Rule 1 :: Example BODY

Slide 8

Slide 8 text

Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Prediction Rule 2 For any optional part, no token beginning the optional part can also come after the optional part. i.e., FIRST(RULE) != FOLLOW(RULE)

Slide 9

Slide 9 text

Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Prediction Rule 2 :: Example BODY PROGRAM

Slide 10

Slide 10 text

jgs Let’s Finish Our Parser Stage 1

Slide 11

Slide 11 text

Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Step 1 Complete your Grammar 1. variable declaration with initialization 2. Global/Local 3. do/while 4. for loop 5. switch-case 6. method declaration 7. with parameters 8. return values 9. read from the keyboard (we have print)

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Step 2 Calculate FIRST and FOLLOW for the New Rules

Slide 13

Slide 13 text

jgs Let’s Finish Our Parser Stage 2

Slide 14

Slide 14 text

Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Step 3 Program the New Rules

Slide 15

Slide 15 text

Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Step 3

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Bonus

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Step 4 Implement Full Error Recovery § It is time to complete that error() method and ”recover from errors.” § Recover from an error: make the current token point to an expected token. In summary, it will be as follows: § A token in the FIRST set of the rule to be called next § A token in the FOLLOW set on the rule called before Details are in the following slides.

Slide 18

Slide 18 text

jgs Error Recovery

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Error Recovery PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B

Slide 20

Slide 20 text

Dr. Javier Gonzalez-Sanchez | Compilers | 20 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 21

Slide 21 text

Dr. Javier Gonzalez-Sanchez | Compilers | 21 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; lines 3 and 4 should not be considered as follows: x= 1 + ( x = ( y;)

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Error Recovery At this point: § Errors do not increment currentToken. § currentToken increase when the token is used (added to the tree). § Error recovery is about ignoring tokens § How do we know which tokens should be ignored?

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Dr. Javier Gonzalez-Sanchez | Compilers | 30 jgs Parser | Error Recovery RETURN

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Dr. Javier Gonzalez-Sanchez | Compilers | 34 jgs Parser | Error Recovery If ( tokens.get(currentToken).getLine() < tokens.get(currentToken+1).getLine() ) { / / go back until reaching RULE_BODY() }

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Dr. Javier Gonzalez-Sanchez | Compilers | 36 jgs Homework Programming Final Version of Your Parser

Slide 37

Slide 37 text

jgs Compilers Javier Gonzalez-Sanchez, Ph.D. jgonzalezs@up.edu.mx 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.