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

UP Lecture 15

UP Lecture 15

Compilers
Parser II
(202503)

Javier Gonzalez-Sanchez

December 18, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Parser public

    static void RULE_PROGRAM() { if (tokens.get(currentToken).getWord().equals(“{”)) { currentToken++; else error(1); RULE_BODY(); if (tokens.get(currentToken).getWord().equals(“}”)) currentToken++; else error(2); } PROGRAM
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Assignment 2

    | Code public static void RULE_ASSIGNMENT() { } ASSIGNMENT Program this one
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Parser public

    static void RULE_BODY() { while (!tokens.get(currentToken).getWord().equals(“}”)) { RULE_EXPRESSION(); if (tokens.get(currentToken).getWord().equals(“;”)) currentToken++; else error(3); } } BODY
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Parser public

    static void RULE_EXPRESSION() { RULE_X(); while (tokens.get(currentToken).getWord().equals(“|”)) { currentToken++; RULE_X(); } } EXPRESSION
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Parser public

    static void RULE_X() { RULE_Y(); while (tokens.get(currentToken).getWord().equals(“&”)) { currentToken++; RULE_Y(); } } X
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Parser public

    static void RULE_Y() { if (tokens.get(currentToken).getWord().equals(“!”)) { currentToken++; } RULE_R(); } Y
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Parser public

    static void RULE_R() { RULE_E(); while ( tokens.get(currentToken).getWord().equals(“<”) |tokens.get(currentToken).getWord().equals(“>”) |tokens.get(currentToken).getWord().equals(“==”) |tokens.get(currentToken).getWord().equals(“!=”) ) { currentToken++; RULE_E(); } } R
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Parser public

    static void RULE_E() { RULE_A(); while (tokens.get(currentToken).getWord().equals(“-”) | tokens.get(currentToken).getWord().equals(“+”) ) { currentToken++; RULE_A(); } } E
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Parser public

    static void RULE_A() { RULE_B(); while (tokens.get(currentToken).getWord().equals(“/”) | tokens.get(currentToken).getWord().equals(“*”) ) { currentToken++; RULE_B(); } } A
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 30 jgs Parser public

    static void RULE_B() { if (tokens.get(currentToken).getWord().equals(“-”)) { currentToken++; } RULE_C(); } B
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 31 jgs Parser public

    static void RULE_C() { if (tokens.get(currentToken).getToken().equals(“integer”)) { currentToken++; } else if (tokens.get(currentToken).getToken().equals(“identifier”)) { currentToken++; } else if (tokens.get(currentToken).getWord().equals(“(”)) { currentToken++; RULE_EXPRESSION(); if (tokens.get(currentToken).getWord().equals(“)”)) { currentToken++; } else error(4); } } else { error (5); } } C
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Assignment 2

    | Code public static void RULE_C() { } C
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 33 jgs Assignment 2

    | Code public static void RULE_VARIABLE() { } VARIABLE
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 34 jgs Assignment 2

    | Code public static void RULE_WHILE() { } WHILE
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 35 jgs Assignment 2

    | Code public static void RULE_IF() { } IF
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 36 jgs Assignment 2

    | Code public static void RULE_RETURN() { } RETURN
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 37 jgs Assignment 2

    | Code public static void RULE_CALL_METHOD () { } CALL_METHOD
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 38 jgs Assignment 2

    | Code public static void RULE_PARAM_VALUES () { } PARAM_VALUES
  19. 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.