目录

  • 1 Chapter 1 Introduction
    • 1.1 1.0 Course Contents
    • 1.2 1.1 Basic Conception
      • 1.2.1 Lecture 1
      • 1.2.2 Lecture 2
    • 1.3 1.2 Compiler Structure
      • 1.3.1 Lecture 1
      • 1.3.2 Lecture 2
      • 1.3.3 Lecture 3
    • 1.4 1.3 The Technique of Compiler Developing
  • 2 Chapter 2 Conspectus of Formal Language
    • 2.1 2.1 Alphabets and Strings
      • 2.1.1 Lecture 1
      • 2.1.2 Lecture 2
    • 2.2 2.2 Grammars and its Categories
      • 2.2.1 Lecture 1
      • 2.2.2 Lecture 2
    • 2.3 2.3 Languages and Parse Tree
      • 2.3.1 Lecture 1
      • 2.3.2 Lecture 2
    • 2.4 2.4 Notes of Formal Language
    • 2.5 2.5 Basic Parsing Techniques
      • 2.5.1 Lecture 1
      • 2.5.2 Lecture 2
  • 3 Chapter 3 Finite Automata
    • 3.1 3.1 Formal Definition of FA
      • 3.1.1 Lecture 1
      • 3.1.2 Lecture 2
    • 3.2 3.2 Transition from NDFA to DFA
      • 3.2.1 Lecture 1
      • 3.2.2 Lecture 2
      • 3.2.3 Lecture 3
    • 3.3 3.3 RG and FA
    • 3.4 3.4 Regular Expression & Regular Set
      • 3.4.1 Lecture 1
      • 3.4.2 Lecture 2
  • 4 Chapter 4 Scanner(Lexical Analyzer)
    • 4.1 4.1 Lexical Analyzer and Tokens
      • 4.1.1 Lecture 1
      • 4.1.2 Lecture 2
    • 4.2 4.2 Step for developing a lexical analyzer
    • 4.3 4.3  Dealing with Identifier
    • 4.4 4.4  Using Regular Expressions
    • 4.5 4.5 Using Flex
      • 4.5.1 Lecture 1
      • 4.5.2 Lecture 2
  • 5 Chapter 5 Top-Down Parsing
    • 5.1 5.0 Push Down Automata (PDA, Added)
      • 5.1.1 Lecture 1
      • 5.1.2 Lecture 2
      • 5.1.3 Lecture 3
    • 5.2 5.1 Elimination Left-Recursion
    • 5.3 5.2 LL(k) Grammar
      • 5.3.1 Lecture 1
      • 5.3.2 Lecture 2
    • 5.4 5.3 Deterministic LL(1) Analyzer Construction
    • 5.5 5.4 Recursive-descent (Non-backtracking) parsing
    • 5.6 5.5 复习与结课
      • 5.6.1 Lecture1 结课感言
      • 5.6.2 Lecture 2 关于复习
      • 5.6.3 Lecture 3 习题讲解
  • 6 Chapter 6 bottom-up Parsing and precedence analyzer
    • 6.1 6.1 Bottom-Up Parsing
    • 6.2 6.2 Phrase, Simple Phrase and Handle
    • 6.3 6.3 A Shift-Reduce Parser
    • 6.4 6.4 Some Relations on Grammar
    • 6.5 6.5 Simple Precedence Parsing
    • 6.6 6.6 Operator-Precedence Parsing
      • 6.6.1 Lecture 1
      • 6.6.2 Lecture 2
      • 6.6.3 Lecture 3
    • 6.7 6.7 Precedence Functions and Construction
  • 7 Chapter 7  LR Parsing
    • 7.1 7.1 LR Parsers
      • 7.1.1 Lecture 1
      • 7.1.2 Lecture 2
    • 7.2 7.2 Building a LR(0) parse table
      • 7.2.1 Lecture 1
      • 7.2.2 Lecture 2
    • 7.3 7.3 SLR Parse Table Construction
    • 7.4 7.4 Constructing Canonical LR(1) Parsing Tables
    • 7.5 7.5 LALR Parsing Tables Construction
    • 7.6 7.6 Using Ambiguous Grammars
    • 7.7 7.7 Yacc/Bison Overview
  • 8 Chapter 8 Syntax-Directed Translation
    • 8.1 8.1 Syntax-Directed Translation
      • 8.1.1 Lecture1
      • 8.1.2 Lecture 2
      • 8.1.3 Lecture 3
    • 8.2 8.2 Abstract Syntax Tree
    • 8.3 8.3 Intermediate Representation
      • 8.3.1 Lecture 1
      • 8.3.2 Lecture 2
  • 9 Chapter 9 Run-Time Environment
    • 9.1 9.1 Data Area & Attribute
    • 9.2 Section 9.2~9.4 & Section 9.8~9.9
    • 9.3 9.5 Parameter Passing
    • 9.4 9.6 Stack Allocation
    • 9.5 9.7 Heap allocation
  • 10 Chapter 10 Symbol Tables
    • 10.1 10.1 A symbol Table Class
    • 10.2 10.2 Basic Implementation Techniques
    • 10.3 10.3 Block-structured Symbol Table
    • 10.4 10.4 Implicit Declaration
    • 10.5 10.5 Overloading
  • 11 Chapter 11 Code Optimization
    • 11.1 11.1 Control Flow Graph
    • 11.2 11.2 Redundancies
    • 11.3 11.3 Loop Optimizations
    • 11.4 11.4 Instruction Dispatch
      • 11.4.1 Lecture 1
      • 11.4.2 Lecture 2
  • 12 Chapter 12 Code Generation
    • 12.1 12.1 Code generation issues
    • 12.2 12.2 Simple Stack Machine
    • 12.3 12.3 Register Machine
    • 12.4 12.4 A Simple Code Generator
  • 13 13 Extended Reading扩展阅读1 斯坦福大学公开课
    • 13.1 Lecture 1
    • 13.2 Lecture 2
    • 13.3 Lecture 3
    • 13.4 Lecture 4
    • 13.5 Lecture 5
    • 13.6 Lecture 6
    • 13.7 Lecture 7
    • 13.8 Lecture 8
    • 13.9 Lecture 9
    • 13.10 Lecture 10
    • 13.11 Lecture 11
    • 13.12 More sources
  • 14 14 Extended Reading 2 扩展阅读2 illinois.edu lectures
    • 14.1 Lecture 1 Overview
    • 14.2 Lecture 2 Strings, Languages, DFAs
    • 14.3 Lecture 3 More on DFAs
    • 14.4 Lecture 4 Regular Expressions and Product Construction
    • 14.5 Lecture 5 Nondeterministic Automata
    • 14.6 Lecture 6 Closure properties
    • 14.7 Lecture 7 NFAs are equivalent to DFAs
    • 14.8 Lecture 8 From DFAs/NFAs to Regular Expressions
    • 14.9 Lecture 9 Proving non-regularity
    • 14.10 Lecture 10 DFA minimization
    • 14.11 Lecture 11 Context-free grammars
    • 14.12 Lecture 12 Cleaning up CFGs and Chomsky Normal form
    • 14.13 Lecture 13 Even More on Context-Free Grammars
    • 14.14 Lecture 14 Repetition in context free languages
    • 14.15 Lecture 15 CYK Parsing Algorithm
    • 14.16 Lecture 16 Recursive automatas
    • 14.17 Lecture 17 Computability and Turing Machines
    • 14.18 Lecture 18 More on Turing Machines
    • 14.19 Lecture 19 Encoding problems and decidability
    • 14.20 Lecture 20 More decidable problems, and simulating TM and “real” computers
    • 14.21 Lecture 21 Undecidability, halting and diagonalization
    • 14.22 Lecture 22 Reductions
    • 14.23 Lecture 23 Rice Theorem and Turing machine behavior properties
    • 14.24 Lecture 24 Dovetailing and non-deterministic Turing machines
    • 14.25 Lecture 25 Linear Bounded Automata and Undecidability for CFGs
  • 15 15 Extended Reading3 扩展阅读3 Extended  Reference Books
    • 15.1 15.1 English Text Book
    • 15.2 15.2 编译原理(何炎祥,伍春香,王汉飞 2010.04)
    • 15.3 15.3 编译原理(陈光建主编;贾金玲,黎远松,罗玉梅,万新副主编 2013.10)
    • 15.4 15.4 编译原理((美)Alfred V. Aho等著;李建中,姜守旭译 2003.08)
    • 15.5 15.5 编译原理学习与实践指导(金登男主编 2013.11)
    • 15.6 15.6 编译原理及编译程序构造 第2版(薛联凤,秦振松编著 2013.02)
    • 15.7 15.7 编译原理学习指导(莫礼平编 2012.01)
    • 15.8 15.8 JavaScript动态网页开发案例教程
  • 16 16 中文版课件(pdf)辅助学习
    • 16.1 第1章 引论
    • 16.2 第2章 形式语言概论
    • 16.3 第3章 有穷自动机
    • 16.4 第4章 词法分析
    • 16.5 第5章 自上而下分析
    • 16.6 第6章 优先分析方法
    • 16.7 第7章 自下而上的LR(k)分析方法
    • 16.8 第8章 语法制导翻译法
    • 16.9 第9章 运行时的存储组织与管理
    • 16.10 第10章 符号表的组织与查找
    • 16.11 第11章 优化
    • 16.12 第12章 代码生成
  • 17 17 Extended Reading4 扩展阅读4 Static Single Assignment
    • 17.1 17.1 SSA-based Compiler Design
    • 17.2 17.2 A Simple, Fast Dominance Algorithm (Rice Computer Science TR-06-33870)
    • 17.3 17.3 The Development of Static Single Assignment Form(KennethZadeck-Presentation on the History of SSA at the SSA'09 Seminar, Autrans, France, April 2009)
    • 17.4 17.4 SPIR-V Specification(John Kessenich, Google and Boaz Ouriel, Intel Version 1.00, Revision 12 January 16, 2018)
    • 17.5 17.5 Efficiently Computing Static Single Assignment Form and the Control Dependence Graph
    • 17.6 17.6 Global Value Numbers and Redundant Computations
  • 18 18 Extended Reading4 扩展阅读5 Computer Science
    • 18.1 1 实地址模式和保护模式的理解
    • 18.2 2 实模式和保护模式
    • 18.3 3 实模式和保护模式区别及寻址方式
    • 18.4 计算机专业术语
    • 18.5 Bit Math in c Language
    • 18.6 Auto-generating subtitles for any video file
    • 18.7 Autosub
    • 18.8 C语言中的内联函数(inline)与宏定义(#define)
  • 19 19 相关学习
    • 19.1 龙书、鲸书和虎书
    • 19.2 Complexity
    • 19.3 MPC Complexity
    • 19.4 NP-completeness
    • 19.5 Computational complexity theory
  • 20 20 全球战疫-武汉战疫延伸与扩展
    • 20.1 Extraordinary G20 Leaders’ Summit Statement on COVID-19
    • 20.2 Experts urge proactive measures to fight virus
    • 20.3 covid-19病毒下贫穷国家
    • 20.4 正确理解病亡率、压平曲线、疫情高峰术语
    • 20.5 为什么全球经济可能陷入长期衰退
    • 20.6 为何新冠病毒检测会出现“假阴性”
    • 20.7 在纽约,几乎每个人身边都有人感染病毒
    • 20.8 An Address by Her Majesty The Queen
    • 20.9 Boris Johnson admitted to hospital over virus sympto
    • 20.10 Edinburgh festivals cancelled due to coronavirus
    • 20.11 US set to recommend wearing of masks
    • 20.12 Boris Johnson in self-isolation after catching coronavirus
    • 20.13 Covid-19:The porous borders where the virus cannot be controlled
    • 20.14 当欧洲人开始戴上口罩
    • 20.15 Lockdown and ‘Intimate Terrorism’
    • 20.16 Us Election 2020: Bernie Sanders Suspends Presidential Campaign
    • 20.17 The aircraft carrier being infected with the coronavirus
    • 20.18 Spent to the W.H.O.
    • 20.19 Unemployment
    • 20.20 The beat of a heart the glimmer of a soul
    • 20.21 Coronavirus pandemic: EU agrees €500bn rescue package
    • 20.22 the world after coronavirus冠状病毒之后的世界
  • 21 21 课程思政方案
    • 21.1 21.1 课程思政
    • 21.2 21.2 实施方案
Complexity

计算复杂度

来源:https://blog.csdn.net/yan_jin_feng/article/details/98172281

求极限

n->无穷大:

表达式n:–>无穷大

表达式1/n:—>0

(2n2+2n3+n)/n^3

2/n+2+1/n^2

n–》无穷大的时候:

2/n–》0

1/n^2-》0

2–》常数—》此算法的时间复杂度是n^3

时间复杂度的概念:

执行的次数和同数量级(n最高次方数)取商是常数

,那么同数量级就是这个算法的时间复杂度

时间复杂度的计算过程:

1 算出每一行语句执行的次数

2 求和,推导出一个n的表达式T(n)

3 找出同数量级的表示式(n的最高次方)

4 T(n)/同数量级–>常数,那么同数量级就是此算法的时间复杂度

举例1:

for i in range(n): #n

for j in range(5) : #5n

print(i,j)      #5n

print(100)              #1

T(n)=n+5n+5n+1=11n+1

第三步:同数量级:n

第四步:(11n+1)/n=11

时间复杂度:T(n)=O(n)

举例2:

常数阶

Temp=i;

i=j;

j=temp;

以上三条单个语句的频度均为1,该程序段的执行时间是一个与问题规模n无关

的常数。算法的时间复杂度为常数阶,时间复杂度记作T(n)=O(1)。

如果算法的执行时间不随着问题规模n的增加而增长,即使算法中有上千条语句,其执

行时间也不过是一个较大的常数。此类算法的时间复杂度是O(1)。

举例3:

n阶:

for i in range(n): #n

print(1)       #n

2n–>n

时间复杂度:O(n)

举例4:

n2阶:

for i in range(n): #n

for j in range(n): #n^2

print(1)       #n^2

T(n)=2n^2+n

时间复杂度:O(n)=n^2

举例5:

n^3阶:

for i in range(n): #n

for j in range(n): #n^2

for m in range(n): #n^3

print(1)       #n^3

T(n)=2n3+n2+n

时间复杂度:O(n)=n^3

举例5:

对数阶

i=1; ①

while (i<=n) #x表示它的执行次数

i=i2; ② 每执行一次,都会2,所以运行次数为2的f(n)次方

解:语句1的频度是1,

设语句2的频度是f(n), 则:2^f(n)<=n;f(n)<=log2n

取最大值f(n)= log2n,

时间复杂度:T(n)=O(log2n )

技巧:循环n的时候,循环体里面有了一些条件,让n的执行次数变小,那么就可以认

为是log2n的时间负责度

举例6:

n的对数阶:nlog2n

for i in range(n):

i=1;          ①执行1次

while (i<=n)  #x表示它的执行次数

i=i*2;

时间复杂度:T(n)=O(nlog2n )

时间复杂度常用的记忆法:

1)访问数组中的元素是常数时间操作,或说O(1)操作。

2)一个算法如 果能在每个步骤去掉一半数据元素,如二分检索,通常它就取O(logn)

