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

UP Lecture 18

UP Lecture 18

Compilers
Semantic Analysis I
(202404)

Javier Gonzalez-Sanchez

December 21, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    Understanding the meaning i.e., Interpreting expressions in their context
  2. 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.
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Case 1:

    int i; char j; int m; void method(int n, char c) { int n; short l; i = j; i = m; } Case 2: int i, j; void method() { int i = 5; int j = i + i; int i = i + i; } Case 3: int i, m, k; boolean j; void main() { if (i>5) { ++i; } while (i + 1) { ++i; } do {++i; } while (i); for (i = 0; m; ++i) { k++; } } Case 4: int a; int b; int c, d; char c1, c2; int test1(int x, int y) { return x+y; } void main() { int i; i = a++; i = test1(a, b); i = test1(c1, c2); i = test1(a, c1); } } Case 5: int i, m; boolean j; public void main() { int m; int a[]; a = new int[j]; } Case 6: int i; void main(int m) { i++; return i; } Study Cases
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 6 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
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 7 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
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Symbol Table

    names bindings i j m method-int-char n l method k method-int int global int method char global int method int global void function void function int function int method-int-char short method-int-char int global int Method-int
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Exercise int

    a, b; char c, d; float e,f; void foo1(int a) { // float a = 1; float w = a; } void foo2(char b) { int a = c + d; } int foo3() { int i = a + b; } Create the symbol table
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Start thinking

    about this Programming Assignment Level 1 How to Reviewing Declaration and Unicity
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 13 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> ')'
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 14 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); } . . .
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs A3 ::

    SemanticAnalyzer.java public class SemanticAnalizer { private Hashtable<String, Vector<SymbolTableItem>> symbolTable; public static void CheckVariable(string type, string id) { // A. Search the id in the symbol table // B. if !exist then insert type, scope=global, value={0, false, "", ’’} // C. else error(1): “variable id is already defined.” }
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Extra If

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