Overview

Overview

70%lab 30%final exam

what is a compiler

把一种高级语言转换成另一种高级语言,比如C->Rust 称为transpiler

先把代码转换成bytecode,再放到对应的virtual machine上执行

interpreter不将一种高级语言转换成另一种语言,而是直接在源语言中解释状态

linker链接

compiler组成

  • Front End
    • Lexical Analysis 词法分析器
    • Syntax Analysis 语法
    • Semantic Analysis 语义
  • IR(Intermediate Representation 中间表示)
  • Optimiser
  • IR
  • Back End
    • Register Allocation
    • Instruction Selection
    • Instruction Scheduling

Lexical Analysis -> Syntax Analysis -> Semantic Analysis

Front End: Lexical Analysis

Find lexemes according to patterns, and create tokens

  • Lexeme a character string
  • Pattern regular expression(lexical errors if no patterns matched)
  • Token <token-class, attribute> attribute distinguishes tokens in the same class

Front End: Syntax Analysis

Create the (abstract ) syntax tree (AST)生成语法树

Front End: Semantic Analysis

Passing syntax analysis does not mean the program is valid

Semantic analysis checks correct meaning and decorates AST

Front End Generation

Middle End: Optimizations

可以优化代码

Back End: Instruction Selection

Translate IR to machine code

Back End: Register Allocation