上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor

CS308 Compiler Theory CS308 Compiler Theory
CS308 Compiler Theor y CS308 Compiler Theory 1

Grading Homework Pop-quizzes 10% ·Programming assignments:20%↓ 。Final exam:70%↑ CS308 Compiler Theory 2
Grading • Homework + Pop-quizzes : 10% • Programming assignments : 20% ↓ • Final exam : 70% ↑ CS308 Compiler Theory 2

Course Outline Introduction to Compiling A Simple Syntax-Directed Translator ·Lexical Analysis ·Syntax Analysis Context Free Grammars Top-Down Parsing,LL Parsing Bottom-Up Parsing,LR Parsing Syntax-Directed Translation Attribute Definitions Evaluation of Attribute Definitions Semantic Analysis,Type Checking Run-Time Organization Intermediate Code Generation ·Code Generation Machine-Independent Optimizations CS308 Compiler Theory 3
Course Outline • Introduction to Compiling • A Simple Syntax-Directed Translator • Lexical Analysis • Syntax Analysis – Context Free Grammars Context Free Grammars – Top-Down Parsing, LL Parsing – Bottom-Up Parsing, LR Parsing • S t yn ax-Di t d T l ti Directed Translation – Attribute Definitions – Evaluation of Attribute Definitions • Semantic Analysis, Type Checking • Run-Time Organization • Intermediate Code Generation Intermediate Code Generation • Code Generation • Machine-Independent Optimizations 3 p p 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) 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) CS308 Compiler Theory 5

Terminology of Languages Alphabet:a finite set of symbols (ASCII characters) String Finite sequence of symbols on an alphabet Sentence and word are also used in terms of string -8 is the empty string -s is the length of string s. ● Language:sets of strings over some fixed alphabet -3 the empty set is a language. -(s}the set containing empty string is a language The set of well-formed C programs is a language -The set of all possible identifiers is a language. Operators on Strings: -Concatenation:xy represents the concatenation of strings x and y.s s =s &s=s -sh =sss..s(n times)s CS308 Compiler Theory 6
Terminology of Languages • Alphabet : a finite set of symbols (ASCII characters) • String : – Finite sequence of symbols on an alphabet – Sentence and ord are also sed in terms of string Sentence and word are also used in terms of string – ε is the empty string – |s| is the length of string s. • Language: sets of strings over some fixed alphabet – ∅ the empty set is a language. – { ε}h ii i i l } t he set containing empty string is a language – The set of well-formed C programs is a language – The set of all possible identifiers is a language. • Operators on Strings: – Concatenation: xy represents the concatenation of strings x and y. s ε = s ε s = s 6 – s n = s s s .. s ( n times) s 0 = ε CS308 Compiler Theory

Operations on languages ·Concatenation: L L2={SiS2I S1E L1 and S2E L2} ·Union -L1UL2={s|s∈L1ors∈L2} ·Exponentiation: -L0={ε}L1=L L2=LL ·Kleene Closure -L-UL i0 ·Positive Closure -=U2 CS308 Compiler Theory 7
Operations on Languages • Concatenation: – L L = { s s | s ∈ L and s ∈ L } 1L 2 { s1s2 | s1 ∈ L1 and s2 ∈ L2 } • Union – L L { | L L } 1 ∪ L 2 = { s| s ∈ L1 or s ∈ L2 } • Exponentiation: – L 0 = { ε} L1 = L L 2 = LL • Kleene Closure Kleene Closure – L* = U ∞ i = 0 i L • Positive Closure – L + = U ∞ i L 7 i =1 CS308 Compiler Theory

