Docsity

Type Checking - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Type Checking - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chap 6: Type Checking/Semantic Analysis

Overview

 Type Checking and Semantic Analysis is a Critical Features of Compilers and Compilation

 Passing a Syntax Check (Parsing) not Sufficient

 Type Checking Provides Vital Input  Software Engineers Assisted in Debugging Process

 We’ll Focus on Classical Type Checking Issues

 Background and Motivation  Type Analysis  The Notion of a Type System  Examining a Simple Type Checker  Other Key Typing Concepts

 Concluding Remarks/Looking Ahead

Background and Motivation

 What we have achieved

 All the “words” (Tokens) are known  The tree is syntactically correct

 What we still do not know...

 Does the program make sense?

 What we will not try to find out

 Is the program correct?  This is Impossible!

 Our Concern:

 Does it Compile?  Are all Semantic Errors Removed?  Do all Types and their Usage Make Sense?

Background and Motivation

 The program makes “sense”

 Programmer’s intent is clear  Program is semantically unambiguous  Data-wise

  • We know what each name denotes
  • We know how to represent everything  Flow-wise
  • We know how to execute all the statements  Structure-wise
  • Nothing is missing
  • Nothing is multiply defined  The program is correct

 It will produce the expected input

Output?

 What the analysis produce

 Data structures “on the side”  To describe the types(resolve the types)  To describe what each name denotes (resolve the scopes)  A Decorated tree  Add annotations in the tree  Possibly.... Semantic Errors!

Pictorially

Type Analysis

 Why Bother?

A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kind of values they compute.

Uses

 Many!

 Error detection  Detect early  Detect automatically  Detect obvious and subtle flaws  Abstraction  The skeleton to provide modularity  Signature/structure/interface/class/ADT/....  Documentation  Program are easier to read  Language Safety guarantee  Memory layout  Bound checking  Efficiency Docsity.com

How do we do this?

 Compute the type of every sentence

 On the tree  With a tree traversal  Some information will flow up (synthesized)  Some information will flow down (inherited)

 Questions to answer

 What is a type?  How do I create types?  How do I compute types?

Types...

 Types form a language!

 With  Terminals...  Non-terminals....  And a grammar!

 Alternatively

 Types can be defined inductively  Base types (a.k.a. the terminals)  Inductive types (a.k.a. grammatical productions)

Inductive Type Definition

 Purpose

 Define a type in terms of other simple/smaller types

 Example

 array  pointer  reference  Pair (products in the book)  structure  function  methods  classes  ...

Relation to Grammar?

Type → array ( Type , Type ) → pair ( Type , Type ) → tuple ( Type +^ ) → struct ( FieldType +^ ) → fun ( Type ) : Type → method ( ClassType , Type ) : Type → pointer( Type ) → reference( Type ) → ClassType → BasicType ClassType → class ( name [ , Type ] ) FieldType → name : Type BasicType → int | bool | char | float | double | void | error

So...

 If

 we have Type Term  we have a Type Language

 We can parse it and obtain....

 Type Trees!

fun(tuple(int,char)) : int

The Notion of a Type System

 Logical Placement of Type Checker:

 Role of Type Checker is to Verify Semantic Contexts  Incompatible Operator/Operands  Existence of Flow-of Control Info (Goto/Labels)  Uniqueness w.r.t. Variable Definition, Case Statement Labels/Ranges, etc.  Naming Checks Across Blocks (Begin/End)  Function Definition vs. Function Call  Type Checking can Occur as “Side-Effect” of Parsing via a Judicious Use of Attribute Grammars!  Employ Type Synthesis

Stream^ Token^ Parser^ SyntaxTree CheckerType^ SyntaxTree^ Int. CodeGenerator^ Interm.Repres.