Chapter 5 Symbol Tables and Type Checking
Chapter 5 Symbol Tables and Type Checking
Course Outline
1. Introduction
2. Lexical Analysis
3. Syntax Analysis
4. Syntax Directed Translation
5. Symbol Tables & Type Checking
6. Intermediate Code Generation
7. Run Time Environment
8. Code Generation
9. Code Optimization
1
By: Yohannes Taye Jemberie
Chapter 5: Symbol Tables & Type Checking
Type and Issues in typing
Static Checking
Type Checking
Type Systems
Type Expressions
Type Constructors
Specification of a Simple Type Checker
Equivalence of types
Type Conversions
Types
• Type =
– a set of values
– operations that are allowed on these values.
• Why?
– To generate better code, with less runtime
overhead
– To avoid runtime errors
Example:
• In Ada loops can have names. However, the same name should be
used to start and end the loop.
• Kind Checking
– The processes of identifying errors in a program
based on stated kind information
• Variables
• Functions/procedures
By: Yohannes12
Taye Jemberie
Type Systems
• Every language has a set of types and rules for
assigning types to language constructs.
• In almost all languages, types are either basic
or constructed.
• Two types
– Basic types
– Constructed types
By: Yohannes14
Taye Jemberie
• Constructed types (user-defined types)
– types that are constructed from basic types and
other constructed types
– Var A: Array [1..10] of integer
X Pointer
• Expressions
E literal
E num
E id
E E1 mod E2
E E1[E2]
E ^E1
By: Yohannes Taye Jemberie 26
Simple Type Checker (cont’d)
• Expressions
E literal {E.type := char}
E num {E.type := integer}
E id {E.type := lookup (id.lexeme)}
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 E2.type = integer and E1.type := array (s, t) then
t
Else
Type_error
}
E ^E1 { E.type := if E2.type = pointer (t) then
t
Else
Type_error }
By: Yohannes Taye Jemberie 27
Simple Type Checker (cont’d)
• Statements
S id := E
S if E then S1
S while E do S1
S S1 ; S2