zakirullin/tiny-compiler
소개
LL(2) 문법을 사용하는 언어를 위한 작은 컴파일러입니다. 렉서, 파서, ASM과 유사한 코드 생성기, VM을 포함합니다. 단순한 토이 예제가 아니라 실제 컴파일러가 어떻게 동작하는지 감을 잡을 수 있을 만큼 복잡합니다.
프로그래밍 언어 및 런타임측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- +1
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalTiny compiler
A tiny compiler for a language featuring LL(2) grammar. 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.
The compiler consist of typical parts, known as:
- Lexer (
lex.h) - Parser (
parser.h) - Assembler like code generator (
gen.h) - Virtual machine (
vm.h) - Symbol table (
sym.h) - Abstract syntax tree (
ast.h)
It is by no means a complete industry standard implementation. Some parts are simplified for the sake of better understanding.
Build
$ make
Usage
$ ./compiler program.src
An example program for Pythagorean theorem:
cath1 = 3;
cath2 = 4;
hypsquare = cath1 * cath1 + cath2 * cath2;
Execution result:
hypsquare = 25
Generated 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 grammar in EBNF:
program = expr, ";", { program } ;
expr = id, "=", expr | term, { ("+"|"-"), term } ;
term = factor, { ("*"|"/"), factor } ;
factor = id | num | "(", expr, ")" ;
This grammar can be parsed with an LL (left-to-right, leftmost derivation) parser with only 2 tokens of lookahead. In other words, we only need to examine at most 2 upcoming lexical tokens to unambiguously determine which production rule to apply.
If we look at a single id token, we can't tell whether it's an expr or a factor. We need to look at one more token to remove the ambiguity. That makes it an LL(2) grammar.
It is also a context-free grammar, which means that on the left side of every production rule there is a single non-terminal symbol.
관련 프로젝트 살펴보기
불러오는 중…