Docsity

Type Checking in Semantic Analysis II - EECS 483, University of Michigan, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Type Checking in Semantic Analysis II - EECS 483, University of Michigan and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

  • Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18,

Announcements^ ™^ Updated schedule^ » Today: Semantic analysis II^ » Mon 10/23: MIRV Q/A session (Yuan Lin)^ » Wed 10/25: Semantic analysis III (Simon Chen)^ » Mon 10/30: Exam review^ » Wed 11/1: Exam 1 in class^ ™^ Project 2^ » Teams of 2

Æ^ Please send Simon/I mail with names y^ Persons can work individually if really want to » No extensions on deadline due to Exam, so getstarted!

™^ Reading - 6.1-6.

  • 3 -

Catching Semantic Errors int x; void f(int m) {^ float x, y;^ ...^ {int i, j; x=1; }^ {int x; l:

i=2 ; } } int g(int n) {^ char t;^ x=3; }

x^ var^ int f^ func^ int^ Æ^ void g^ func^ int^ Æ^ int

m^ arg^ int x^ var^ float y^ var^ float

n^ arg^ int t^ var^ char

i^ var^ int j^ var^

x int var

int l^ label Global symtab

i=

Error! undefinedvariable

Symbol Table Operations^ ™^ Two operations:^ » To build symbol tables, we need to insert new

identifiers in the table » In the subsequent stages of the compiler we need toaccess the information from the table: use lookupfunction ™ Cannot build symbol tables during lexical analysis » Hierarchy of scopes encoded in syntax ™ Build the symbol tables: » While parsing, using the semantic actions » After the AST is constructed

Back to Type Checking^ ™^ What are types?^ » They describe the values computed during the

execution of the program » Essentially they are a predicate on values^ y^ E.g., “int x” in C means –2^31 <= x < 2^31 ™ Type Errors: improper or inconsistentoperations during program execution ™ Type-safety: absence of type errors

How to Ensure Type-Safety^ ™^ Bind (assign) types, then check types^ ™^ Type binding: defines type of constructs inthe program (e.g., variables, functions)^ » Can be either explicit (int x) or implicit (x=1)^ » Type consistency (safety) = correctness with

respect to the type bindings ™ Type checking: determine if the programcorrectly uses the type bindings » Consists of a set of type-checking rules

4 Concepts Related to Types/Languages^ 1.^ Static vs dynamic checking^ »^

When to check types

2.^ Static vs dynamic typing^ »^

When to define types

3.^ Strong vs weak typing^ »^

How many type errors

4.^ Sound type systems^ »^

Statically catch all type errors

Static vs Dynamic Checking^ ™^ Static type checking^ » Perform at compile time^ ™^ Dynamic type checking^ » Perform at run time (as the program executes)^ ™^ Examples of dynamic checking^ » Array bounds checking^ » Null pointer dereferences

Strong vs Weak Typing^ ™^ Refer to how much type consistency isenforced^ ™^ Strongly typed languages^ » Guarantee accepted programs are type-safe^ ™^ Weakly typed languages^ » Allow programs which contain type errors^ ™^ These concepts refer to run-time^ » Can achieve strong typing using either static

or dynamic typing

Soundness^ ™^ Sound type systems: can statically ensurethat the program is type-safe^ ™^ Soundness implies strong typing^ ™^ Static type safety requires a conservativeapproximation of the values that may occurduring all possible executions^ » May reject type-safe programs^ » Need to be expressive: reject as few type-safe

programs as possible

Why Static Checking?^ ™^ Efficient code^ » Dynamic checks slow down the program^ ™^ Guarantees that all executions will be safe^ » Dynamic checking gives safety guarantees

only for some execution of the program ™ But is conservative for sound systems » Needs to be expressive: reject few type-safeprograms

Type Systems^ ™^ What are types?^ » They describe the values computed during the

execution of the program » Essentially they are a predicate on values^ y^ E.g., “int x” in C means –2^31 <= x < 2^31 ™ Type expressions

: Describe the possible

types in the program^ » E.g., int, char*, array[], object, etc. ™ Type system

: Defines types for language

constructs^ » E.g., expressions, statements

Type Comparison^ ™^ Option 1: Implement a method T1.Equals(T2)^ » Must compare type trees of T1 and T2^ » For object-oriented languages: also need sub-typing,

T1.SubtypeOf(T2) ™ Option 2: Use unique objects for each distincttype » Each type expression (e.g., array[int]) resolved tosame type object everywhere » Faster type comparison: can use == » Object-oriented: check subtyping of type objects

Creating Type Objects^ ™^ Build types while parsing – use a syntax-directed definitionnon terminal Type typetype : INTEGER^ ™^ Type objects = AST nodes for typeexpressions

{$$ = new IntType(id); }| ARRAY LBRACKET type RBRACKET{$$ = new ArrayType($3); } ;