# compile_theory_practice **Repository Path**: xenogene/compile_theory_practice ## Basic Information - **Project Name**: compile_theory_practice - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-14 - **Last Updated**: 2025-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C-- 编译器项目(with简易Py测试前端) ## 本项目为天津大学编译原理课程大作业。 ## 本项目开发环境 *Windows Linux Subsystem Ubuntu 22.04* ## 目录 1. [快速开始](#快速开始) 2. [项目概览](#项目概览) 3. [功能说明](#功能说明) 4. [构建和运行](#构建和运行) 5. [开发报告](#开发报告) 6. [测试报告](#测试报告) --- ## 快速部署 ### 环境 - C++17编译器(GCC/Clang/MSVC) - CMake 3.10+ ### 编译&前端启动 ```bash cd build cmake .. make cd .. ``` ```bash cd /mnt/e/计算机/compile_theory_practice source .venv/bin/activate python3 web_app.py ``` ### 运行 ./build/compiler tests/17case.sy output/17case.ll ```bash 用例测试 # 基础四个测试 ./build/compiler tests/test_simple.sy output/output_simple.ll ./build/compiler tests/test_input.sy output/output_input.ll ./build/compiler tests/test_var.sy output/output_var.ll ./build/compiler tests/test_func.sy output/output_func.ll # 类型相关 ./build/compiler tests/char.sy output/char.ll ./build/compiler tests/const.sy output/const.ll ./build/compiler tests/float.sy output/float.ll # 结构体 / 联合体 / switch ./build/compiler tests/struct.sy output/struct.ll ./build/compiler tests/struct2.sy output/struct2.ll ./build/compiler tests/union.sy output/union.ll ./build/compiler tests/switch.sy output/switch.ll # sample1 ~ sample10 ./build/compiler tests/sample1.sy output/sample1.ll ./build/compiler tests/sample2.sy output/sample2.ll ./build/compiler tests/sample3.sy output/sample3.ll ./build/compiler tests/sample4.sy output/sample4.ll ./build/compiler tests/sample5.sy output/sample5.ll ./build/compiler tests/sample6.sy output/sample6.ll ./build/compiler tests/sample7.sy output/sample7.ll ./build/compiler tests/sample8.sy output/sample8.ll ./build/compiler tests/sample9.sy output/sample9.ll ./build/compiler tests/sample10.sy output/sample10.ll ./build/compiler tests/sample11.sy output/sample11.ll ./build/compiler tests/sample12.sy output/sample12.ll ./build/compiler tests/sample13.sy output/sample13.ll ./build/compiler tests/sample14.sy output/sample14.ll ./build/compiler tests/sample15.sy output/sample15.ll ./build/compiler tests/sample16.sy output/sample16.ll ./build/compiler tests/sample17.sy output/sample17.ll ./build/compiler tests/sample18.sy output/sample18.ll ./build/compiler tests/sample19.sy output/sample19.ll ./build/compiler tests/sample20.sy output/sample20.ll ./build/compiler tests/sample21.sy output/sample21.ll ./build/compiler tests/sample22.sy output/sample22.ll ./build/compiler tests/sample23.sy output/sample23.ll ./build/compiler tests/sample24.sy output/sample24.ll ./build/compiler tests/sample25.sy output/sample25.ll ./build/compiler tests/sample26.sy output/sample26.ll ./build/compiler tests/sample27.sy output/sample27.ll ./build/compiler tests/sample28.sy output/sample28.ll ./build/compiler tests/sample29.sy output/sample29.ll ./build/compiler tests/sample30.sy output/sample30.ll ./build/compiler tests/sample31.sy output/sample31.ll ./build/compiler tests/sample32.sy output/sample32.ll ./build/compiler tests/sample33.sy output/sample33.ll ./build/compiler tests/sample34.sy output/sample34.ll ./build/compiler tests/sample35.sy output/sample35.ll ./build/compiler tests/sample36.sy output/sample36.ll ./build/compiler tests/sample37.sy output/sample37.ll ./build/compiler tests/sample38.sy output/sample38.ll ./build/compiler tests/sample39.sy output/sample39.ll ./build/compiler tests/sample40.sy output/sample40.ll ./build/compiler tests/sample41.sy output/sample41.ll ./build/compiler tests/sample42.sy output/sample42.ll ./build/compiler tests/sample43.sy output/sample43.ll ./build/compiler tests/sample44.sy output/sample44.ll ./build/compiler tests/sample45.sy output/sample45.ll ./build/compiler tests/sample46_finaltest.sy output/sample46_finaltest.ll ``` ### 查看输出 详见output目录下输出即可。 --- ## 测试用例一览 | 文件名 | 主要测试点 | 复杂程度 | |--------|------------|----------| | `tests/test_simple.sy` | 全局/局部变量、基本类型定义与简单赋值 | 低 | | `tests/test_input.sy` | 函数参数与输入、简单表达式计算 | 低 | | `tests/test_var.sy` | 变量声明、初始化与作用域 | 低 | | `tests/test_func.sy` | 函数定义与调用、返回值处理 | 中 | | `tests/char.sy` | `char` 字面量与 ASCII 整数表示 | 低 | | `tests/const.sy` | `const` 常量声明与初始化 | 低 | | `tests/float.sy` | `float` 常量和表达式(在 IR 中按 i32 处理) | 低 | | `tests/struct.sy` | 多个 `struct` 类型声明与变量定义 | 低 | | `tests/struct2.sy` | `struct`/`union` 组合、成员访问与复杂表达式 | 中 | | `tests/union.sy` | 基础 `union` 类型声明与变量定义 | 低 | | `tests/switch.sy` | `switch/case/default/break` 控制流 | 中 | | `tests/sample1.sy` | 基本类型、常量和简单赋值 | 低 | | `tests/sample2.sy` | 函数参数、常量、单目运算和算术表达式 | 中 | | `tests/sample3.sy` | 逻辑运算 `&&`、`II`,返回表达式 | 中 | | `tests/sample4.sy` | `struct` 内包含函数声明与嵌套 `struct`,仅语法覆盖 | 中 | | `tests/sample5.sy` | 第二个结构体类型及变量 `s` 的声明 | 低 | | `tests/sample6.sy` | 结构体内嵌 `union`/`struct` 字段的语法覆盖 | 中 | | `tests/sample7.sy` | 简单 `union` 声明与变量 `u1,u2` 定义 | 低 | | `tests/sample8.sy` | `union` 内嵌 `union` 与 `struct` 的语法覆盖 | 中 | | `tests/sample9.sy` | 只声明不定义变量的 `union` 类型 | 低 | | `tests/sample10.sy` | `switch` 与函数调用混合控制流 | 中 | | `tests/sample11.sy` | `if/else` + 算术表达式组合 | 低 | | `tests/sample12.sy` | 多层 `if/else if/else`,逻辑运算和表达式 | 中 | | `tests/sample13.sy` | 多函数、函数间调用与返回值表达式 | 中 | | `tests/sample14.sy` | `struct` 成员赋值和读取,算术组合 | 中 | | `tests/sample15.sy` | `union` 成员写入/读取的共享存储语义 | 中 | | `tests/sample16.sy` | 两个 `struct` 类型、独立变量访问内部字段 | 中 | | `tests/sample17.sy` | 多 `case` + `default` 的 `switch` 结构 | 低 | | `tests/sample18.sy` | `struct` 与 `switch` 组合使用 | 中 | | `tests/sample19.sy` | `union` 与复杂算术表达式 `(u.b + x) * (u.a - 1)` | 中 | | `tests/sample20.sy` | `struct` + `union` 混合与复杂表达式(嵌套成员访问语法覆盖) | 高 | | `tests/sample21.sy` | `printInt`/`println` 打印整型表达式结果 | 低 | | `tests/sample22.sy` | `printChar` + `println` 打印字符与换行 | 低 | | `tests/sample23.sy` | 条件分支结果打印 | 低 | | `tests/sample24.sy` | 结构体成员访问 + 打印多个字段和求和 | 中 | | `tests/sample25.sy` | `union` 成员写入/读取 + 打印结果 | 中 | | `tests/sample26.sy` | `if/else` + 比较/逻辑表达式 + 打印 | 中 | | `tests/sample27.sy` | `switch/case/default/break` + 打印 | 中 | | `tests/sample28.sy` | `struct` 成员写入/读取 + 算术表达式 | 中 | | `tests/sample29.sy` | `union` 成员写入/读取 + 打印 | 中 | | `tests/sample30.sy` | `struct`/`union` 类型引用字段(语法覆盖)+ 访问/打印 | 高 | | `tests/sample31.sy` | 多函数 + `struct/union` 形参(语法覆盖)+ 调用/打印 | 高 | | `tests/sample32.sy` | `char` 常量 + `printChar/println` | 低 | | `tests/sample33.sy` | 逻辑异或 `^^` + `if/else` + 打印 | 中 | | `tests/sample34.sy` | 多 `struct` 变量 + 成员访问 + 组合表达式 | 中 | | `tests/sample35.sy` | 全局 `struct` 变量 + 成员访问 + 打印 | 中 | | `tests/sample36.sy` | `else if` 链 + 比较运算 + 打印 | 中 | | `tests/sample37.sy` | `const` + 算术表达式 + 打印 | 中 | | `tests/sample38.sy` | `float` 常量 + 混合表达式 + 打印 | 中 | | `tests/sample39.sy` | `switch`(无 default)+ `break` + 打印 | 中 | | `tests/sample40.sy` | 函数返回值 + `if` 分支 + 调用/打印 | 中 | | `tests/sample41.sy` | 全局 `union` 变量 + 成员访问 + 打印 | 中 | | `tests/sample42.sy` | `struct` 定义后多变量声明 + 成员访问 + 打印 | 中 | | `tests/sample43.sy` | `void` 函数调用(语法覆盖)+ 打印 | 中 | | `tests/sample44.sy` | `char` 参与表达式 + `printChar/printInt` | 中 | | `tests/sample45.sy` | 复杂条件表达式(&&/||/==/<)+ 打印 | 中 | ## 项目概览 这是一个完整的C--语言编译器前端实现,包括词法分析、语法分析和中间代码生成三个主要模块。 ### 核心特性 - **词法分析**:识别所有C--单词符号 - **语法分析**:基于递归下降算法的完整语法分析 - **代码生成**:生成标准LLVM IR代码 - **符号表管理**:支持多作用域的符号表 - **SLR分析表**:LR(0)项目集、FIRST/FOLLOW集计算 ### 项目结构 ``` compile_theory_practice/ ├── include/ # 头文件 │ ├── lexer.h # 词法分析器接口 │ ├── parser.h # 语法分析器接口 │ ├── ast.h # 抽象语法树定义 │ ├── codegen.h # 代码生成器接口 │ ├── symbol_table.h # 符号表 │ ├── slr_table.h # SLR分析表 │ └── fsm.h # 自动机(词法DFA) ├── src/ # 源文件 │ ├── lexer.cpp │ ├── parser.cpp │ ├── codegen.cpp │ ├── symbol_table.cpp │ ├── slr_table.cpp │ └── fsm.cpp ├── tests/ # 测试用例 │ ├── test_simple.sy │ ├── test_input.sy │ ├── test_var.sy │ └── test_func.sy ├── docs/ # 文档 │ └── PROJECT_STRUCTURE.md 等 ├── output/ # LLVM IR 输出目录(运行编译器时指定) ├── build/ # CMake 构建目录(cmake .. && make 后生成) ├── CMakeLists.txt # 顶层构建配置 └── main.cpp # 主程序入口 ``` --- ## 功能说明 ### 支持的C--语言特性 #### 数据类型 - `int` - 32位整数 - `float` - 浮点数(在当前实现中在 LLVM IR 中按 i32 参与运算) - `char` - 字符类型(在当前实现中按对应 ASCII 的 i32 处理) - `void` - 无返回值 - `struct` / `union` - 自定义复合类型(当前主要做语法/结构检查,并在 IR 中以注释形式记录类型信息) #### 关键字 - `int`, `float`, `char`, `void` - 类型关键字 - `const` - 常量修饰符 - `if`, `else`, `switch`, `case`, `default`, `break` - 控制流关键字 - `struct`, `union` - 复合类型关键字 - `return` - 返回语句 #### 运算符 - 算术:`+`, `-`, `*`, `/`, `%` - 比较:`>`, `<`, `>=`, `<=`, `==`, `!=` - 逻辑:`&&`, `||`, `!`, `^^` - 赋值:`=` #### 语句 - 变量声明和初始化(全局 / 局部) - 常量声明 - 函数定义和调用(支持带参数的函数) - 条件语句:`if` / `else` - 多分支语句:`switch` / `case` / `default` / `break`(基于整数条件) - 结构体 `struct` / 联合体 `union` 类型声明 - 返回语句 `return` ### 词法分析器 (Lexer) **文件**:`lexer.h`, `lexer.cpp` **功能**: - 识别14个关键字:int, void, return, const, float, if, else, char, struct, union, switch, case, default, break - 识别16个运算符:+, -, *, /, %, =, >, <, ==, <=, >=, !=, &&, ||, !, ^^ - 识别8个界符:(, ), {, }, ;, ,, ', : - 识别标识符、整数、浮点数 - 支持单行注释 `//` 和多行注释 `/* */` **输出格式**: ``` [单词] <[种别],[内容]> ``` **示例**: ``` int a = 10 ; ``` ### 语法分析器 (Parser) **文件**:`parser.h`, `parser.cpp`, `slr_table.h`, `slr_table.cpp` **功能**: - 递归下降解析算法 - 支持36条产生式规则 - 构造LR(0)项目集规范族 - 计算FIRST集合、FOLLOW集合 - 构造SLR预测分析表 - 生成抽象语法树(AST) **支持的语法结构**: 1. 变量定义的声明(int a;) 2. 变量定义的初始化(int a = 3;) 3. 常类型数据的声明和定义(const int a = 3;) 4. 变量赋值传递(a = 3; / a = b;) 5. 函数名称、返回值类型、基本块和返回指令 6. 函数传入参数的创建、管理 7. 二元运算表达式(加法和乘法) 8. 单目运算符和变量数值结合(+/-/!) 9. 复合运算表达式 10. 大小比较(>/ >=/ , <, ==, <=, >=, !=, &&, \|\|, !, ^^ | | 界符(SE) | 若干 | (, ), {, }, ;, ,, ', : | | 标识符(IDN) | - | 变量名、函数名 | | 整数(INT) | - | 数字串 | | 浮点数(FLOAT) | - | 包含小数点的数字串 | **关键函数**: - `Lexer(const std::string& src)` - 构造函数 - `Token nextToken()` - 获取下一个单词 - `std::vector tokenize()` - 完整词法分析 - `readNumber()` - 读取数字 - `readIdentifierOrKeyword()` - 读取标识符或关键字 - `readOperator()` - 读取运算符 ### 语法分析器算法描述 语法分析器采用递归下降解析算法,根据C--文法规则自顶向下进行语法分析。 **分析过程**: 1. 初始化产生式集合 2. 从顶层符号开始递归解析 3. 逐个匹配终结符和非终结符 4. 构建抽象语法树(AST) **文法规则**:共36条产生式规则 **主要规则类别**: - 程序结构:Program, compUnit - 声明:decl, constDecl, varDecl - 函数定义:funcDef, funcType, funcFParams - 语句:stmt, block, blockItem - 表达式:exp, cond, addExp, mulExp, relExp, eqExp, lAndExp, lOrExp **关键函数**: - `Parser(const std::vector& toks)` - 构造函数 - `std::shared_ptr parse()` - 主解析函数 - `parseVarDecl()` - 解析变量声明 - `parseFuncDef()` - 解析函数定义 - `parseConstDecl()` - 解析常量声明 ### 代码生成算法描述 代码生成器通过访问者模式(Visitor Pattern)遍历AST,为每个节点类型生成对应的LLVM IR代码。 **处理流程**: 1. 遍历AST根节点 2. 对每个节点调用对应的visit函数 3. 生成LLVM IR指令 4. 维护符号表和临时变量 **关键函数**: - `CodeGenerator()` - 构造函数,初始化LLVM头部 - `visit(const std::shared_ptr&)` - 访问程序节点 - `visit(const std::shared_ptr&)` - 访问变量声明 - `visit(const std::shared_ptr&)` - 访问函数定义 - `emitCode(const std::string&)` - 发出LLVM代码 ### LLVM类型映射 | C--类型 | LLVM类型 | |--------|---------| | int | i32 | | float | float | | void | void |