




























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































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
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?
The program makes “sense”
Programmer’s intent is clear Program is semantically unambiguous Data-wise
It will produce the expected input
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!
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.
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
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 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)
Purpose
Define a type in terms of other simple/smaller types
Example
array pointer reference Pair (products in the book) structure function methods classes ...
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
If
we have Type Term we have a Type Language
We can parse it and obtain....
Type Trees!
fun(tuple(int,char)) : int
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.