时间。

3)用strcmp比较两个具有n个字符的串需要O(n)时间。

4)常规的矩阵乘算法是O(n^3),因为算出每个元素都需要将n对 元素相乘并加到一起

,所有元素的个数是n^2。

5)指数时间算法通常来源于需要求出所有可能结果。例如,n个元 素的

6)集合共有2n个子集,所以要求出所有子集的算法将是O(2n)的。指数算

7)法一般说来是太复杂了,除非n的值非常小,因为,在 这个问题中增

8)加一个元素就导致运行时间加倍。

在计算时间复杂度时,我们一般使用的大O表示法,其时间复杂度,从小到大的排序是

(1) < (logn)< (n)< (nlogn)< (n^2)<…< (2^n)< (n!)

时间复杂度越大,时间越多,算的越慢

稳定排序

a=[1,2,-1,-2,3,5,3]

排序:用一个算法排序,排序之后,2个3(任意的多个相等值)的前后相对位置没有

发生变化。换句话说,在排序中,2个3没有进行过交换,那么就叫稳定排序。

————————————————

版权声明:本文为CSDN博主「yan_jin_feng」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/yan_jin_feng/java/article/details/98172281




以下来自:https://www.cnblogs.com/zxhyJack/p/10596735.html

复杂度分析

一、什么是复杂度分析


  1. 数据结构和算法本身解决的是“快”和“省”的问题,衡量算法执行效率的指标就包括复杂度


  2. 复杂度分析包括时间复杂度和空间复杂度分析,时间复杂度是指算法执行的时间与数据规模的关系,空间复杂度是指算法占用的空间与数据规模的关系


