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

UP Lecture 19

UP Lecture 19

Compilers
Semantic Analysis II
(202404)

Javier Gonzalez-Sanchez

December 22, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Semantic Analysis

    1. Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2. Types. Review that the types of variables match the values assigned to them. 3. Array’s indexes. Review that the indexes are integers. 4. Conditions. Review that all expressions on the conditions return a boolean value. 5. Return type. Review that the value returned by a method matches the type of the method. 6. Parameters. Review that the parameters in a method match the type and number with the declaration of the method.
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs A3 ::

    Parser Update VARIABLE void rule_variable( ) { . . . if (tokens.get(currentToken1).getType().equals(“identifier”)) { SemanticAnalizer.CheckVariable( tokens.get(currentToken-1).getWord(), tokens.get(currentToken).getWord() ); currentToken++; } else { error (6); } . . .
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Symbol Table

    int i; char j; int m; void method(int n, char c) { int n; short l; i = j; i = m; } void method() { int i = 5; int j = i + i; } int k; int method(int i) { if (i>5) { ++i; } while (i + 1) { ++i; } do {++i; } while (i); for (i = 0; m; ++i) { k++; } } name type scope value i int global j char global m int global method-int-char void function n int method-int-char l short method-int-char method void function i int method j int method k int global method-int int function i Int method-int
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Semantic Analysis

    1. Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2. Types. Review that the types of variables match the values assigned to them. 3. Array’s indexes. Review that the indexes are integers. 4. Conditions. Review that all expressions on the conditions return a boolean value. 5. Return type. Review that the value returned by a method matches the type of the method. 6. Parameters. Review that the parameters in a method match the type and number with the declaration of the method.
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Type matching

    | Example 1 int x, y, z; char p, q, r; float a, b, c; boolean foo; void method() { x = a * c + p; } x * + a c p =
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Type matching

    | Cube fill one sheet for each operator in the language cube
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Type matching

    | Cube OPERATOR int float char strin g boolea n void int float char string boolean void cube
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Type matching

    | Cube OPERATOR + int float char strin g boolea n void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Type matching

    | Cube OPERATOR & int float char strin g boolea n void int X X X X X X float X X X X X X char X X X X X X string X X X X X X boolean X X X X boolean X void X X X X X X cube
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Type matching

    | Cube OPERATOR < int float char string boolean void int boolea n boolea n X X X X float boolea n boolea n X X X X char X X X X X X string X X X X X X boolean X X X X X X void X X X X X X cube
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Type matching

    | Cube OPERATOR = int float char strin g boolea n void int OK X X X X X float OK OK X X X X char X X OK X X X string X X X OK X X boolean X X X X OK X void X X X X X OK cube
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Type matching

    | Cube OPERATOR - int float char strin g boolea n void int float X X X X cube (- unary) OPERATOR + int float char strin g boolea n void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube (- binary)
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Type matching

    | Example 1 int x, y, z; char p, q, r; float a, b, c; boolean foo; void method() { x = a * c + p; } x * + a c p =
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Type matching

    | Example 2 int a; int c (int b) { return b * 3 * 2 * 1 ; } void main () { a = 1; boolean a= c(14)/2 > 1; } cube for matching types symbol table stack
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Handwritten Notes

    / / Definition of the cube of types and operators int cube [][][]; public static int OP_PLUS = 1; public static int OP_MINUS = 2; public static int OP_MULT = 3; / / … public static int INTEGER = 1; public static int FLOAT = 2; / / … cube [OP_PLUS][INTEGER][INTEGER] = INTEGER; cube [OP_PLUS][FLOAT][INTEGER] = FLOAT; cube [OP_PLUS][INTEGER][FLOAT] = FLOAT;
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 20 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> ')'
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Error Recovery

    PROGRAM BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Assignment 2

    | Code C 1. / / except for the open parenthesis SemanticAnalizer.pushStack( tokens.get(currentToken).getToken() );
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Parser 1.

    Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “-” ); SemanticAnalizer.pushStack(result); }
  20. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Parser 1.

    Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
  21. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Parser 1.

    Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
  22. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Parser 1.

    Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
  23. Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Parser 1.

    Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “!” ); SemanticAnalizer.pushStack(result); }
  24. Dr. Javier Gonzalez-Sanchez | Compilers | 28 jgs Parser 1.

    Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag }
  25. Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Parser 1.

    Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag }
  26. Dr. Javier Gonzalez-Sanchez | Compilers | 30 jgs Assignment 2

    | Code String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “=” ); if (!result.equals(“OK”) { error(2); }
  27. Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Extra If

    time allows it start with your Semantic Analyzer (more details/work is coming)
  28. 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.