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

UP Lecture 14

UP Lecture 14

Compilers
Parser
(202503)

Javier Gonzalez-Sanchez

December 17, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Rules PROGRAM

    BODY C EXPRESSION X Y R E A B { 0; 1 + 2; 3 * (4 + hello); }
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Rules PROGRAM

    BODY C A B { 0; 1 + 2; 3 * (4 + hello); } EXPRESSION X Y R E
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Parser |

    Step by Step For each rule in the grammar { ▪ Step 1. The left-hand side (method declaration) ▪ Step 2. The right-hand side (body of the method) as follows: ▪ Step 2.1 Non-terminals (boxes) will be a call to a method ▪ Step 2.2. Terminals (ovals) will be if-conditions ▪ Step 2.3. Lines in a loop (EBNF items) will be loops }
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Parser public

    class Parser { private static Vector<Token> tokens; private static int currentToken; public static void RULE_PROGRAM () {} public static void RULE_BODY () {} public static void RULE_EXPRESSION () {} public static void RULE_X () {} public static void RULE_Y () {} public static void RULE_R () {} public static void RULE_E () {} public static void RULE_A () {} public static void RULE_B () {} public static void RULE_C () {}
  5. 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
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 17 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
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Parser public

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

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

    static void RULE_Y() { if (tokens.get(currentToken).getWord().equals(“!”)) { currentToken++; } RULE_R(); } Y
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 21 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
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Parser public

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

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

    static void RULE_B() { if (tokens.get(currentToken).getWord().equals(“-”)) { currentToken++; } RULE_C(); } B
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 25 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
  15. 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.
  16. 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.