编译技术1-1


1. Course Information 课程信息
Course Overview 课程概述
• Techniques of Compiler introduces students to the principles and technologies of programming language implementation.

• 《编译技术》 向学生介绍编程语言实现的原理和技术。

• Topics covered:

• Lexical analysis (词法分析)

• Parsing theory / Syntax analysis (语法分析)

• Semantic analysis (语义分析)

• Code generation (Intermediate code generation) (代码生成,中间代码生成)

Grading Policy 评分标准
| Component 组成部分 | Weight 权重 |
|-------------------|------------|
| Assignments & Attendance 作业与考勤 | 10% |
| Coding Experiments 编程实验 | 20% |
| Final Exam 期末考试 | 70% |


2. The History of Programming Languages and Compilers 编程语言与编译器的历史
Why Compiler? 为什么需要编译器?
• Evolution of Programming Languages 编程语言的发展

Language Type 语言类型 Example 示例
Machine Language (机器语言) C7 06 0000 0002 (Binary/Hex)
Assembly Language (汇编语言) MOV x, 2
High-Level Language (高级语言) x = 2;

• Key Problem 关键问题

• Computers can only execute machine instructions (计算机只能执行机器指令).

• High-level programs must be translated into machine code (高级语言程序必须被翻译成机器代码).

What is a Compiler? 什么是编译器?
• A compiler is a program that translates source code (high-level language) into target code (machine/assembly language).

• 编译器 是将 源代码(高级语言)转换为 目标代码(机器/汇编语言)的程序。

• Input (输入): High-level language (e.g., C, C++, Java).

• Output (输出): Object code (e.g., machine language, assembly).


3. The Structure of a Compiler 编译器的结构
A compiler consists of several phases:
编译器包含多个 阶段:

  1. Lexical Analysis (词法分析)
    • Converts source code into tokens (e.g., keywords, identifiers, operators).

    • 将 源代码 转换为 词法单元(如关键字、标识符、运算符)。

  2. Syntax Analysis (语法分析)
    • Checks if tokens follow the grammar rules of the language (e.g., using parsing).

    • 检查词法单元是否符合语言的 语法规则(如使用 语法分析)。

  3. Semantic Analysis (语义分析)
    • Ensures the program is meaningful (e.g., type checking, scope resolution).

    • 确保程序 有意义(如类型检查、作用域解析)。

  4. Intermediate Code Generation (中间代码生成)
    • Produces an intermediate representation (e.g., three-address code).

    • 生成 中间表示(如三地址码)。

  5. Code Optimization (代码优化)
    • Improves efficiency (e.g., removing redundant operations).

    • 提高效率(如删除冗余操作)。

  6. Code Generation (代码生成)
    • Converts intermediate code into machine code.

    • 将中间代码转换为 机器代码。


4. Language Processing System 语言处理流程
Steps in Compilation 编译步骤
1. Source Code (源代码) → Preprocessor (预处理器) → Modified Source (预处理后代码)
2. Modified Source → Compiler (编译器) → Assembly Code (汇编代码)
3. Assembly Code → Assembler (汇编器) → Machine Code (机器代码)
4. Machine Code → Linker (链接器) → Executable (可执行文件)

Interpreter vs. Compiler 解释器 vs. 编译器
| Feature 特点 | Compiler (编译器) | Interpreter (解释器) |
|-------------|----------------------|------------------------|
| Execution (执行方式) | Translates entire program before execution (先翻译整个程序再执行) | Translates & executes line by line (逐行翻译并执行) |
| Speed (速度) | Faster execution (执行更快) | Slower execution (执行较慢) |
| Error Detection (错误检测) | Errors detected after full compilation (全部编译后检测错误) | Errors detected during execution (执行时检测错误) |
| Examples (例子) | GCC (C), Clang (C++), Java Compiler (javac) | Python, JavaScript, Ruby |


Summary 总结
• Compilers bridge the gap between high-level languages and machine code.

• 编译器 连接 高级语言 和 机器代码。

• Key phases: Lexical, Syntax, Semantic Analysis, Code Generation.

• 关键阶段:词法分析、语法分析、语义分析、代码生成。

• Understanding compilers helps in writing efficient code and debugging.

• 理解编译器有助于 编写高效代码 和 调试。

This course will provide hands-on experience in building a simple compiler!
本课程将提供 构建简单编译器 的实践机会!