【正文】
Compiler Theory Fall 2021 Jianhui Yue Chapter 6 Data Types and Type Checking Instructor Jianhui Yue Software College @SCU Compiler Theory Fall 2021 Jianhui Yue Principal Task of Compiler 1. Type inference the putation and maintenance of information on data types 2. Type checking – the use of the information to ensure that each part of the program makes sense under the type rules of the language. ? These two tasks are related, performed together, and referred as type checking. Compiler Theory Fall 2021 Jianhui Yue Data Type ? Data type is set of values with certain operations on those values. ? Example: – integer refers to a subset of the mathematical integers, together with the arithmetic operations that are provided by the language definition. ? Data types are described by a type expressions, which is – A type name (such as integer) or – A structured expressions (such as array[10] ) – Operations are assumed or implied Compiler Theory Fall 2021 Jianhui Yue Type Expressions ? Type expressions can occur in several places in a program. ? Explicit type information – int x。 associates a type to a variable name – class Car { …} – defines a new type name ? Implicit type information – const greeting = “Hello!”。 array of char in Pascal. ? Type information, that is contained in declarations, is maintained in the symbol table and retrieved by the type checker whenever the associated names are referenced. – Example: a[i]. A range checking is not statically determinable. Compiler Theory Fall 2021 Jianhui Yue Simple Types ? There are buildin ( predefined ) types in a programming language. – Example: int and double ? The predefined types correspond to – Numeric data types that are provided by machine architectures and whose operations exist as machine instructions. – Elementary types like boolean or char whose behavior is easy to implement. ? Such data types are simple types, their values have no internal structure. Compiler Theory Fall 2021 Jianhui Yue N