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

UP Lecture 11

UP Lecture 11

Compilers
Parser II
(202402)

Javier Gonzalez-Sanchez

December 14, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Programming Assignment

    2 Level 3 The complete grammar for our language
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs KEYWORD {return,

    print} STRING Language Actions Expressions (operators) Instructions Control Structures ARITHMETIC OPERATORS { +, -, *, /, =} LOGIC OPERATORS { &, |, ! } RELATIONAL OPERATORS {<, >, ==, !=} KEYWORD { if, else, while, switch, case } Data INTEGER FLOAT HEXADECIMAL CHAR KEYWORD { void, int, char, string, float, boolean } KEYWORD { true, false } BINARY Delimiter : ; , ( ) { } [ ] Assignment 2 | Language OCTAL IDENTIFIER
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Grammar <PROGRAM>

    à '{' <BODY> '}’ <BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'} <ASSIGNMENT> à identifier '=' <EXPRESSION> <VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier <WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM> <IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>] <RETURN> à 'return' <PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’ <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer | octal | hexadecimal | binary | true | false | string | char | float | identifier|'(' <EXPRESSION> ')'
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Grammar <PROGRAM>

    à '{' <BODY> '}’ <BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'} <ASSIGNMENT> à identifier '=' <EXPRESSION> <VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier <WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM> <IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>] <RETURN> à 'return' <PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’ <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer | octal | hexadecimal | binary | true | false | string | char | float | identifier|'(' <EXPRESSION> ')'
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Assignment 2

    | Code 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 | 10 jgs Assignment 2

    | Code public static void RULE_BODY() { while (!tokens.get(currentToken).getWord().equals(“}”)) { if (tokens.get(currentToken).getToken().equals(“identifier”)) { RULE_ASSIGNMENT(); if (tokens.get(currentToken).getWord().equals(“;”)) { currentToken++; else error(3); } else if (tokens.get(currentToken).getToken().equals(“int”) | ...) { RULE_VARIABLE(); if (tokens.get(currentToken).getWord().equals(";")) { currentToken++; else error(3); } else if (tokens.get(currentToken).getWord().equals(“while”)) { RULE_WHILE(); } else if (tokens.get(currentToken).getWord().equals(“if”)) { RULE_IF(); } else if (tokens.get(currentToken).getWord().equals(“return”)) { RULE_RETURN(); if (tokens.get(currentToken).getWord().equals(“;”)) { currentToken++; else error(3); } else error(4); } } BODY
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Assignment 2

    | Code public static void RULE_ASSIGNMENT() { } ASSIGNMENT
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Assignment 2

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

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

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

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

    | Code public static void RULE_PRINT () { } PRINT
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Assignment 2

    | Code public static void RULE_C() { } C
  14. 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.