斯坦福cs231(编译原理)の 1 Introduction

Introduction

Compiler vs Interpreter

image-20230625222308600

History of Compiler

很喜欢的一句话:

When hardware cost the most in absolute and relative terms more than they would ever cost again already, the software was the dominant expense. And in making good use out of computers.

当硬件占据了绝对成本,就该考虑软件了。

FORTRAN:

The first compiler: Huge impact on computer science

Led to an enormous body of theoretical work

Modern compilers preserve the outline of FORTRAN

现代编译器一般由以下组成:

  • Lexical Analysis
  • Parsing
  • Semantic Analysis
  • Optimization
  • Code Genderation

First Step: recognize words — Smallest unit above letters

This is a sentence

Lexical analysis divides program text into "words" or "tokens".

If x==y then z = 1; else z = 2;

编译器如何知道第一个双等于不是两个分开的独立的等于号?

Once words are understood, the next step is to understand sentence structure

Parsing = Digramming Sentences — The diagram is a tree

看一个自然语言的例子

image-20230625224323367

看一个编程语言的例子:

image-20230625224931669

一旦句子的结构清晰了,就可以尝试去理解它的意思,compilers perform limited semantic analysis to catch inconsistencies.

🌰:

1
2
3
4
5
Jack said Jerry left his assignments at home
这个句子里his指代不明确

Jack said Jack left his assignment at home ?
这个句子里更糟糕了,his可能指第一个Jack,当然也可能是第二个Jack,还有可能是其他人

类比编程语言:定义了作用域限制了二义性

1
2
3
4
5
6
7
{
int Jack = 3;
{
int Jack = 4;
cout << Jack;
}
}

Compiler perform many semantic checks besides bariable bindings

eg:

1
Jack said Jerry left her assignments at home

a tyoe mismatch between her and Jack, we know thet are different people

优化在自然语言中该户没有对应的部分,但是有点像简写;优化程序的目的是:运行速度更快,使用内存更少

简写不是随便的:

1
2
x = Y * 0 => X = 0
这是不正确的,Y如果是NaN,那么X应该仍然是NaN,而不是0

代码生成通常指汇编代码生成,有点类型自然语言里的"翻译"

现代编译器大多是这样的轮廓,只不过各部分所占据的比重不一样,现代编译器更注重优化:

image-20230625230214905

Economy of 编译器

Why are there so many programming langs ?

Why are there new programming langs ?

Why is a good programming lang ?

Why are there so many programming langs ?

不同的领域有着不同的语言需求

  • 比如科学计算,需要好的浮点数处理,快速的矩阵运算和并行,代表语言:FORTRAN(formula translation,公式翻译)

  • 商业应用:需要数据分析和报表,代表语言:sql

  • 系统编程:需要实时性,与操作系统打交道,C/C++

Why are there new programming langs

  • 程序员的训练是一门语言的主要成本
  • 很容易开始一门新的语言
  • 新的领域产生,新的语言更适用于新的领域

Why is a good programming lang ?

没有统一的标准去规定哪种语言是最好的语言

Cool语法

wip


斯坦福cs231(编译原理)の 1 Introduction
https://mingmingjiang1.github.io/emocoder/2023/06/10/编译原理/编译原理一/
作者
迷途知返
发布于
2023年6月10日
许可协议