Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zakirullin/tiny-compiler
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
https://github.com/zakirullin/tiny-compiler
asm ast compiler lexer parser
Last synced: 4 days ago
JSON representation
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
- Host: GitHub
- URL: https://github.com/zakirullin/tiny-compiler
- Owner: zakirullin
- License: mit
- Created: 2017-04-12T08:40:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-20T18:32:10.000Z (almost 2 years ago)
- Last Synced: 2025-01-12T06:04:27.044Z (12 days ago)
- Topics: asm, ast, compiler, lexer, parser
- Language: C
- Homepage:
- Size: 78.1 KB
- Stars: 562
- Watchers: 14
- Forks: 45
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- AwesomeCompiler - tiny-compiler
README
# 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) ([`lex.c`](./src/lex.c))
* [Parser](https://en.wikipedia.org/wiki/Parsing) ([`parser.c`](./src/parser.c))
* Assembler like [code generator](https://en.wikipedia.org/wiki/Code_generation_(compiler)) ([`gen.c`](./src/gen.c))
* [Virtual machine](https://en.wikipedia.org/wiki/Virtual_machine) ([`vm.c`](./src/vm.c))
* [Symbol table](https://en.wikipedia.org/wiki/Symbol_table) ([`sym.c`](./src/sym.c))
* [Abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) ([`ast.c`](./src/ast.c))
## It is by no means a complete industry standard implementation. Some parts are simplified for the sake of better understanding
## Build
```$ cmake -S . -B 'build' && cmake --build 'build'```
## Usage
```$ ./build/tinycompiler ```
## 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, ")" ;
```