Slide 1

Slide 1 text

jgs Compilers Lecture 18: Semantic Analysis Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs Semantic Analysis Understanding the meaning i.e., Interpreting expressions in their context

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs 1. Variable Declaration and Unicity

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Start thinking about this Programming Assignment Level 1 How to Reviewing Declaration and Unicity

Slide 11

Slide 11 text

Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Symbol Table

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Symbol Table

Slide 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Grammar à '{' '}’ à {';'|';'|';’|||';'} à identifier '=' à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier à 'while' '(' ')' à 'if' '(' ')' ['else' ] à 'return' à ’print’ ‘(‘ ‘)’ à {'|' } à {'&' } à ['!'] à {('>'|'<'|'=='|'!=') } à {(’+'|'-’) } à {('*'|'/') } à ['-'] à integer | octal | hexadecimal | binary | true | false | string | char | float | identifier|'(' ')'

Slide 14

Slide 14 text

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); } . . .

Slide 15

Slide 15 text

Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs A3 :: SemanticAnalyzer.java public class SemanticAnalizer { private Hashtable> 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.” }

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs A3 :: Review

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Questions

Slide 18

Slide 18 text

Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Homework Programming Final Version of Your Parser

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Extra If time allows it start with your Semantic Analyzer (more details/work is coming)

Slide 20

Slide 20 text

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.