【编译原理】Compiler_Week 1_Stanford University

学习课程的目的

想成为一名优秀的计算机专业本科生,应该对程序编译有所了解。通过这节课学习编译器的构造,理解程序编译的实现过程,实现一个编译器,提升自己的能力。

课程源地址

第一周 介绍和Cool编译器

Week 1: Introduction & the Cool Programming Language

编译器和解释器的区别

The difference between Compilers and Interpreters

在这里插入图片描述

FORTRAN I

The introduction of FORTRAN I
  1. The first compiler.
    — Huge impact on computer science
  2. Led to an enormous body of theoretical work.
  3. Modern compilers preserve the outline of FORTRAN I.
The structure of FORTRAN I
  1. Lexical Analysis
  2. Parsing
  3. Semantic Analysis
  4. Optimization
  5. Code Generation
Explain the above structure
  1. 词法分析将程序分解成“单词”或“符号”
    Lexical analysis divides program text into “words” or “tokens”
  2. 解析 = 图解句子
    Parsing = Diagramming Sentences
    — The diagram is a tree
    Parsing English text
  3. 编译器执行有限的语法分析来捕获不一致性
    Compilers perform limited semantic analysis to catch inconsistencies
    Inconsistencies in English text
    编程语言定义了严格的规则来避免这些歧义性。
    Programming languages define strict rules to avoid such ambiguities.
    ambiguities
  4. 优化过程在英语中没有十分对应的过程
    Optimization has no strong counterpart in English
    Optimization
    自动修改程序以便运行更快使用更少内存
    Automatically modify programs so the they
    Run faster
    Use less memory
    Optimization
  5. 代码生成通常生成汇编代码
    Code Generation usually produce assembly code
    从FORTRAN到现代编译器的改变。
    The changes from FORTRAN to the modern compilers.
    changes

编程语言的经济性

The economy of programming languages

为什么有如此多的编程语言?

Why are there s many programming languages?

应用域具有独特或冲突的需要。
Application domains have distinctive/conflicting needs.

为什么有新的编程语言?

Why are there new programming languages?

  1. 广泛使用的语言改变缓慢。
    Widely used languages are slow to change.
  2. 开始一门新语言很简单。
    Easy to start a new language.
  3. 填补空白的语言。
    Languages adopted to fill a void.

声明:程序员培训是编程语言的主要成本。
Claim: Programmer training is the dominant cost for a programming language.

什么是一个好的编程语言?

What is a good programming language?

语言设计没有普遍接受的度量标准。
There is no universally accepted metric for language design.

COOL Overview

cool的VM安装方式(推荐使用这个)

cool的linux安装方式

The Introduction of COOL
  1. Classroom Object Oriented language
  2. Design to be implementable in a short time
  3. Give a taste of implementation of modern
    (1) Abstraction
    (2) Static typing
    (3) Reuse(inheritance)
    (4) Memory management
    And more …
  4. But many things are left out
My first COOL programming
class Main{
	main():Int { 1 };
}; 
(*需要返回值*)
class Main {
	i : IO <- new IO;
	main():IO { i.out_string("Hello World!\n") };
}
(*返回类型需要相等*)
class Main {
	i : IO <- new IO;
	main():Object { i.out_string("Hello World!\n") };
}
(*Object类型也可以*)
class Main {
	main():Object { (new IO).out_string("Hello World!\n") };
}
class Main inherits IO{
	main():Object { self.out_string("Hello World!\n") };
}
(*继承写法*)
COOL example II
class Main {
	main():Object {
		(new IO).out_string((new IO).in_string().concat("\n\n"))
	};
};
(*从键盘输入字符串,输出该字符串*)
class Main inherits A2I{
	main():Object {
		(new IO).out_string(i2a(a2i((new IO).in_string())+1).concat("\n\n"))
    };
};
(*
 *将输入的字符串转换为数字串,输出数字串+1的字符串
 *注:编译的时候需要将atoi.cl一同编译,Main类继承
 *)
class Main inherits A2I{
    main():Object {
        (new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n\n"))
    };

    fact(i: Int): Int {
        if (i = 0) then 1 else i * fact(i-1) fi
    };
};
(*求阶乘递归写法*)
class Main inherits A2I{
    main():Object {
        (new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n\n"))
    };
	
	fact(i: Int): Int {
		let fact: Int <- 1 in { (*fact 初始化为1*)
			while (not (i = 0)) loop
				{
					fact <- fact * i; (*<-为赋值,=为比较*)
					i <- i - 1;
				}
			pool;
			fact;
		}
	};
};
(*求阶乘无递归写法*)
COOL example III
class List inherits A2I {
    item: Object;
    next: List;
    
    init(i: Object, n: List): List {
       {
            item <- i;
            next <- n;
            self;
        }
    };

    flatten(): String {
       let string: String <-
           case item of
                i: Int => i2a(i);
                s: String => s;
                o: Object => {  abort(); ""; };
            esac
       in
            if (isvoid next) then
                string
            else
            	string.concat(next.flatten())
            fi
    };
};

class Main inherits IO {
    main():Object {
       let hello: String <- "Hello ",
       		world: String <- "World!",
      		newline: String <- "\n",
       		nil: List,
       		list: List <-
            	(new List).init(hello,
            	(new List).init(world,
            	(new List).init(newline, nil)))
        in
                out_string(list.flatten())
    };
};
(*该样例程序比较难,跟着视频能收获更多*)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值