清华大学软件学院编译原理课件pdf

上传人:xzh****18 文档编号:45721539 上传时间:2018-06-18 格式:PDF 页数:54 大小:976.84KB
返回 下载 相关 举报
清华大学软件学院编译原理课件pdf_第1页
第1页 / 共54页
清华大学软件学院编译原理课件pdf_第2页
第2页 / 共54页
清华大学软件学院编译原理课件pdf_第3页
第3页 / 共54页
清华大学软件学院编译原理课件pdf_第4页
第4页 / 共54页
清华大学软件学院编译原理课件pdf_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《清华大学软件学院编译原理课件pdf》由会员分享,可在线阅读,更多相关《清华大学软件学院编译原理课件pdf(54页珍藏版)》请在金锄头文库上搜索。

1、THSSTHSS34100162341001622010 / 43012010 / 4301Principles of Compilation王朝坤王朝坤 IISETsinghuaTHSSTHSS34100162341001622010 / 43012010 / 4301Chap. 1 IntroductionChaokun Wang IISETsinghuaTHSSTHSS34100162341001622010 / 43012010 / 43013Outline What 什么是编译和编译器什么是编译和编译器 Why Course Info. PL 编译与解释的区别和联系编译与解释的区别和

2、联系 History Contents编译各阶段的内容编译各阶段的内容THSSTHSS34100162341001622010 / 43012010 / 43011.1 Computer a := b+2 ; write a ; 直接将直接将4的的值输值输出(出(显示显示) (直接对源直接对源程序中的语程序中的语句进句进行行分析分析,执行,执行其隐含其隐含的的操作。操作。)Int 2 St b Ld b add 2 St a生成代码 CompilerInterpreterTHSSTHSS34100162341001622010 / 43012010 / 430126编译程序和解释程序编译程序和

3、解释程序编译程序是一编译程序是一个个语言语言处理处理程序程序, 把把一一个个高级语言程序翻译成高级语言程序翻译成某个某个机器的汇编机器的汇编或二进制或二进制 代代码码程序程序, 这个二进制这个二进制代代码码程序在机器程序在机器上运上运行行以生以生成成 结果结果. 通过通过编译程序使编译程序使得我们得我们可可以先准备以先准备好一好一个个在在该该机器机器上运上运 行的程序行的程序,然然后这个后这个程序程序便会以便会以机器的机器的速度运速度运行行. 但但是在不是在不把整个把整个程序程序全部都全部都翻译翻译结结束束之之后后,这个这个程序是不程序是不 能能开开始始运运行行,也也不不能产能产生生任任何何结

4、果结果的的. 编译和编译和运运行是行是两个两个独立独立分分开的阶段开的阶段. 但但在一在一个个交互交互环境环境中中,不不需需要将这两个要将这两个阶段阶段分分隔隔开开,编译编译就就 不如解释的不如解释的方方法法更更方方便便. 解释程序不解释程序不需需要要在在运运行行前前先把源先把源程序翻译成程序翻译成目标目标代代码码,也也 可可以以让让我们实现我们实现在在某某台台机器机器上运上运行程序行程序并并生生成成结果结果.THSSTHSS34100162341001622010 / 43012010 / 430127解释程序解释程序 是是这这样样一一个个程序程序,它它接接受受某个某个语言的程序语言的程序并

5、立即并立即运运 行行这个源这个源程序程序. 它它的工的工作作模式模式是一是一个个个个的的获取获取,分析分析并并执行执行源源程序程序 语语句句,一一旦第旦第一一个个语语句分析结句分析结束束,源源程序程序便便开开始始运运 行行并且并且生生成成结果结果, 它特它特别别适合适合程序程序员交互方式员交互方式的的 工工作情况作情况,即希望即希望在在获取获取下下一一个个语语句句之之前前了解了解每每个个 语语句句的执行的执行结果结果,允许允许执行执行时时修改修改程序程序. 著名著名的解释程序有的解释程序有Basic语言解释程序语言解释程序 ,Lisp语言语言 解释程序解释程序,UNIX命令命令语言解释程序语言

6、解释程序(shell),数据库数据库 查询查询语言语言SQL 解释程序解释程序以以及及bytecode解释程序解释程序.THSSTHSS34100162341001622010 / 43012010 / 430128高级语言解释系统高级语言解释系统(interpreter) 功能功能让让计算机执行高级语言(计算机执行高级语言(basic,lisp,prolog) 与编译程序的不与编译程序的不同同1)不)不生生成成目标目标代代码码 2)能支持交互能支持交互环境环境 (同同增增量量式式编译系编译系统统) 源源程 序程 序初始数据初始数据解释程序计算结果THSSTHSS341001623410016

7、22010 / 43012010 / 4301291.5 History of compiler development1953 IBM develops the 701 EDPM (Electronic Data Processing Machine), the first general purpose computer, built as a “defense calculator” in the Korean WarNo high- level languages were available, so all programming was done in assemblyTHSSTH

8、SS34100162341001622010 / 43012010 / 430130History of compilers (contd)As expensive as these early computers were, most of the money companies spent was for software development, due to the complexities of assembly.John BackusIn 1953, John Backus came up with the idea of “speed coding”, and developed

9、 the first interpreter. Unfortunately, this was 10- 20 times slower than programs written in assembly.He was sure he could do better.THSSTHSS34100162341001622010 / 43012010 / 430131History of compilers (contd)In 1954, Backus and his team released a research paper titled “Preliminary Report, Specific

10、ations for the IBM Mathematical FORmula TRANslating System, FORTRAN.”The initial release of FORTRAN I was in 1956, totaling 25,000 lines of assembly code. Compiled programs ran almost as fast as handwritten assembly!Projects that had taken two weeks to write now took only 2 hours. By 1958 more than

11、half of all software was written in FORTRAN.THSSTHSS34100162341001622010 / 43012010 / 430132How to make a compilerTHSSTHSS34100162341001622010 / 43012010 / 430133The A- S ModelThe Analysis- Synthesis Model of Compilation analysis breaks up the source program into constituent pieces creates an interm

12、ediate representation of the source program synthesis constructs the desired target program from the intermediate representation. requires the most specialized techniquesDuring analysis Data Symbol TableTHSSTHSS34100162341001622010 / 43012010 / 430134Syntax Tree Operations A hierarchical structure t

13、he operations implied by the source program are determined. the operations are recorded Syntax tree a special kind of tree stores the operations implied by the source program each node represents an operation the children of a node represent the arguments of the operation. A syntax tree for the assi

14、gnment statement position:= initial + rate *60THSSTHSS34100162341001622010 / 43012010 / 430135Symbol- Table Management As essential function of a compiler is to record the identifiers used in the source program collect information about various attributes of each identifier. These attributes may pro

15、vide information about the storage allocated for an identifier, its type, its scope (作作用用域域, where in the program it is valid), in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (e.g., by reference), and the type returned, if any.THSSTHSS34100162341001622010 / 43012010 / 430136Symbol- Table A symbol table (符号表符号表) is a data structure containing a record for each identifier, with fields

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 计算机原理

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