{"id":16779691,"url":"https://github.com/lc044/minic","last_synced_at":"2025-07-13T06:32:42.638Z","repository":{"id":176126096,"uuid":"644774986","full_name":"LC044/MiniC","owner":"LC044","description":"MiniC语言编译器前端，生成抽象语法树，产生线性IR，生成控制流图","archived":false,"fork":false,"pushed_at":"2024-03-25T13:46:31.000Z","size":91835,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T21:14:14.902Z","etag":null,"topics":["ast","complier","ir"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LC044.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-24T08:25:48.000Z","updated_at":"2025-04-07T09:43:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"d28dea59-1a04-4c68-b461-c9ec47dbfe93","html_url":"https://github.com/LC044/MiniC","commit_stats":null,"previous_names":["lc044/minic"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LC044/MiniC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LC044%2FMiniC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LC044%2FMiniC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LC044%2FMiniC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LC044%2FMiniC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LC044","download_url":"https://codeload.github.com/LC044/MiniC/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LC044%2FMiniC/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265099203,"owners_count":23711299,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","complier","ir"],"created_at":"2024-10-13T07:31:43.026Z","updated_at":"2025-07-13T06:32:42.607Z","avatar_url":"https://github.com/LC044.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![stars](https://img.shields.io/github/stars/LC044/MiniC.svg)](https://github.com/LC044/MiniC/stargazers)\n[![文档](https://img.shields.io/badge/文档-brightgreen.svg)](./doc/readme.md)\n[![LICENSE](https://img.shields.io/badge/license-GPL3.0-blue.svg)](LICENSE)\n\n- [西北工业大学编译原理实验课](#西北工业大学编译原理实验课)\n  - [要求与目标](#要求与目标)\n    - [实验目标：](#实验目标)\n    - [实验要求：](#实验要求)\n  - [总体完成情况](#总体完成情况)\n- [minic](#minic)\n  - [功能](#功能)\n  - [功能示例](#功能示例)\n  - [具体功能](#具体功能)\n    - [词法分析](#词法分析)\n    - [语法分析](#语法分析)\n    - [语义分析](#语义分析)\n    - [代码优化](#代码优化)\n  - [源代码构成](#源代码构成)\n  - [各模块之间的关系](#各模块之间的关系)\n  - [详细设计](#详细设计)\n\n# 西北工业大学编译原理实验课\n\n2023.06.28\n\n## 要求与目标\n\n### 实验目标：\n\n1. 理解并掌握词法分析、语法分析、语义分析、中间代码生成功能\n2. 理解并掌握非线性 IR：抽象语法树\n3. 理解并掌握线性 IR：三地址语句或四元式\n\n### 实验要求：\n\n1. 识别程序是否符合MiniC的语法要求\n2. 输入MiniC的源文件\n3. 输出程序的中间IR表示\n4. 输出抽象语法树，通过Graphviz显示\n5. 输出控制流图，通过Graphviz显示\n\n## 总体完成情况\n\n![alt text](./doc/image/image.png)\n\n# minic\n\n## 功能\n\n1. ✅选项-a可产生抽象语法树AST\n   \n    cmake-build-debug\\minic.exe -a -o test.jpg  test.c\n\n2. ✅选项-r可产生线性IR\n   \n    cmake-build-debug\\minic.exe -i -o test.ir  test.c\n\n3. ✅选项-c可产生控制流图\n   \n    cmake-build-debug\\minic.exe -c main -o test_main_cfg.png  test.c\n\n## 功能示例\n\n1. [028_if_test1.c](/doc/image/028_if_test1_ast.jpg) 抽象语法树\n   \n   \u003cdiv align=\"center\", \u003e\n    \u003cimg src=\"./doc/image/028_if_test1_ast.jpg\"\u003e\n    \u003cp \u003e抽象语法树\u003c/p\u003e\n    \u003c/div\u003e\n\n2. [028_if_test1.c](/doc/image/028_if_test1_ast.jpg) 线性ir\n   \n   ```bash\n   define i32 @ifElse() {\n       declare i32 %l0 ; variable: return\n       declare i32 %l1 ; variable: a\n       declare i32 %t2\n       declare i1 %t3\n       declare i32 %t4\n       declare i32 %t5\n       declare i32 %t6\n       declare i32 %t7\n       entry\n       %l1 = 5\n       %t2 = %l1\n       %t3 = cmp eq %t2, 5\n       bc %t3, label .L3, label .L4\n   .L3:\n       %l1 = 25\n       br label .L5\n   .L4:\n       %t4 = %l1\n       %t5 = mul %t4, 2\n       %l1 = %t5\n       br label .L5\n   .L5:\n       %t6 = %l1\n       %l0 = %t6\n       %t7 = %l0\n       exit %t7\n   }\n   define i32 @main() {\n       declare i32 %l0 ; variable: return\n       declare i32 %t1\n       declare i32 %t2\n       entry\n       %t1 = call i32 @ifElse()\n       %l0 = %t1\n       %t2 = %l0\n       exit %t2\n   }\n   ```\n\n3. [028_if_test1.c](/doc/image/028_if_test1_ast.jpg) 控制流图\n   \n   \u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"./doc/image/028_if_test1_cfg.png\"\u003e\n    \u003cp\u003e控制流图\u003c/p\u003e\n    \u003c/div\u003e\n\n## 具体功能\n\n### 词法分析\n\n1. 词法分析使用flex实现\n2. 识别MiniC文法的所有终结符(标识符、关键字、十进制、八进制、十六进制整数)\n3. C语言的注释识别支持\n4. 提供文法识别错误的行号定位信息\n\n### 语法分析\n\n1. 语法分析使用bison实现\n2. 支持int和void类型，变量只允许int，函数返回值可以使用void和int\n3. 变量定义(含全局变量、局部变量)与使用\n4. 表达式语句、空语句支持\n5. if语句、while语句和赋值语句支持\n6. 语句块支持\n7. 表达式支持算术运算、关系运算、逻辑运算；满足C语言的优先级、结合性等要求\n8. 支持函数定义、声明与函数调用\n9. 支持一维数组和多维数组定义与使用\n10. 简单变量定义时初始化支持(语法支持，语义暂不支持)\n11. 支持for循环\n12. 支持自增与自减运算\n13. 语法分析错误检查与定位(暂只支持错误定位，不支持错误类型判断)\n\n### 语义分析\n\n1. 采用语法制导翻译，生成抽象语法树\n2. 遍历抽象语法树产生中间IR\n3. 变量分层管理支持，支持变量的作用域，特别是变量的重名\n4. 变量或函数要先声明后使用，否则语义错误\n5. 逻辑运算支持短路求值\n6. 具备简单的语义错误检查定位和输出\n7. 中间IR表达采用实验定义IR\n\n### 代码优化\n\n1. 基本块划分与控制流图生成\n2. 控制流图图形化显示(使用graphviz的API产生控制流图)\n3. 控制流的优化\n   1. 删除不可达基本块\n   2. 删除多余Label指令\n4. IR的优化\n   1. 常量折叠(a = 2+3等价于a=5)\n   2. 没有了\n\n\u003c!-- \ngit config --global --unset http.proxy\n\ngit config --golbal http.proxy \u003chttp://127.0.0.1:7890\u003e\n\ngit commit -m\n\ngit push -u origin main\n\ngit reset --hard\n\ngit fetch --all\n\ngit reset --hard origin/main\n\n老师写好的程序运行方式\n\n.\\test\\IRCompiler\\MINGW-x86_64\\IRCompiler.exe -S -I -o test.ir0 test.c --\u003e\n\n## 源代码构成\n\n| 文件名                      | 作用                          |\n| ------------------------ | --------------------------- |\n| minic.l                  | 借助flex工具实现的词法分析器脚本源代码       |\n| minic_lex.cpp            | 借助flex工具实现的词法分析器自动生成C语言源文件  |\n| minic_lex.h              | 借助flex工具实现的词法分析器自动生成C语言头文件  |\n| minic.y                  | 借助bison工具实现的语法分析器脚本源代码      |\n| minic_yacc.cpp           | 借助bison工具实现的语法分析器自动生成C语言源文件 |\n| minic_yacc.h             | 借助bison工具实现的语法分析器自动生成C语言头文件 |\n| ast.cpp                  | 抽象语法树创建所需要的函数               |\n| ast.h                    | ast.cpp对应的头文件               |\n| parser.h                 | flex和bison工具用到的节点属性定义       |\n| graph.cpp                | 遍历抽象语法树利用graphviz生成图片       |\n| graph.h                  | graph.cpp对应的头文件             |\n| symbol.cpp               | 符号表管理                       |\n| symbol.h                 | symbol.cpp对应的头文件            |\n| genSymbol.cpp            | 产生符号表并进行语义检查                |\n| genIR.cpp                | 线性IR指令类的实现                  |\n| IRInst.h                 | 线性IR指令类的头文件                 |\n| IRCode.cpp               | 线性IR管理类的实现                  |\n| cfg.h,.cfg.cpp           | 控制流图的头文件及其实现                |\n| Basic_block_division.cpp | 实现基本块划分                     |\n| common.cpp,common.h      | 一些通用函数的定义与实现                |\n| test.c                   | 测试用例                        |\n| main.cpp                 | 计算器程序的主函数                   |\n\n## 各模块之间的关系\n\n![alt text](./doc/image/img5.png)\n\n\n## 详细设计\n\n[参考文档](./doc/readme.md)\n\n打个广告\n\n[MemoTrace——一款强大的微信聊天记录管理工具](https://memotrace.cn/)\n\n\u003e 我深信有意义的不是微信，而是隐藏在对话框背后的一个个深刻故事。未来，每个人都能拥有AI的陪伴，而你的数据能够赋予它有关于你过去的珍贵记忆。我希望每个人都有将自己的生活痕迹👨‍👩‍👦👚🥗🏠️🚴🧋⛹️🛌🛀留存的权利，而不是遗忘💀。\n\n![](https://memotrace.cn/img/pdfdemo.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flc044%2Fminic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flc044%2Fminic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flc044%2Fminic/lists"}