2014compiler课件Ch6_3章节

上传人:w****i 文档编号:92194304 上传时间:2019-07-07 格式:PPT 页数:27 大小:75KB
返回 下载 相关 举报
2014compiler课件Ch6_3章节_第1页
第1页 / 共27页
2014compiler课件Ch6_3章节_第2页
第2页 / 共27页
2014compiler课件Ch6_3章节_第3页
第3页 / 共27页
2014compiler课件Ch6_3章节_第4页
第4页 / 共27页
2014compiler课件Ch6_3章节_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《2014compiler课件Ch6_3章节》由会员分享,可在线阅读,更多相关《2014compiler课件Ch6_3章节(27页珍藏版)》请在金锄头文库上搜索。

1、Chapter 6,Data Types and Type Checking,Principal Task of Compiler,Type inference - the computation and maintenance of information on data types 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 relat

2、ed, performed together, and referred as type checking.,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

3、described by a type expressions, which is A type name (such as integer) or A structured expressions (such as array10 ) Operations are assumed or implied,Type Expressions,Type expressions can occur in several places in a program. Explicit type information int x; - associates a type to a variable name

4、 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: ai. A ran

5、ge checking is not statically determinable.,Simple Types,There are build-in ( 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. Ele

6、mentary types like boolean or char whose behavior is easy to implement. Such data types are simple types, their values have no internal structure.,New Simple Data Types,In some programming languages it is possible to define new simple types. typedef enum red, green, blue Color;,Structured Types,Give

7、n a set of predefined types, new data types can be created using type constructors, such as array and struct. Such types are often called structured types. Arrays are commonly allocated contiguous storage from smaller to larger indexes.,Type Definitions,Type definitions provides a mechanism to assig

8、n names to type expressions. Example: Type declarations cause the declared type names to be entered into the symbol table. Type names are associated with attributes (like scope) in the symbol table.,typedef struct double r; int i; RealIntRec;,RealIntRec x;,Recursive Data Types,Recursive data types i

9、nclude lists, trees, and other structures. Languages may or may not permit the direct use of recursion in type declarations. C allows recursion only indirectly, through pointers.,struct intBST int val; intBST *left, *right; ;,Type Equivalence,Are two type expressions equivalent? bolean typeEqual ( T

10、ypeExp t1, TypeExp t2) Takes two type expressions Returns true, if they represent the same type according to the type equivalence rules of the language. Returns false, otherwise How are type expressions are within a compiler? One method is to syntax tree representation.,Example 1,Simple grammar for

11、type expressions. No new type names are allowed.,var-decls - var-decls ; var-decl | var-decl var-dec - id: type-exp type-exp - simple-type | struct-type simple-type - int | bool | real | char | void struct-type - array num of type-exp | record var-decls end | pointer to type-exp | proc(type-exps) ty

12、pe-exp type-exps - type-exps , type-exp | type-exp,Example 1 (cont),record x: pointer to real; y: array 10 of int end,record,var (x),var (y),pointer,real,array (10),int,Structural Equivalence,The only one available in the absence of names for types. Two types are the same if and only if they have th

13、e same structure. If syntax trees are used to represent types, two types are the same if and only if they have syntax trees that are identical in structure. Two array are not equivalent unless they have the same size and component type. Two records are not equivalent unless they have the same compon

14、ents with the same name and the same order. Different choices can be made.,Type expressions with type declarations.,var-decls - var-decls ; var-decl | var-decl var-dec - id: simple-type-exp type-decls - type-decls ; type-decl | type-decl type-decl - id = type-exp type-exp - simple-simple-type | stru

15、ct-type simple-type-exp - simple-type | id simple-type - int | bool | real | char | void struct-type - array num of type-exp | record var-decls end | pointer to type-exp | proc(type-exps) type-exp type-exps - type-exps , simple-type-exp | simple-type-exp,Example 2,Example 2 (cont),record x: pointer

16、to real; y: array 10 of int end,t1 = pointer to real; t2 = array 10 of int t3 = record x: t1 y: t2 end,Name Equivalence,Two type expressions are equivalent if and only if they are either the same simple type or are the same type name. t1 and t2 are not equivalent. Actual type names are entered into the symbol table. It is possible to retain structural equivalence . When a name is encountered, its corresponding type expression must be fetched from the symb

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号