You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# SysY Compiler
用于实现SysY编译器的代码框架。
## Getting Started
建议使用Ubuntu 22.04系统原生版本与WSL版本均可。
[Ubuntu下载与安装说明](https://ubuntu.com/download/desktop)
[WSL Ubuntu安装说明](https://learn.microsoft.com/en-us/windows/wsl/install)
SysY编译器前端基于[ANTLR](https://www.antlr.org/index.html)工具实现本仓库已经包含ANTLR 4.9.3版本的可执行程序与C++运行时库但编译ANTLR运行时库存在一些依赖需要提前安装。
```bash
sudo apt update
sudo apt install -y uuid-dev libutfcpp-dev pkg-config make git cmake openjdk-11-jre
```
依赖安装完成后可以开始构建SysY编译器构建过程包含了ANTLR运行时库的构建
```bash
git clone https://gitee.com/xsu1989/sysy.git
cd sysy
cmake -S . -B build
cmake --build build
```
构建完成后,可以运行一个小的测试用例。该测试将逗号分隔的整数或字符串列表进行格式化后重新输出,即将相邻参数之间的分隔统一调整为逗号外加一个空格。
```bash
cat /test/funcrparams.sysy
# -> 1,0xa , 011, "hellow"
./build/bin/sysyc test/funcrparams.sy
# -> 1, 0xa, 011, "hellow"
```
## Documentation
[ANTLR手册](doc/The%20Definitive%20ANTLR%204%20Reference.pdf)
[SysY语言规范](doc/sysy-2022-spec.pdf)
[SysY运行时库](doc/sysy-2022-runtime.pdf)
## 实验1用ANTLR实现SysY词法/语法分析器
当前的代码框架已经部署好了编译环境,同学们可专注于程序开发。
在实验1中同学们需要完成的任务包括
- 参照SysY语言规范修改`src/SysY.g4`文件实现SysY词法/语法的完整定义
- 修改任意代码后需要重新执行`cmake --build build`命令重新构建项目ANTLR工具会从`SysY.g4`生成词法/语法分析器,生成的文件位于`./build/src`目录
- (进阶内容)修改`src/ASTPrinter.h`与`src/ASTPrinter.cpp`实现从AST输出源程序但输出的源程序是经过格式化的测试用例为`test/format-test.sy`,格式化后的参考结果为`test/format-ref.sy`
## 实验2从AST生成中间表示
exp2分支为大家准备好了进行实验2的基本代码框架包括
- IR相关数据结构的定义`src/IR.h`
- 创建IR对象的工具类`src/IRBuilder.h`
- IR生成器的示例代码`src/SysYIRGenerator.h`
在实验2中同学们需要完成的任务包括
- 熟悉掌握IR定义与相关数据结构
- 从AST生成IR基于visitor机制