Type Checking
Type Checking
Compiler Construction
Semantic Analysis
Type Checking
1
Overview
struct Type_ {
enum TypeClass typeClass;
int arraySize;
struct Type_ *elementType;
};
enum TypeClass {
TP_INT,
TP_CHAR,
TP_ARRAY
};
4
Type checking in constant declaration
• Constant:
• [+/-] <constant>
• The type of <constant> is integer
Type checking in assign statement
• Assign statement
• <LValue> := <Expr>;
• Basic types of <Lvalue> and <Expr> must be the same
• Depend on object kind of Lvalue, function compileLvalue return its data
type
• Type of the expression is evaluated on parse tree whose leaves are
factors
Type of a factor
case TK_NUMBER:
eat(TK_NUMBER);
type = intType;
break;
case TK_CHAR:
eat(TK_CHAR);
type = charType;
break;
7
Type attribute of a factor (represented by an identifier)
8
Type attribute of a factor (represented by an identifier)
9
Type attribute of a factor (represented by an identifier)
10
Type checking for For statement
# Filename Task
1 Makefile Project
2 scanner.c, scanner.h Token reader
3 reader.h, reader.c Read character from source file
4 charcode.h, charcode.c Classify character
5 token.h, token.c Recognize and classify token, keywords
6 error.h, error.c Manage error types and messages
7 parser.c, parser.h Parse programming structure
8 debug.c, debug.h Debugging
9 symtab.c symtab.h Symbol table construction
10 semantics.c. semantics.h Analyse the program’s semantic
11 main.c Main program
Functions for type checking
• Type comparison
• checkIntType
• checkCharType
• checkArrayType
• checkTypeEquality
Assignment 4