Regular Expressions (Rules) Regular expressions over alphabet Reg.Expr Language it denotes 8 {ε} a∈∑ {a} ()|(2) L()UL(2) (1)(2) L(r1)L(2) (r) (L(r)* (r) Lr) ·(r)=(r)r)* ·(r)?=()|E CS308 Compiler Theory 8
Regular Expressions (Rules) Regular expressions over alphabet Σ Reg. Expr Language it denotes ε { ε } a∈ Σ {a} (r ) | (r ) L(r ) ∪ L(r ) 1) | (r 2 ) L(r1) ∪ L(r 2 ) (r1) (r 2) L(r1) L(r 2 ) (r) * (L(r)) * (r) L(r) • (r) + = (r)(r) * • ( r )? = ( r ) | ε 8 ( ) ()| CS308 Compiler Theory

Finite Automata A recognizer for a language is a program that takes a string x,and answers "yes"if x is a sentence of that language,and "no"otherwise. We call the recognizer of the tokens as a finite automaton. A finite automaton can be:deterministic(DFA)or non-deterministic (NFA) This means that we may use a deterministic or non-deterministic automaton as a lexical analyzer. Both deterministic and non-deterministic finite automaton recognize regular sets ·Which one? deterministic-faster recognizer,but it may take more space non-deterministic-slower,but it may take less space Deterministic automatons are widely used lexical analyzers. First,we define regular expressions for tokens;Then we convert them into a DFA to get a lexical analyzer for our tokens. Algorithm1:Regular Expression>NFA>DFA (two steps:first to NFA,then to DFA) Algorithm2:Regular Expression>DFA (directly convert a regular expression into a DFA) CS308 Compiler Theory
Finite Automata • A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence of that language and is a sentence of that language, and “no” otherwise otherwise. • We call the recognizer of the tokens as a finite automaton. • A finite automaton can be: deterministic( ) DFA or non-deterministic ( ) NFA • This means that we may use a deterministic or non-deterministic automaton as a lexical analyzer. • B hd i i i d Both deterministic and non-d i i i fi i i l deterministic finite automaton recognize regular sets. • Which one? – deterministic – faster recog, y p nizer, but it may take more space – non-deterministic – slower, but it may take less space – Deterministic automatons are widely used lexical analyzers. • First we define regular expressions for tokens; Then we convert them into a DFA to First, we define regular expressions for tokens; Then we convert them into a DFA to get a lexical analyzer for our tokens. – Algorithm1: Regular Expression Î NFA Î DFA (two steps: first to NFA, then to DFA) Al ith 2: Re l E e i Î DFA (di e tl e t e l e e i i t DFA) 9 – Algorithm2: Regular Expression Î DFA (directly convert a regular expression into a DFA) CS308 Compiler Theory

Non-Deterministic Finite Automaton (NFA) A non-deterministic finite automaton (NFA)is a mathematical model that consists of: -S-a set of states ->-a set of input symbols (alphabet) move-a transition function move to map state-symbol pairs to sets of states. So -a start(initial)state F-a set of accepting states(final states) 8-transitions are allowed in NFAs.In other words,we can move from one state to another one without consuming any symbol. A NFA accepts a string x,if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x. CS308 Compiler Theory 10
Non-Deterministic Finite Automaton (NFA) • A non-deterministic finite automaton (NFA) is a mathematical model that consists of: that consists of: – S - a set of states – Σ - a set of in p y (p ) ut s ymbols (al phabet ) – move – a transition function move to map state-symbol pairs to sets of states. – s0 - a start (initial) state – F – a set of accepting states (final states) a set of accepting states (final states) • ε - transitions are allowed in NFAs In other words we can move from transitions are allowed in NFAs. In other words, we can move from one state to another one without consuming any symbol. • A NFA accepts a string x if and only if there is a path from the starting A NFA accepts a string x, if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x. CS308 Compiler Theory 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Bottom-Up Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Top-Down Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第七周讲义_Code Generation Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_A Simple Syntax-Directed Translator.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Introduction to Compilin.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_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
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_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
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 2:Solution of nonlinear equations.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 3:Polynomial Interpolation.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 4:Numerical differentiation and integration.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.pdf