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

UP Lecture 17

UP Lecture 17

Compilers
Full Parser
(202404)

Javier Gonzalez-Sanchez

December 20, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. 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)
  3. 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 ) = Ø
  4. 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)
  5. 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)
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Step 2

    Calculate FIRST and FOLLOW for the New Rules
  7. 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.
  8. 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
  9. 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)
  10. 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;)
  11. 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:
  12. 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?
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Parser |

    Error Recovery PROGRAM Line N: expected { Line N: expected } currentToken++; Searching for FIRST(BODY) or }
  14. 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
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Parser |

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

    Error Recovery VARIABLE Line N: expected identifier
  17. 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)
  18. 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)
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 31 jgs Parser |

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

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

    Error Recovery C Line N: expected value, identifier or ( Line N: expected )
  22. 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() }
  23. 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.