二、为什么需要时间复杂度分析


  1. 通过测试、统计、监控,可以的得到算法执行的时间和占用的内存大小,但是测试结果非常依赖测试环境,测试结果受数据规模的影响很大


  2. 我们需要一个不依赖测试环境和数据规模就可以粗略估算算法执行效率的方法


三、如何进行复杂度分析

1.大O复杂度表示法


  • 包括大O时间复杂度表示法和大O空间复杂度表示法


  • 大O时间复杂度表示法表示代码执行时间随数据规模增长的变化趋势,又称渐进时间复杂度



  1. 大O空间复杂度表示法表示代码执行所占的内存空间随数据规模增长的变化趋势,又称渐进空间复杂度


2.时间复杂度分析法则

1). 所有代码的执行时间T(n)与每行代码的执行次数成正比

  $T(n) = O(f(n))$


  • n表示数据规模的大小


  • f(n)表示每行代码执行的次数总和


  • 大O表示代码的执行时间T(n)与f(n)表达式成正比


2). 只关注循环次数最多的一段代码

3). 加法法则:总的时间复杂度等于量级最大的那段代码的时间复杂度

$如果T1(n)=O(f(n)), T2(n)=o(g(n));那么T(n)=T1(n)+T2(n)=max(O(f(n)),O(g(n)))=O(max(f(n),g(n)))$

