Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously …

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 conditons return a boolean value. 5. Return type. Review that the value returned by a method match the type of the method. 6. Parameters. Review that the parameters in a method match in type and number with the declaration of the method.

Slide 4

Slide 4 text

Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Errors to be reported § Line : Variable not found § Line : Variable is already defined § Line : Incompatible types: type mismatch § Line : Incompatible types: expected boolean

Slide 5

Slide 5 text

Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Errata

Slide 6

Slide 6 text

Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Addition

Slide 7

Slide 7 text

Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Logic Operators

Slide 8

Slide 8 text

Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Test Case 1 1.{ 2.boolean boo; boolean foo; 3.int boo; int j; 4.int i; int minusfour; int k; 5.boolean k; 6.i = 4; 7.j = 3; 8.minusfour = boo; 9.while(i > 0) { 10. j = hello + 100 / 50; 11. while( j > 0) { 12. k = i*j; 13. print (k); 14. j = j-1; 15. } 16. i = i-1; 17.} 18.if (boo + minusfour) { 19. print (i); 20.} 21. 22.if (i < minusfour) { 23. print (j); 24.} 25.while (!i > minusfour) { 26. print (i); 27. i = i - 1; 28.} 29. 30.i = i + foo; 31.j = j + 1; 32.print (i); 33.print (j); 34.j = 100 > 90; 35.}

Slide 9

Slide 9 text

Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Test Case 1 1.Line 2: variable is already defined 2.Line 4: variable is already defined 3.Line 8: incompatible types: type mismatch 4.Line 10: variable not found 5.Line 10: incompatible types: type mismatch 6.Line 18: boolean expression expected 7.Line 30: incompatible types: type mismatch 8.Line 34: incompatible types: type mismatch

Slide 10

Slide 10 text

Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs 3. Conditions

Slide 11

Slide 11 text

Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Example { int a; int b; boolean c; a = 4; b = a + 1; if (a > b) { print (a); } } cube for matching types symbol table stack

Slide 12

Slide 12 text

Dr. Javier Gonzalez-Sanchez | Compilers | 12 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 13

Slide 13 text

Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Conditions WHILE String x = SemanticAnalizer.popStack(); if (!x.equals(“boolean”) { error(3); }

Slide 14

Slide 14 text

Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Conditions IF String x = SemanticAnalizer.popStack(); if (!x.equals(“boolean”) { error(3); }

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Assignment #3 public class SemanticAnalyzer { private static final Hashtable> symbolTable; private static final Stack stack; // create here a data structure for the cube of types public static Hashtable> getSymbolTable() public static void checkVariable(String type, String id) public static String isBoolean() public static String isTypeMatching() public static void stackPush (String type) public static String stackPop() private String calculateType(String type, String operator) private String calculateType(String type1, String type2, String operator) private static void error(Gui gui, int error, int line, String info) }

Slide 17

Slide 17 text

Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Programming Assignment 3 Level 3 Reviewing Conditions

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Homework Programming Assignment Full Semantic Analyzer

Slide 20

Slide 20 text

Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Let Us Work 1. Functions: Grammar and Semantic Validation 2. Arrays: Grammar and Semantic Validation 3. Switch, For, and Do/While: Grammar and Semantic Validation 4. CallToMethod: Grammar and Semantic Validation

Slide 21

Slide 21 text

Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Tool https:/ /tabatkins.github.io/ railroad-diagrams/generator.html

Slide 22

Slide 22 text

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

Slide 23

Slide 23 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.