tailieunhanh - Lecture Programming languages (2/e): Chapter 6a - Tucker, Noonan
Chapter 6 - Type systems. A type system enables the programmer to understand and properly implement operations on values of various types. A carefully specified type system allows the compiler to perfonn rigorous type checking on a program before run time, thus heading off run-time errors that may occur because of inappropriately typed operands. The full specification and implementation of a type system is the focus of a deeper study in Chapter 6. | Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as to maximize the number of errors that cannot be made, or if made, can be reliably detected at compile time. Hoare Contents Type System for Clite Implicit Type Conversion Formalizing the Clite Type System Motivation: Detecting Type Errors The detection of type errors, either at compile time or at run time, is called type checking. Type errors occur frequently in programs. Type errors can’t be prevented/detected by EBNF If undetected, type errors can cause severe run-time errors. A type system can identify type errors before they occur. Type System for CLite Static binding Single function: main Single scope: no nesting, no globals Name resolution errors detected at compile time Each declared variable must have a unique identifier Identifier must not be a keyword (syntactically enforced) Each variable referenced must be declared. Example Clite Program (Fig ) // compute the factorial of integer n void main ( ) { int n, i, result; n = 8; i = 1; result = 1; while (i , , } Can implement as a hash table Function typing creates a type map Function typeOf retrieves the type of a variable: typeOf(id) = type public static TypeMap typing (Declarations d) { TypeMap map = new TypeMap( ); for (Declaration di : d) { (, ); } return map; } The typing Function creates a type map Type Rule All declared variables must have unique names. . | Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as to maximize the number of errors that cannot be made, or if made, can be reliably detected at compile time. Hoare Contents Type System for Clite Implicit Type Conversion Formalizing the Clite Type System Motivation: Detecting Type Errors The detection of type errors, either at compile time or at run time, is called type checking. Type errors occur frequently in programs. Type errors can’t be prevented/detected by EBNF If undetected, type errors can cause severe run-time errors. A type system can identify type errors before they occur. Type System for CLite Static binding Single function: main Single scope: no nesting, no globals Name resolution errors detected at compile time Each declared variable must have a unique identifier Identifier must not be a keyword (syntactically enforced) Each variable referenced must
đang nạp các trang xem trước