(1.1.4)--Samuel's Ch1_2编译原理与实践英文版.ppt
《(1.1.4)--Samuel's Ch1_2编译原理与实践英文版.ppt》由会员分享,可在线阅读,更多相关《(1.1.4)--Samuel's Ch1_2编译原理与实践英文版.ppt(34页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 1 Chapter 1 Introduction Introduction Compilers are computer programs that translate one language to another.Very complex program from 10,000 to 1,000,000 lines of code.Its input is a program written in its source language.It produces an equivalent program written in its target language.Comp
2、ilerS2CompilersCompilersCompilerS3Translation ProcessTranslation ProcessThis is a book.Step 1:lexical analysisThis/is/a/book/.Step 2:syntax analysisThis subjectis predicatea quantifierbook object.endStep 3:semantic analysisThis pronoun 这is copula 是a numeral 一book noun 书.period 。Step 4:This is a book
3、.Step 5:这是一书。Step 6:这是一本书。CompilerS4Translation ProcessTranslation ProcessScannerParserSemantic AnalyzerSource Code OptimizerCode GeneratorTarget Code OptimizerSource CodeLiteral TableSymbol TableError Handler123456Target CodeCompilerS5Translation ProcessTranslation ProcessScannerParserSemantic Anal
4、yzerSource Code OptimizerCode GeneratorTarget Code OptimizerSource CodeLiteral TableSymbol TableError Handler1Reads the source program(stream of characters).Performs lexical analysis:collects sequences of characters into meaningful units called tokens.Example:aindex=4+2 a identifier left bracket ind
5、ex identifier right bracket =assignment 4 number +plus sign 2 numberCompilerS6The ScannerThe ScannerCompilerS7Translation ProcessTranslation ProcessScannerParserSemantic AnalyzerSource Code OptimizerCode GeneratorTarget Code OptimizerSource CodeTokensLiteral TableSymbol TableError Handler2Receives t
6、he source in form of tokens.Performs syntax analysis determines the structure of the program similar to performing grammatical analysis on a sentence in natural language.The result is represented as a parse tree or a syntax tree.CompilerS8The ParserThe ParserCompilerS9Parse TreeParse Tree expression
7、 assign-expression expression expression=subscript-expression additive-expression expression expression expression expression+identifieraidentifierindexnumber 4number 2aindex=4+2CompilerS10Abstract Syntax Tree Abstract Syntax Tree An abstract syntax tree is a condensation of the information containe
8、d in a parse tree.assign-expression subscript-expression additive-expressionidentifieraidentifierindexnumber 4number 2aindex=4+2CompilerS11Translation ProcessTranslation ProcessScannerParserSemantic AnalyzerSource Code OptimizerCode GeneratorTarget Code OptimizerSource CodeTokensSyntax TreeLiteral T
9、ableSymbol TableError Handler3The semantics of a program are its“meaning”.The semantics of a program determine its runtime behavior.Most programming languages have features(called static semantics)that can be determined prior to execution.Typical static semantics featuresDeclarationsType checkingThe
10、 extra information computed by the semantic analyzer are called attributes.They are added to the tree as annotations,or“decorations”CompilerS12The Semantic AnalyzerThe Semantic AnalyzerCompilerS13Abstract Syntax Tree Abstract Syntax Tree An abstract syntax tree is a condensation of the information c
11、ontained in a parse tree.assign-expression subscript-expression additive-expressionidentifieraidentifierindexnumber 4number 2aindex=4+2CompilerS14Abstract Syntax Tree Abstract Syntax Tree assign-expression subscript-expression additive-expressionidentifieraidentifierindexnumber 4number 2aindex=4+2Co
12、mpilerS15Annotated TreeAnnotated Tree assign-expression subscript-expression integer additive-expressioninteger identifieraarray of integeridentifierindexintegernumber 4integernumber 2integeraindex=4+2CompilerS16Translation ProcessTranslation ProcessScannerParserSemantic AnalyzerSource Code Optimize
13、rCode GeneratorTarget Code OptimizerSource CodeTokensSyntax TreeAnnotated TreeLiteral TableSymbol TableError Handler4The earliest point at which optimization steps can be performed is just after semantic analysis.There may be possibilities that depend only on the source code.Compilers exhibit a wide
14、 variation in the kind of optimization and its placement.The output of the source code optimizer is the intermediate representation(IR)or intermediate code.CompilerS17The Source Code OptimizerThe Source Code Optimizer 4+2 can be precomputed by the compiler.This optimization is known as constant fold
15、ing.This optimization can be performed on the annotated syntax tree by collapsing the right hand subtree to its constant value.CompilerS18Example Example assign-expression subscript-expression integer additive-expressioninteger identifieraarray of integeridentifierindexintegernumber 4integernumber 2
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 1.1.4-Samuel's Ch1_2编译原理与实践英文版 1.1 Samuel Ch1_2 编译 原理 实践 英文
限制150内