Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Type Checking - Lecture Slides | CMSC 430, Study notes of Computer Science

Material Type: Notes; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-79d
koofers-user-79d 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Type Checking - Lecture Slides | CMSC 430 and more Study notes Computer Science in PDF only on Docsity! 1 Type Checking CS430 2 Roadmap (Where are we?) Last lecture • Context-sensitive analysis → Motivation → Attribute grammars → Ad hoc Syntax-directed translation This lecture • Type checking → Type systems → Using syntax directed translation • Symbol tables → Lexical scoping → Implementation  Stack  Threaded stack CS430 3 Types Type: A set of values and meaningful operations on them Types provide semantic “sanity checks” (consistency checks) and determine efficient implementations for data objects Types help identify → errors, if an operator is applied to an incompatible operand  dereferencing of a non-pointer  adding a function to something  incorrect number of parameters to a procedure  … → which operation to use for overloaded names and operators, or what type coercion to use (e.g.: 3.0 + 1) → identification of polymorphic functions CS430 4 Type Systems Type system: Each language construct (operator, expression, statement, …) is associated with a type expression. The type system is a collection of rules for assigning type expressions to these constructs. Type expressions for → basic types  integer, char, real, boolean, typeError → constructed types // T is a type expression  array(lb…ub, T) // array of T  pointer(T) // pointer to T  T1 X T2 // tuple of T1, T2  T1 → T2 // function w/ arg T1 returning T2 CS430 5 Type Checker • A type checker implements a type system. It computes or “constructs” type expressions for each language construct • Static type checking → Detects type errors at compile time → No run time overhead → Not always possible (e.g., A[i]) • Dynamic type checking → Performed at run time → More flexible, allows prototyping → Run-time overhead to maintain & check tags CS430 6 Types Inference Rules • Specifies the type of an expression • Example → If operands of addition are of type integer, result is of type integer → Result of unary & operator is pointer to type of operand • Denotational semantics of type inference rule E e1 : integer E e2 : integer E (e1 + e2): integer where E is a type environment that maps constants and variables to their type expressions. • Question → How to specify rules that allow type coercion (type widening) from integers to reals in arithmetic expressions? 3.0 + 1 or 1 + 3.0 2 CS430 7 Type Equivalence Structural -- type equivalence: type names are expanded Name -- type equivalence: type names are not expanded Example: type A is array(1..10) of integer; type B is array(1..10) of integer; a : A; b : B; c, d: array(1..10) of integer; e: array(1..10) of integer; Answer: structural equivalence: name equivalence: (a, b, c, d, e) (a), (b), (c, d, e) CS430 8 Syntax Directed Translation Scheme (in CUP) Revisit our type inference rule for “+”. exp ::= exp:e1 PLUS exp:e2 {: if (e1 == sym.INT && e2 == sym.INT ) RESULT = sym.INT ; else { RESULT = typeError; System.out.println(“Error: illegal operand types”); } :} • The definition of type expression as Java types (static final int fields in class sym) should be done in mycc.cup. • The assignment of type expression Java types to terminals and nonterminals of the grammar is done in mycc.cup. CS430 9 Syntax Directed Translation Scheme (in Yacc) Revisit our type inference rule for “+”. exp : exp ‘+’ exp { if ($1 == integer && $3 == integer) $$ = integer; else { $$ = typeError; printf(“Error: illegal operand types\n”); } } • The definition of type expression as C types (structs) should be done in attr.h . attr.c may contain helper functions. • The assignment of type expression C types to terminals and nonterminals of the grammar is done in parse.y. CS430 10 Type Checker Example CS430 11 Type Checker Example (cont.) • Handling declarations CS430 12 Type Checker Example (cont.) • Handling expressions
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved