上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Introduction to Compilin

Introduction to Compiling CS308 Compiler Theory
Introduction to Compilin g CS308 Compiler Theory 1

COMPILERS A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. source program COMPILER target program Normally a program written in Normally the equivalent program in a high-level programming language) machine code-relocatable object file) error messages A compiler is a bridge. CS308 Compiler Theory 2
COMPILERS • A compiler is a program takes a program written in a source language and l ii i l i l d translates it into an equivalent program in a target language. source program COMPILER target program ( Normally the equivalent program in ( Normally a program written in a high-level programming language) ( Normally the equivalent program in machine code – relocatable object file) error messages • A compiler is a bridge. CS308 Compiler Theory 2

Compiler Architecture In more detail: Intermediate Language Source Front End- Back End- 一Target Language Language language specific machine specific Analysis Synthesis .Separation of Concerns .Retargeting CS308 Compiler Theory 3
Compiler Architecture In more detail: Intermediate Language Front End – language specific Back End – machine specific Source Language Target Language Analysis Synthesis •Separation of Concerns •Retar getin g 3 g g CS308 Compiler Theory

Phases of A Compiler Source LexicalSyntax Semantic Intermediate Code Code Target Program AnalyzerAnalyzer Generator Optimizer GeneratorProgram Each phase transforms the source program from one representation into another representation. They communicate with error handlers. They communicate with the symbol table. CS308 Compiler Theory 4
Phases of A Compiler Lexical Analyzer Semantic Analyzer Syntax Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Source Program • Each phase transforms the source program from one representation into another re presentation. • They communicate with error handlers. • They communicate with the symbol table. CS308 Compiler Theory 4

Lexical Analyzer Lexical Analyzer reads the source program character by character and returns the tokens of the source program. A token describes a pattern of characters having same meaning in the source program.(such as identifiers,operators,keywords,numbers, delimeters and so on) Ex: newval :oldval 12 => tokens: newval identifier = assignment operator oldval identifier + add operator 12 a number Puts information about identifiers into the symbol table. Regular expressions are used to describe tokens(lexical constructs). A (Deterministic)Finite State Automaton can be used in the implementation of a lexical analyzer. CS308 Compiler Theory 5
Lexical Analyzer • Lexical Analyzer reads the source program character by character and ret rns the returns the t ko ens of the so rce program of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, source program. (such as identifiers, operators, keywords, numbers, delimeters and so on) Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number • Puts information about identifiers into the symbol table. • R l i d t d ib t k (l i l t t ) Regular expressions are used to describe tokens (lexical constructs). • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer. 5 implementation of a lexical analyzer. CS308 Compiler Theory

Syntax Analyzer A Syntax Analyzer creates the syntactic structure(generally a parse tree)of the given program. A syntax analyzer is also called as a parser. .A parse tree describes a syntactic structure. assgstmt identifier expression In a parse tree,all terminals are at leaves. newval expression expression All inner nodes are non-terminals in a context free grammar. identifier number oldval 12 CS308 Compiler Theory 6
Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree) fh i o t e given program. • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier := expression • In a parse tree, all terminals are at leaves. newval expression + expression All i d t i li identifier number • All inner nodes are non-terminals in a context free grammar. oldval 12 CS308 Compiler Theory 6

Syntax Analyzer (CFG) The syntax of a language is specified by a context free grammar (CFG). The rules in a CFG are mostly recursive. A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. If it satisfies,the syntax analyzer creates a parse tree for the given program. Ex:We use BNF(Backus Naur Form)to specify a CFG assgstmt -identifier :=expression expression -identifier expression -number expression -expression expression CS308 Compiler Theory 7
Syntax Analyzer (CFG) • The syntax of a language is specified by a context free grammar ( ) CFG ). • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies the syntax analyzer creates a parse tree for the given program If it satisfies, the syntax analyzer creates a parse tree for the given program. • Ex: We use BNF (Backus Naur Form) to specify a CFG We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier := expression ex pression -> identifie r expression -> number expression -> expression + expression CS308 Compiler Theory 7

Syntax Analyzer versus Lexical Analyzer Which constructs of a program should be recognized by the lexical analyzer,and which ones by the syntax analyzer? Both of them do similar things;But the lexical analyzer deals with simple non-recursive constructs of the language. The syntax analyzer deals with recursive constructs of the language. The lexical analyzer simplifies the job of the syntax analyzer. The lexical analyzer recognizes the smallest meaningful units(tokens)in a source program. -The syntax analyzer works on the smallest meaningful units(tokens)in a source program to recognize meaningful structures in our programming language CS308 Compiler Theory 8
Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical anal d hi h b h l lyzer, an d whi c h ones by t he syntax ana lyzer? – Both of them do similar things; But the lexical analyzer deals with simple non-recursive constructs of the lan g g ua ge. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The le ical anal er recogni es the smallest meaningf l nits (tokens) in a so rce program The le xical analyzer recogni zes the smallest meaningful units (tokens) in a so urce program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. CS308 Compiler Theory 8

Parsing Techniques Depending on how the parse tree is created,there are different parsing techniques. These parsing techniques are categorized into two groups: Top-Down Parsing, Bottom-Up Parsing ● Top-Down Parsing: Construction of the parse tree starts at the root,and proceeds towards the leaves -Efficient top-down parsers can be easily constructed by hand. Recursive Predictive Parsing,Non-Recursive Predictive Parsing(LL Parsing). ·Bottom-Up Parsing: -Construction of the parse tree starts at the leaves,and proceeds towards the root. -Normally efficient bottom-up parsers are created with the help of some software tools. Bottom-up parsing is also known as shift-reduce parsing. Operator-Precedence Parsing-simple,restrictive,easy to implement LR Parsing-much general form of shift-reduce parsing,LR,SLR,LALR CS308 Compiler Theory 9
Parsing Techniques • Depending on how the parse tree is created, there are different parsing techni ques. • These parsing techniques are categorized into two groups: – Top-Down Parsing, – Bottom-Up Parsing • Top-Down Parsing: – Construction of the parse tree starts at the root, and proceeds towards the leaves. – Efficient top-down parsers can be easily constructed by hand. – Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). • Bottom-Up Parsing: – Construction of the parse tree starts at the leaves, and proceeds towards the root. – Normally efficient bottom Normally efficient bottom -up parsers are created with the help of some software tools up parsers are created with the help of some software tools. – Bottom-up parsing is also known as shift-reduce parsing. – Operator-Precedence Parsing – simple, restrictive, easy to implement LR P i h l f f hif d i LR SLR LALR 9 – LR Parsing – muc h general form o f shift-re duce parsing, LR, SLR, LALR CS308 Compiler Theory

Semantic Analyzer A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. Type-checking is an important part of semantic analyzer. 。 Normally semantic information cannot be represented by a context-free language used in syntax analyzers. Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules) the result is a syntax-directed translation, -Attribute grammars ·EX: newval :oldval 12 The type of the identifier newval must match with type of the expression (oldval+12) CS308 Compiler Theory 10
Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and coll h i f i f h d i llects the type information for the code generation. • Type-checking is an important part of semantic analyzer. • Normally semantic information cannot be represented by a context-free language used in syntax analyzers. • Ctt on ex -f d i th t l i i t t d ith free grammars used in the syntax analysis are integrated with attributes (semantic rules) – the result is a syntax the result is a syntax-directed translation directed translation, – Attribute grammars • Ex: newval := oldval + 12 • The type of the identifier newval must match with type of the expression (oldval+12) 10 The type of the identifier newval must match with type of the expression (oldval 12) CS308 Compiler Theory
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Parameter Passing Mechanisms.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_CS308 Compiler Theory.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT15 并发与多线程.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT14 Python GUI工具包:Tkinter.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT13 算法设计分析.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT12 面向对象设计.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT11 数据集合体.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT10 类的定义.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT09 模拟设计.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT08 控制结构——循环语句.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT07 控制结构——条件语句.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT06 函数.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT05 面向对象与图形编程.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT04 字符串计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT03 数值计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT02 程序构件.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT01 绪论.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(课程教材)简明Python教程(PDF版).pdf
- 《程序设计思想与方法》课程教学资源(课程教材)Python Programming:An Introduction to Computer Science.pdf
- 《程序设计思想与方法》课程教学资源(课程教材)How to Think Like a Computer Scientist(Learning with Python).pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_A Simple Syntax-Directed Translator.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第七周讲义_Code Generation Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Top-Down Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Bottom-Up Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_Machine-Independent Optimizations Ⅳ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Lexical Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Syntax Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_lex.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第五周讲义_Type Checking.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅰ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅲ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Intermediate Code Generation.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Heap Management.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Run-Time Environments.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第四周讲义_Syntax-Directed Translation.pdf
- 上海交通大学:《计算机辅助设计》教学资源_Product Visualization.doc
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 1:Introduction, calculus review and computer representation of numbers.pdf