4). 乘法法则:嵌套代码的复杂度等于嵌套内外代码复杂度的乘积

$如果 T1(n)=O(f(n)),T2(n)=O(g(n));那么 T(n)=T1(n)T2(n)=O(f(n))O(g(n))=O(f(n)*g(n)).$

tips:复杂度分析关键在于“熟练”,无他,唯手熟尔

几种常见时间复杂度实例分析

分为两类:


  • 非多项式量级:$O(2^n)$和$O(n!)$ 低效的算法


  • 多项式量级 除非多项式量级剩下的全是多项式量级


常见的复杂度有低阶到高阶有:$O(1),O(log(n)),O(n),O(nlogn),O(n^2)$

空间复杂度分析

空间复杂度全称渐进空间复杂度,表示算法的存储空间与数据规模之间的增长关系

四、最好、最坏、平均、均摊时间复杂度


  1. 最好情况时间复杂度:在最理想的情况下,执行这段代码的时间复杂度


  2. 最坏情况时间复杂度:在最糟糕的情况下,执行这段代码的时间复杂度


  3. 加权(期望)平均时间复杂度:代码在所有情况下执行次数的加权平均值表示


  4. 均摊时间复杂度:在代码执行的所有复杂度情况中绝大部分是低级别的复杂度,个别情况是高级别复杂度且发生具有时序关系时,可以将高级别复杂度用的时间均摊到低级别时间复杂度上,当低时间复杂度出现的概率远远大于高时间复杂度时,均摊的结果近似等于低时间复杂度。