栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

MIT CS143 lecture 01 course overview

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MIT CS143 lecture 01 course overview

课程首页

http://web.stanford.edu/class/cs143/

课程要求 Open the lid of compilers and see inside
  • Understand what they do
  • Understand how they work
  • Understand how to build them
Correctness over performance
  • Correctness is essential in compilers
  • They must produce correct code
  • CS143 is more like CS103+CS110 than CS107
  • Other classes focus on performance (CS149, CS243)
课程目标

You will write your own compiler in 4 parts.

语言是如何实现的? 有两种主要策略
  • Interpreters run your program
  • Compilers translate your program

Compilers dominate low-level languages
  • C, C++, Go, Rust
Interpreters dominate high-level languages
  • Python, Ruby
Some language implementations provide both
  • Java, Javascript, WebAssembly
  • Interpreter + Just in Time (JIT) compiler
编译器架构
  1. Lexical Analysis — identify words
  2. Parsing — identify sentences
  3. Semantic Analysis — analyse sentences
  4. Optimization — editing
  5. Code Generation — translation
词法分析
  • 识别单词:Smallest unit above letters
  • Lexical analyzer divides program text into “words” or “tokens”
Parsing
  • Once words are understood, the next step is to understand sentence structure
  • Parsing = Diagramming Sentences(The diagram is a tree)
Diagramming a Sentence


Parsing program expressions is the same,consider:

If x == y then z = 1; else z = 2; text{If x == y then z = 1; else z = 2;} If x == y then z = 1; else z = 2;

Diagrammed:

Semantic Analysis
  • Once sentence structure is understood, we can try to understand “meaning”
  • Compilers perform limited semantic analysis to catch inconsistencies

一些英语语境中语法分析的例子:

  • Jack said Jerry left his assignment at home.(What does “his” refer to? Jack or Jerry?)
  • Jack said Jack left his assignment at home?(How many Jacks are there? Which one left the assignment?)

编程语言中的语法分析:

  • Programming languages define strict rules to avoid such ambiguities
  • 如下的C++代码会打印4,使用内层的Jack定义
{
	int Jack = 3;
	{
		int Jack = 4;
		cout << Jack;
	}
}
Compilers perform many semantic checks besides variable bindings

例子:

Jack left her homework at home.(Possible type mismatch between her and Jack,Jack是一个男性)

Optimization

Akin to editing:

  • Minimize reading time
  • Minimize items the reader must keep in short-term memory

Automatically modify programs so that they

  • Run faster
  • Use less memory
  • In general, to use or conserve some resource

(CS143课程中并没有涉及到优化的部分)

优化的例子:

X = Y * 0 is the same as X = 0

Code Generation
  • Typically produces assembly code
  • Generally a translation into another language
Intermediate Representations

Many compilers perform translations between successive intermediate languages

All but first and last are intermediate representations (IR) internal to the compiler

IRs are generally ordered in descending level of abstraction

  • Highest is source
  • Lowest is assembly

IRs are useful because lower levels expose features hidden by higher levels:

  • registers
  • memory layout
  • raw pointers
  • etc

But lower levels obscure high-level meaning:

  • Classes
  • Higher-order functions
  • Even loops
  • etc
Issues
  • Compiling is almost this simple, but there are many pitfalls
  • Example: How to handle erroneous programs?

Language design has big impact on compiler:

  • Determines what is easy and hard to compile
  • Course theme: many trade-offs in language design
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号