tiny-compiler-master
所属分类:其他
开发工具:C/C++
文件大小:8KB
下载次数:1
上传日期:2019-12-25 20:59:30
上 传 者:
御坂10066
说明: c语言编译器,实现了词法语法分析,以及中间代码生成
(C language compiler, which realizes lexical syntax analysis and intermediate code generation)
文件列表:
LICENSE.md (1082, 2019-11-09)
examples (0, 2019-11-09)
examples\program1 (17, 2019-11-09)
examples\program2 (36, 2019-11-09)
examples\program3 (65, 2019-11-09)
src (0, 2019-11-09)
src\asm_dumper.h (789, 2019-11-09)
src\ast.h (559, 2019-11-09)
src\codes.h (171, 2019-11-09)
src\defs.h (70, 2019-11-09)
src\error.h (131, 2019-11-09)
src\generator.h (951, 2019-11-09)
src\lexer.h (2131, 2019-11-09)
src\main.c (601, 2019-11-09)
src\parser.h (2276, 2019-11-09)
src\stack.h (504, 2019-11-09)
src\symbol_table.h (765, 2019-11-09)
src\virtual_machine.h (983, 2019-11-09)
# A tiny compiler for a simple synthetic language featuring [LL(2) grammar](https://en.wikipedia.org/wiki/LL_grammar), written in pure C
## The compiler consist of typical parts, known as:
* [Lexer](https://en.wikipedia.org/wiki/Lexical_analysis) (```lexer.h```)
* [Parser](https://en.wikipedia.org/wiki/Parsing) (```parser.h```)
* Assembler like [code generator](https://en.wikipedia.org/wiki/Code_generation_(compiler)) (```generator.h```)
* [Virtual machine](https://en.wikipedia.org/wiki/Virtual_machine) (```virtual_machine.h```)
* [Symbol table](https://en.wikipedia.org/wiki/Symbol_table) (```symbol_table.h```)
* [Abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) (```ast.h```)
## The compiler is implemented for educational purposes. Some parts are simplified for the sake of better understanding
## Build
```$ gcc main.c -o compiler```
## Usage
```$ ./compiler source```
## An example program for Pythagorean theorem:
```
cath1 = 3;
cath2 = 4;
hypsquare = cath1 * cath1 + cath2 * cath2;
```
Execution result:
```
hypsquare = 25
```
Generated ASM:
```asm
PUSH 3
WRITE cath1
PUSH 4
WRITE cath2
READ cath1
READ cath1
MUL POP, POP
READ cath2
READ cath2
MUL POP, POP
ADD POP, POP
WRITE hypsquare
```
## The language description in [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form):
```
program = expr, ";", { program } ;
expr = id, "=", expr | ("+"|"-"), term, { ("+"|"-"), term } ;
term = factor, { ("*"|"/"), factor } ;
factor = "id" | "num" | (expr) ;
```
近期下载者:
相关文件:
收藏者: