SlideShare a Scribd company logo
1
Static Checking and Type
Systems
Chapter 6
COP5621 Compiler Construction
Copyright Robert van Engelen, Florida State University, 2005
2
The Structure of our Compiler
Revisited
Lexical analyzer
Syntax-directed
static checker
Character
stream
Token
stream
Java
bytecode
Yacc specification
JVM specification
Lex specification
Syntax-directed
translator
Type
checking
Code
generation
3
Static versus Dynamic Checking
• Static checking: the compiler enforces
programming language’s static semantics,
which are checked at compile time
• Runtime checking: dynamic semantics are
checked at run time by special code
generated by the compiler
4
Static Checking
• Typical examples of static checking are
– Type checks
– Flow-of-control checks
– Uniqueness checks
– Name-related checks
5
Type Checks, Overloading,
Coercion, and Polymorphism
int op(int), op(float);
int f(float);
int a, c[10], d;
d = c+d; // FAIL
*d = a; // FAIL
a = op(d); // OK: overloading (C++)
a = f(d); // OK: coersion
vector<int> v; // OK: template instantiation
6
Flow-of-Control Checks
myfunc()
{ …
break; // ERROR
}
myfunc()
{ …
switch (a)
{ case 0:
…
break; // OK
case 1:
…
}
}
myfunc()
{ …
while (n)
{ …
if (i>10)
break; // OK
}
}
7
Uniqueness Checks
myfunc()
{ int i, j, i; // ERROR
…
}
cnufym(int a, int a) // ERROR
{ …
}
struct myrec
{ int name;
};
struct myrec // ERROR
{ int id;
};
8
Name-Related Checks
LoopA: for (int I = 0; I < n; I++)
{ …
if (a[I] == 0)
break LoopB;
…
}
9
One-Pass versus Multi-Pass
Static Checking
• One-pass compiler: static checking for C,
Pascal, Fortran, and many other languages
can be performed in one pass while at the
same time intermediate code is generated
• Multi-pass compiler: static checking for
Ada, Java, and C# is performed in a
separate phase, sometimes requiring
traversing the syntax tree multiple times
10
Type Expressions
• Type expressions are used in declarations
and type casts to define or refer to a type
– Primitive types, such as int and bool
– Type constructors, such as pointer-to, array-of,
records and classes, templates, and functions
– Type names, such as typedefs in C and named
types in Pascal, refer to type expressions
11
Graph Representations for Type
Expressions
int *f(char*,char*)
fun
args pointer
char
int
pointer
char
pointer
Tree forms
fun
args pointer
char
int
pointer
DAGs
12
Cyclic Graph Representations
struct Node
{ int val;
struct Node *next;
};
struct
val
pointer
int
Cyclic graph
next
13
Name Equivalence
• Each type name is a distinct type, even
when the type expressions the names refer
to are the same
• Types are identical only if names match
• Used by Pascal (inconsistently)
type link = ^node;
var next : link;
last : link;
p : ^node;
q, r : ^node;
With name equivalence in Pascal:
p ≠ next
p ≠ last
p = q = r
next = last
14
Structural Equivalence of Type
Expressions
• Two types are the same if they are
structurally identical
• Used in C, Java, C#
struct
val next
int
pointer
struct
val
int
pointer
=
pointer
next
15
Structural Equivalence of Type
Expressions (cont’d)
• Two structurally equivalent type
expressions have the same pointer address
when constructing graphs by sharing nodes
struct
val
int
pointer
s
p
struct Node
{ int val;
struct Node *next;
};
struct Node s, *p;
… p = &s; // OK
… *p = s; // OK
next
&s
*p
16
Constructing Type Graphs in
Yacc
Type *mkint() construct int node if not already
constructed
Type *mkarr(Type*,int) construct array-of-type node
if not already constructed
Type *mkptr(Type*) construct pointer-of-type node
if not already constructed
17
Syntax-Directed Definitions for
Constructing Type Graphs in Yacc
%union
{ Symbol *sym;
int num;
Type *typ;
}
%token INT
%token <sym> ID
%token <int> NUM
%type <typ> type
%%
decl : type ID { addtype($2, $1); }
| type ID ‘[’ NUM ‘]’ { addtype($2, mkarr($1, $4)); }
;
type : INT { $$ = mkint(); }
| type ‘*’ { $$ = mkptr($1); }
| /* empty */ { $$ = mkint(); }
;
18
Type Systems
• A type system defines a set of types and
rules to assign types to programming
language constructs
• Informal type system rules, for example “if
both operands of addition are of type
integer, then the result is of type integer”
• Formal type system rules: Post system
19
Type Rules in Post System
Notation
E

e1 : integer E

e2 : integer
E

e1 + e2 : integer
Environment E maps
variables v to types T:
E(v) = T
E(v) = T
E

v : T
E

e : T
E

v := e
E(v) = T
20
A Simple Language Example
P  D ; S
D  D ; D
| id : T
T  boolean
| char
| integer
| array [ num ] of T
| ^ T
S  id := E
| if E then S
| while E do S
| S ; S
E  true
| false
| literal
| num
| id
| E and E
| E mod E
| E [ E ]
| E ^
21
Simple Language Example:
Declarations
D  id : T { addtype(id.entry, T.type) }
T  boolean { T.type := boolean }
T  char { T.type := char }
T  integer { T.type := integer }
T  array [ num ] of T1 { T.type := array(1..num.val, T1.type) }
T  ^ T1 { T.type := pointer(T1)
22
Simple Language Example:
Statements
S  id := E { S.type := if id.type = E.type then void
else type_error }
S  if E then S1 { S.type := if E.type = boolean then S1.type
else type_error }
S  while E do S1 { S.type := if E.type = boolean then S1.type
else type_error }
S  S1 ; S2 { S.type := if S1.type = void and S2.type = void
then void else type_error }
23
Simple Language Example:
Expressions
E  true { E.type = boolean }
E  false { E.type = boolean }
E  literal { E.type = char }
E  num { E.type = integer }
E  id { E.type = lookup(id.entry) }
…
24
Simple Language Example:
Expressions (cont’d)
E  E1 and E2 { E.type := if E1.type = boolean and
E2.type = boolean
then boolean else type_error }
E  E1 mod E2 { E.type := if E1.type = integer and
E2.type = integer
then integer else type_error }
E  E1 [ E2 ] { E.type := if E1.type = array(s, t) and
E2.type = integer
then t else type_error }
E  E1 ^ { E.type := if E1.type = pointer(t)
then t else type_error }
25
Simple Language Example:
Adding Functions
T  T1 -> T2 { T.type := function(T1.type, T2.type) }
E  E1 ( E2 ) { E.type := if E1.type = function(s, t) and
E2.type = s
then t else type_error }
Example:
v : integer;
odd : integer -> boolean;
if odd(3) then
v := 1;
26
Syntax-Directed Definitions for
Type Checking in Yacc
%{
enum Types {Tint, Tfloat, Tpointer, Tarray, … };
typedef struct Type
{ enum Types type;
struct Type *child;
} Type;
%}
%union
{ Type *typ;
}
%type <typ> expr
%%
expr : expr ‘+’ expr { if ($1.type != Tint
|| $3.type != Tint)
semerror(“non-int operands in +”);
$$ = mkint();
emit(iadd);
}
27
Type Conversion and Coercion
• Type conversion is explicit, for example
using type casts
• Type coercion is implicitly performed by the
compiler
• Both require a type system to check and
infer types for (sub)expressions
28
Syntax-Directed Definitions for
Type Coercion in Yacc
%{ … %}
%%
expr : expr ‘+’ expr
{ if ($1.type == Tint && $3.type == Tint)
{ $$ = mkint(); emit(iadd);
}
else if ($1.type == Tfloat && $3.type == Tfloat)
{ $$ = mkfloat(); emit(fadd);
}
else if ($1.type == Tfloat && $3.type == Tint)
{ $$ = mkfloat(); emit(i2f); emit(fadd);
}
else if ($1.type == Tint && $3.type == Tfloat)
{ $$ = mkfloat(); emit(swap); emit(i2f); emit(fadd);
}
else semerror(“type error in +”);
$$ = mkint();
}
29
Syntax-Directed Definitions for
L-Values and R-Values in Yacc
%{
typedef struct Node
{ Type *typ;
int islval;
} Node;
%}
%union
{ Node *rec;
}
%type <rec> expr
%%
expr : expr ‘+’ expr
{ if ($1.typ->type != Tint
|| $3.typ->type != Tint)
semerror(“non-int operands in +”);
$$.typ = mkint();
$$.islval = FALSE;
emit(…);
}
| expr ‘=’ expr
{ if (!$1.islval || $1.typ != $3.typ)
semerror(“invalid assignment”);
$$.typ = $1.typ; $$.islval = FALSE;
emit(…);
}
| ID
{ $$.typ = lookup($1);
$$.islval = TRUE;
emit(…);
}

More Related Content

Similar to Ch6.ppt

Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...
AmitSingh395981
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
Eelco Visser
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
z9819898203
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
YashwanthMalviya
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
Radhakrishnan Chinnusamy
 
Ch3
Ch3Ch3
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
 
Effective Modern C++
Effective Modern C++Effective Modern C++
Effective Modern C++
Wang Hsiangkai
 
Syntax
SyntaxSyntax
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
Yusuke Miyazaki
 
C Tutorial
C TutorialC Tutorial
C Tutorial
Dr.Subha Krishna
 
Ch5a
Ch5aCh5a
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
Tia Ricci
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
Eelco Visser
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl language
Masahiro Honma
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
KamranAli649587
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2
gadisaAdamu
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
Rasan Samarasinghe
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
SHUJEHASSAN
 

Similar to Ch6.ppt (20)

Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Ch3
Ch3Ch3
Ch3
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
 
Effective Modern C++
Effective Modern C++Effective Modern C++
Effective Modern C++
 
Syntax
SyntaxSyntax
Syntax
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
 
C Tutorial
C TutorialC Tutorial
C Tutorial
 
Ch5a
Ch5aCh5a
Ch5a
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl language
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 

Recently uploaded

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 

Recently uploaded (20)

writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 

Ch6.ppt

  • 1. 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005
  • 2. 2 The Structure of our Compiler Revisited Lexical analyzer Syntax-directed static checker Character stream Token stream Java bytecode Yacc specification JVM specification Lex specification Syntax-directed translator Type checking Code generation
  • 3. 3 Static versus Dynamic Checking • Static checking: the compiler enforces programming language’s static semantics, which are checked at compile time • Runtime checking: dynamic semantics are checked at run time by special code generated by the compiler
  • 4. 4 Static Checking • Typical examples of static checking are – Type checks – Flow-of-control checks – Uniqueness checks – Name-related checks
  • 5. 5 Type Checks, Overloading, Coercion, and Polymorphism int op(int), op(float); int f(float); int a, c[10], d; d = c+d; // FAIL *d = a; // FAIL a = op(d); // OK: overloading (C++) a = f(d); // OK: coersion vector<int> v; // OK: template instantiation
  • 6. 6 Flow-of-Control Checks myfunc() { … break; // ERROR } myfunc() { … switch (a) { case 0: … break; // OK case 1: … } } myfunc() { … while (n) { … if (i>10) break; // OK } }
  • 7. 7 Uniqueness Checks myfunc() { int i, j, i; // ERROR … } cnufym(int a, int a) // ERROR { … } struct myrec { int name; }; struct myrec // ERROR { int id; };
  • 8. 8 Name-Related Checks LoopA: for (int I = 0; I < n; I++) { … if (a[I] == 0) break LoopB; … }
  • 9. 9 One-Pass versus Multi-Pass Static Checking • One-pass compiler: static checking for C, Pascal, Fortran, and many other languages can be performed in one pass while at the same time intermediate code is generated • Multi-pass compiler: static checking for Ada, Java, and C# is performed in a separate phase, sometimes requiring traversing the syntax tree multiple times
  • 10. 10 Type Expressions • Type expressions are used in declarations and type casts to define or refer to a type – Primitive types, such as int and bool – Type constructors, such as pointer-to, array-of, records and classes, templates, and functions – Type names, such as typedefs in C and named types in Pascal, refer to type expressions
  • 11. 11 Graph Representations for Type Expressions int *f(char*,char*) fun args pointer char int pointer char pointer Tree forms fun args pointer char int pointer DAGs
  • 12. 12 Cyclic Graph Representations struct Node { int val; struct Node *next; }; struct val pointer int Cyclic graph next
  • 13. 13 Name Equivalence • Each type name is a distinct type, even when the type expressions the names refer to are the same • Types are identical only if names match • Used by Pascal (inconsistently) type link = ^node; var next : link; last : link; p : ^node; q, r : ^node; With name equivalence in Pascal: p ≠ next p ≠ last p = q = r next = last
  • 14. 14 Structural Equivalence of Type Expressions • Two types are the same if they are structurally identical • Used in C, Java, C# struct val next int pointer struct val int pointer = pointer next
  • 15. 15 Structural Equivalence of Type Expressions (cont’d) • Two structurally equivalent type expressions have the same pointer address when constructing graphs by sharing nodes struct val int pointer s p struct Node { int val; struct Node *next; }; struct Node s, *p; … p = &s; // OK … *p = s; // OK next &s *p
  • 16. 16 Constructing Type Graphs in Yacc Type *mkint() construct int node if not already constructed Type *mkarr(Type*,int) construct array-of-type node if not already constructed Type *mkptr(Type*) construct pointer-of-type node if not already constructed
  • 17. 17 Syntax-Directed Definitions for Constructing Type Graphs in Yacc %union { Symbol *sym; int num; Type *typ; } %token INT %token <sym> ID %token <int> NUM %type <typ> type %% decl : type ID { addtype($2, $1); } | type ID ‘[’ NUM ‘]’ { addtype($2, mkarr($1, $4)); } ; type : INT { $$ = mkint(); } | type ‘*’ { $$ = mkptr($1); } | /* empty */ { $$ = mkint(); } ;
  • 18. 18 Type Systems • A type system defines a set of types and rules to assign types to programming language constructs • Informal type system rules, for example “if both operands of addition are of type integer, then the result is of type integer” • Formal type system rules: Post system
  • 19. 19 Type Rules in Post System Notation E  e1 : integer E  e2 : integer E  e1 + e2 : integer Environment E maps variables v to types T: E(v) = T E(v) = T E  v : T E  e : T E  v := e E(v) = T
  • 20. 20 A Simple Language Example P  D ; S D  D ; D | id : T T  boolean | char | integer | array [ num ] of T | ^ T S  id := E | if E then S | while E do S | S ; S E  true | false | literal | num | id | E and E | E mod E | E [ E ] | E ^
  • 21. 21 Simple Language Example: Declarations D  id : T { addtype(id.entry, T.type) } T  boolean { T.type := boolean } T  char { T.type := char } T  integer { T.type := integer } T  array [ num ] of T1 { T.type := array(1..num.val, T1.type) } T  ^ T1 { T.type := pointer(T1)
  • 22. 22 Simple Language Example: Statements S  id := E { S.type := if id.type = E.type then void else type_error } S  if E then S1 { S.type := if E.type = boolean then S1.type else type_error } S  while E do S1 { S.type := if E.type = boolean then S1.type else type_error } S  S1 ; S2 { S.type := if S1.type = void and S2.type = void then void else type_error }
  • 23. 23 Simple Language Example: Expressions E  true { E.type = boolean } E  false { E.type = boolean } E  literal { E.type = char } E  num { E.type = integer } E  id { E.type = lookup(id.entry) } …
  • 24. 24 Simple Language Example: Expressions (cont’d) E  E1 and E2 { E.type := if E1.type = boolean and E2.type = boolean then boolean else type_error } E  E1 mod E2 { E.type := if E1.type = integer and E2.type = integer then integer else type_error } E  E1 [ E2 ] { E.type := if E1.type = array(s, t) and E2.type = integer then t else type_error } E  E1 ^ { E.type := if E1.type = pointer(t) then t else type_error }
  • 25. 25 Simple Language Example: Adding Functions T  T1 -> T2 { T.type := function(T1.type, T2.type) } E  E1 ( E2 ) { E.type := if E1.type = function(s, t) and E2.type = s then t else type_error } Example: v : integer; odd : integer -> boolean; if odd(3) then v := 1;
  • 26. 26 Syntax-Directed Definitions for Type Checking in Yacc %{ enum Types {Tint, Tfloat, Tpointer, Tarray, … }; typedef struct Type { enum Types type; struct Type *child; } Type; %} %union { Type *typ; } %type <typ> expr %% expr : expr ‘+’ expr { if ($1.type != Tint || $3.type != Tint) semerror(“non-int operands in +”); $$ = mkint(); emit(iadd); }
  • 27. 27 Type Conversion and Coercion • Type conversion is explicit, for example using type casts • Type coercion is implicitly performed by the compiler • Both require a type system to check and infer types for (sub)expressions
  • 28. 28 Syntax-Directed Definitions for Type Coercion in Yacc %{ … %} %% expr : expr ‘+’ expr { if ($1.type == Tint && $3.type == Tint) { $$ = mkint(); emit(iadd); } else if ($1.type == Tfloat && $3.type == Tfloat) { $$ = mkfloat(); emit(fadd); } else if ($1.type == Tfloat && $3.type == Tint) { $$ = mkfloat(); emit(i2f); emit(fadd); } else if ($1.type == Tint && $3.type == Tfloat) { $$ = mkfloat(); emit(swap); emit(i2f); emit(fadd); } else semerror(“type error in +”); $$ = mkint(); }
  • 29. 29 Syntax-Directed Definitions for L-Values and R-Values in Yacc %{ typedef struct Node { Type *typ; int islval; } Node; %} %union { Node *rec; } %type <rec> expr %% expr : expr ‘+’ expr { if ($1.typ->type != Tint || $3.typ->type != Tint) semerror(“non-int operands in +”); $$.typ = mkint(); $$.islval = FALSE; emit(…); } | expr ‘=’ expr { if (!$1.islval || $1.typ != $3.typ) semerror(“invalid assignment”); $$.typ = $1.typ; $$.islval = FALSE; emit(…); } | ID { $$.typ = lookup($1); $$.islval = TRUE; emit(…); }