[其它技巧]New Compiler PPT.ppt
《[其它技巧]New Compiler PPT.ppt》由会员分享,可在线阅读,更多相关《[其它技巧]New Compiler PPT.ppt(331页珍藏版)》请在三一办公上搜索。
1、Principles of Compiler Theory and Design,Central South UniversityCollege of Information Science and Engineering,Contents,Preface,Chapter 4 Syntax Analysis,Chapter 1 Introduction to compiling,Chapter 2 Context-free Grammers,Chapter 3 Lexical Analysis,Contents,Chapter 5 Syntax Analysis,Chapter 6 Synta
2、x-Diricted Translation,Chapter 7 Type Checking,Chapter 8 Intermediate Code Generation,Chapter 9 Run-Time Environments,课程说明,课程名称:编译原理 先修课程:C语言、汇编语言、离散数学、数据结构总学时:64(授课学时:64)课程任务:编译原理是计算机专业的重要专业课之一,是每个优秀的计算机专业人员必修的一门课程。以研究程序设计语言编译构造的基本原理和基本实现方法为主要目标,其研究对象是程序设计语言的编译器。通过本课程学习,使学生掌握编译方法的基本理论和设计思想,加深对程序设计语
3、言的理解,为其学习后继专业课奠定坚实的理论基础,并将本课程讨论的概念和技术应用于其他软件设计中,可以比较迅速地掌握新的语言工具。通过本课程的教学培养学生的抽象思维、逻辑推导和概括能力。,Chapter1 Introduction to Compiling1.Compliers,A compiler is a program that reads program written in one language the source language and translates it into a equivalent program in another language the target
4、 language.As an important par of this translation process,the compile reports to its user the presence of errors in the source program.,2.The Context of a Compiler,Lexical AnalysisIn a compiler,linear analysis is called lexical analysis or scanning.For example,in lexical analysis the characters in t
5、he assignment statement position:=initial+rate*60,would be grouped into the following tokens:,1 The identifier position.2 The assignment symbol:=3 The identifier initial 4 The plus sign.5 The identifier rate.6 The multiplication sign.7 The number 60.The blanks separating tee characters of these toke
6、ns would normally be eliminated during lexical analysis.,Syntax Analysis,Hierarchical analysis is called parsing or syntax analysis.It involves grouping the tokens of the source program into grammatical phrases that are used by the synthesize output.,Semantic Analysis and Intermediate Code Generatio
7、n,The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code-generation phase.After syntax and semantic analysis,some compilers generate an explicit intermediate representation of the source program.,Code Optimization,The code optim
8、ization phase attempts to improve the intermediate code.,Code Generation,The final phase of the compiler is generation of target code,consisting normally of relocatable machine code or assemble code.,3.The Phases of a Compiler,source program,target program,syntax analyzer,semantic analyzer,intermedi
9、ate code generator,code optimizer,code generator,symbol-table manger,error handler,lexical analyzer,Chapter 2 Context-Free Grammars,Every programming language has rules that prescribe the syntactic structure of well-formed programs.Many programming language constructs have an inherently recursive st
10、ructure that can be defined by context-free grammars.A context free grammar consists of terminals,nonterminals,a start symbol,and productions.,G=(VT,VN,S,P)VT,terminals are the basic symbols from which strings are formed.For example,if,then,and else is a terminal.VN,nonterminals are syntactic variab
11、les that denote sets of strings.,S,start symbol,It is a nonterminal and denotes is the language defined by the grammar.P,the productions of a grammar specify the manner in which the terminals and nonterminals can be combined to form strings.Each production consists of a nonterminal,followed by an ar
12、row(sometimes the symbole:=is used in place of the arrow),followed by a string of nonterminals and terminals.,Notational Conventions,To avoid always having to state that“these are the terminals”,“these are the nonterminals”,and so on,we shall employ the following notational conventions with regard t
13、o grammars throughout the remainder of this book.,1.These symbols are terminals:i)Lower-case letters early in the alphabet such as a,b,c.ii)Operator symbols such as+,-,etc.iii)Punctuation symbols such as parentheses,comma,etc.iv)The digits 0,1,9.v)Boldface string such as id or if.,2.These symbols ar
14、e nonterminals:i)Upper-case letter early in the alphabet such A,B,C.ii)The letter S,which,when it appears,is usually the start symbol.iii)Lower-case italic names such as expr or stmt.,3.Lower-case Greek letters,for example,represent strings of grammar symbols Thus,a generic production could be writt
15、en as A,indicating that there is a single nonterminal A on the left of the arrow(the left side of the production)and a string of grammar symbols to the right of the arrow(the right side of the production).,4.If A1,A2,Ak,are all productions with A on the left(we calla productions),we may write A1|2|k
16、.5.Unless otherwise stated,the left side of the first production is the start symbol.,Example 2.1,The grammar of simple arithmeticexpressions.EE+E|E*E|(E)|-E|i,Derivations,Consider the following grammar forarithmetic expressions G(2.1):EE+E|E*E|(E)|-E|i,The production E-E signifiers that an expressi
17、on preceded by a minus sign is also an expression.The production can used to generate more complex expressions from simple expressions by allowing use replace any instance of an E by E.,In the simplest case,we can replace asingle E by E.We can describe this bywriting.E=-E Which is read“E derivesE”.T
18、he symbol=means“derives inone step”.,Example 2.2,The string(i+i)can be derived from E.E=-(E)=-(E+E)=-(i+E)=-(i+i)(2.2)Often we wish to say“derives in zero ormore steps”.For this purpose we can use thesymbol=*.,Thus 1.=*for any string,and 2.if=*and=*,then=*.Likewise,we use=+to mean“derivesin one or m
19、ore steps”.,Given a grammar G with start symbol S,we candefine L(G),the language generated by GL(G)=|VT and S=+We say a string of terminals w is in L(G)if andonly if S=+w.The string w is called asentence of G.If two grammars generate the same language,the grammar are said to be equivalent.,Example 2
20、.3.,The string(i+i*i)is a sentence ofgrammar(2.1)because there is thederivationE=(E)=(E*E)=(E+E*E)=(i+E*E)=(i+i*E)=(i+i*i)(2.3),Leftmost derivation,To understand how certain parsers work we need to consider derivations in which only the leftmost nonterminal in any sentential form is replaced at each
21、 step.Such derivations are termed leftmost.,If=by a step in which the leftmostnonterminal in is replaced,we write=lm.Since derivation(2.2)is leftmost,we canrewrite it as:E=lm(E)=lm(E+E)=lm(E+E*E)=lm(i+E*E)=lm(i+i*E)=lm(i+i*i),Rightmost derivation,Analogous definitions hold for Rightmost derivations
22、in which rightmostis replaced at each step.Rightmost derivations are sometimescalled cononical derivations.,Parse Trees,A parse tree may be viewed as a graphical representation for a derivation that filters out the choice regarding replacement order.For example,the parse tree for(i+i)implied by deri
23、vation derivation(2.2)is shown in Fig.2.1.,Fig.2.1.Parse tree for(i+i),E-E(E)E+E i i,Ambiguity,A grammar that produces more than one parse tree for some sentence is said to be ambiguous.Put another way,an ambiguous gram is one that produces more than one leftmost or more than one rightmost derivatio
24、n for the same sentence.,Example 2.3.,The grammar(2.2)is ambiguous.Because there is the sentence i+i*i thathas the two distinct leftmost derivations:E=E+E E=E*E=i+E=E+E*E=i+E*E=i+E*E=i+i*E=i+i*E=i+i*i=i+i*i,The Language Generated by a Grammar,Example 2.4 Consider the grammar(2.2)SAB AaA|a BbB|b The
25、language generated by grammar(2.4):L(G)=ambn|m,n1,Writing A grammar,Grammars are capable of describing most,butnot all,of the syntax of programming languages.Example 2.5.Design a grammar for the following language.L1=anbn|n1 The grammar G1 SaSb|ab generates the language L1,Example 2.6.,Design a gram
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 其它技巧 其它技巧New Compiler PPT 其它 技巧 New
链接地址:https://www.31ppt.com/p-5616454.html