Add README and test

main
Su Xing 3 years ago
parent 2c0ef95d97
commit 97982ee1c5

3
.gitignore vendored

@ -30,3 +30,6 @@
*.exe
*.out
*.app
.cache
.~

@ -21,12 +21,17 @@ if(NOT CMAKE_BUILD_TYPE)
"Choose the type of build, options are: Debug Release." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
# Set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
# ANTLR
set(ANTLR_EXECUTABLE "${PROJECT_SOURCE_DIR}/antlr/antlr-4.9.3-complete.jar")
set(ANTLR_RUNTIME "${PROJECT_SOURCE_DIR}/antlr/antlr4-runtime")
set(ANTLR4_INSTALL ON)
set(ANTLR_BUILD_CPP_TESTS OFF)
# set(ANTLR_BUILD_CPP_TESTS OFF) # Only for ANTLR 4.10.0 and later versions
add_subdirectory(${ANTLR_RUNTIME})
# Project source files

@ -0,0 +1,51 @@
# SysY Compiler
用于实现SysY编译器的代码框架。
## Getting Started
建议使用Ubuntu系统原生版本与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 install uuid-dev libutfcpp-dev pkg-config python3-pip make git
pip3 install cmake
```
依赖安装完成后可以开始构建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`

@ -0,0 +1,31 @@
int get_one(int a) {
return 1;
}
int deepWhileBr(int a, int b) {
int c;
c = a + b;
while (c < 75) {
int d;
d = 42;
if (c < 100) {
c = c + d;
if (c > 99) {
int e;
e = d * 2;
if (get_one(0) == 1) {
c = e * 2;
}
}
}
}
return (c);
}
int main() {
int p;
p = 2;
p = deepWhileBr(p, p);
putint(p);
return 0;
}

@ -0,0 +1,30 @@
int get_one(int a)
{
return 1;
}
int deepWhileBr(int a,int b){
int c;
c = a + b;
while(c<75) {
int d; d=42;
if (c<100) {
c =c+d;
if (c > 99) {
int e;
e = d*2;
if (get_one(0)==1) c=e * 2;
}
}
}
return (c);
}
int main() {
int p;
p = 2;
p = deepWhileBr(p, p);
putint(p);
return 0;
}

@ -0,0 +1 @@
1,0xa , 011, "hellow"
Loading…
Cancel
Save