catc πΊ - Tiny C Compiler with Lex & Yacc
September 21, 2025 Β· View on GitHub
π catc is a lightweight C compiler built using Lex and Yacc! Perfect for learning and experimenting with the basics of C compilation. β¨
π Overview
This project is a small C compiler that transforms C code into assembly using Lex and Yacc. Itβs simple, supports basic C syntax, and is ideal for those curious about how compilers work! π¨βπ» π Great for learning or testing a minimal C compiler.
π οΈ Getting Started
Get up and running in just a few steps! π
1. Install Dependencies
$ dnf install byacc flex
2. Build the Compiler
$ make π¨
3. Compile and Run
- Convert C code to assembly:
$ ./catc -S ./test/hello.c
.section .rodata
.LC0: .string "Hello! tiny C world!!\n"
.text
.align 16
.globl main
.type main,@function
main:
pushq %rbp
movq %rsp,%rbp
subq \$64,%rsp
movq %rbx,-8(%rbp)
leaq .LC0(%rip),%rax
movq %rax,%rdi
call printf
movq \$0,%rax
jmp .L1
.L1: movq -8(%rbp),%rbx
leave
ret
- Assemble the output:
$ ./catc -S ./test/hello.c > ./test/hello.asm
$ as ./test/hello.asm -o ./test/hello.o
$ ./catc32 -S ./test/hello.c
.section .rodata
.LC0: .string "Hello! tiny C world!!\n"
.text
.align 4
.global main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
subl \$24,%esp
movl %ebx,-4(%ebp)
lea .LC0,%eax
pushl %eax
call printf
movl \$0,%eax
jmp .L1
.L1: movl -4(%ebp),%ebx
leave
ret
$ ./catc32 -S ./test/hello.c > ./test/hello.asm
$ as --32 ./test/hello.asm -o ./test/hello.o
$ ./catc_arm -S ./test/hello.c
.section .rodata
.LC0:
.asciz "Hello! tiny C world!!\n"
.text
.align 2
.global main
.type main, %function
main:
push {fp, lr}
add fp, sp, #4
sub sp, sp, #24
str r4, [fp, #-8]
ldr r0, =.LC0
mov r0, r0
bl printf
mov r0, #0
b .L0
.L0:
ldr r4, [fp, #-8]
sub sp, fp, #4
pop {fp, pc}
$ ./catc_wasm -S ./test/hello.c
(func $main (export "main") (param i64) (result i64)
(local i64)
(local i64)
(local $pc i32)
i32.const 2147483646
local.set $pc
loop $big_loop
local.get $pc
i32.const 2147483646
i32.eq
if
i32.const 0
i64.extend_i32_u
local.set 1
local.get 1
call $printf
i64.const 0
local.set 2
local.get 2
return
end
br $big_loop
end
i64.const 0
return
)
- Link and run the program:
$ gcc ./test/hello.o -o ./test/hello
$ ./test/hello
Hello! tiny c world!!
- Or run directly:
$ ./catc ./test/test01.c
$ ./test/test01
Hello world. 100+23=123
π Project Structure
AST.c,AST.h: Abstract Syntax Tree definitions and handling π³clex.l,cparser.y: Lex and Yacc source files πx86_code_gen.c: x86 assembly code generation β‘οΈtest/: Sample C code files πMakefile: Build script π§
π Resources
- TakeWiki π
- C Parser π
- Tiny C Note π
π Project Stats
- β 90 Stars
- π 5 Watching
- π΄ 20 Forks
π€ Contributing
Contributions are welcome! πΎ Feel free to report bugs or suggest features via GitHub Issues or submit a Pull Request.
π License
This project is open source. Check the repository for license details. π
πΊ Dive into the world of compilers with catc! Happy Coding! π»