• Stars
    star
    482
  • Rank 91,212 (Top 2 %)
  • Language
    C
  • Created almost 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A C & x86 version of the "Let's Build a Compiler" by Jack Crenshaw

Information

A C version of the "Let's Build a Compiler" by Jack Crenshaw http://compilers.iecc.com/crenshaw/

This repository is forked form: https://github.com/vtudose/Let-s-build-a-compiler

And since the original is far from complete(only the first two chapter), and the author had been inactive for quite a long time, I decided to create a new repository.

If there are are any licence issue, please inform.

Comments

Below are some comments about the source code here and the original article.

What to do with the generated code

The generated assembly code is devoted to x86 architecture. The syntax is AT&T style and the experiment operating system is Linux. You may try to save the generated code to for example "test.s" and execute the following commands to check the output.

as --32 -o test.o test.s
ld -m elf_i386 test.o -o test
./test
echo $?

Assembly code

It is a C port of the original article which is written in Pascal. And the generated code are ported to x86 instead of 86000.

If you want to test the generated code, please keep in mind that the generated code might be incomplete to be directly assembled. For example, when loading a variable, we directly generate Assembly code "movl X, %eax", and variable 'X' might not be declared since the article is far from mentioning "variable types". Thus you'll have to type the missing parts all by yourself.

IO routines

I am definitely NOT an assembly expert. When the author mentioned adding IO routine by library, I cannot simply find the x86 alternative. The closest thing is C's library function "printf". But then I decided not no bother adding this kind of routine cause they are only used in two chapters.

Instead, I'll save the value as the return code of a process. It's done by saving the value to register "%ebx" and call "int $0x80", as follows:

movl var, %ebx
movl $1, %eax  # exit function
int $0x80

-Note from random github user-

On older operating systems IO was performed by an interrupt to an area of memory that contains instructions for performing the operation with your particular hardware configuration. This left the developers of the operating systems unable to change the memory layout without forcing all the software written to be modified as well.

The POSIX standard on UNIX-like operating systems as well as Windows and Macintosh operating systems, started including libraries for C or Pascal with functions for system calls. This means you will either have to compile for DOS or learn some specialized things about your operating system and the format of C libraries.

For anyone on Windows I would recommend getting a used copy of Charles Petzold's Programming Windows Fifth Edition and checking out http://www.godevtool.com for some free tools and tutorials for accessing Windows API from assembly.

-End of note-

The Article

The article mainly talks about how to design compiler top-down. It covered a lot of aspects in compiler design like lexical scanning, BNF, symbols, procedures, types, code generation, etc.

It is a good article to follow for beginners.

More Repositories

1

skim

Fuzzy Finder in rust!
Rust
5,136
star
2

write-a-C-interpreter

Write a simple interpreter of C. Inspired by c4 and largely based on it.
C
4,032
star
3

rargs

xargs + awk with pattern matching support. `ls *.bak | rargs -p '(.*)\.bak' mv {0} {1}`
Rust
488
star
4

fuzzy-matcher

Fuzzy Matching Library for Rust
Rust
260
star
5

very-simple

A very simple theme for hexo
SCSS
148
star
6

skim.vim

vim support for skim
Vim Script
132
star
7

tuikit

Tool kit for writing TUI applications in Rust.
Rust
104
star
8

hexo-theme-noise

A hexo theme
Less
103
star
9

CodeGenerator

Intellij IDEA Plugin for creating customized code generators like the builtin toString, equals, etc.
Java
54
star
10

spring-security-example

REST authentication apis & token based authentication, etc.
Java
33
star
11

transformer-playground

Annotation processor @Transform for creating transformers for classes.
Java
17
star
12

static-wiki

CSS
16
star
13

pymustache

Mustache template engine from scratch in Python.
Python
15
star
14

SlackGuide-cn

A chinese guide for slackware based on SlackBook.
11
star
15

lotabout.github.io

My personal blog. (source at source branch)
C++
9
star
16

dotfiles

Manage the resource files under home folder
Emacs Lisp
8
star
17

buddy-system

Simple implementation of a buddy system for memory management.
C
8
star
18

orgwiki

personal wiki generated by org-mode
CSS
8
star
19

compiler-design-in-c

Codes for book <compiler design in C>
C
6
star
20

ywvim

a fork of ywvim. http://www.vim.org/scripts/script.php?script_id=2662
Vim Script
6
star
21

simple-template-engine

A simple template engine written in python
Python
5
star
22

vimwiki-tpl

template for vimwiki
CSS
5
star
23

project-euler-racket

Project Euler in racket.
Racket
5
star
24

axe

Handy utilities for racket
Racket
5
star
25

c-interfaces

codes for book "C Interfaces and Implementation Techniques".
C
5
star
26

mdbook-fix-cjk-spacing

mdbook preprocessor that removes extra space rendered for Chinese lines.
Rust
4
star
27

jasypt-online

Jasypt εœ¨ηΊΏεŠ θ§£ε―†ε·₯ε…·
HTML
3
star
28

underscore-comment

Read and Comment on source code of underscore.js
JavaScript
3
star
29

hexo-filter-fix-cjk-spacing

Join continuous CJK lines in markdown in Hexo.
JavaScript
3
star
30

simple-framework

A simple python web framework from scratch
Python
3
star
31

nikola-bnw

A nikola theme.
CSS
2
star
32

zzz

theme for nikola
CSS
2
star
33

learn-c-the-hard-way

code & exercise reading book <Learn C the hard way>
C
2
star
34

curtail

pipe stdin to a fixed-size log file
Rust
2
star
35

zlex

A lexical analyzer generator.
C
2
star
36

build-your-own-pytorch

Understand deep learning framework(like torch) by implementing the essentials
Python
2
star
37

cup

A small web framework for racket
Racket
2
star
38

cljs-douban

douban.fm in CLJS+Electron
Clojure
1
star
39

lemon

Lemon parser generator (http://www.hwaci.com/sw/lemon/). Submit for code review for study purpose.
C
1
star
40

pymkd

Markdown in python
Python
1
star
41

ideas

log for new ideas and the execution of ideas
1
star
42

code-snippets

collection of useful code snippets.
C
1
star
43

my-time

A time management web application written in clojure with luminus.
Clojure
1
star
44

my-slackbuilds

My slackware build scripts
Shell
1
star
45

pscat

socat in one python file
Python
1
star
46

orgmark.vim

Vim Script
1
star
47

docker-slackware

Build slackware images for docker
1
star
48

cljs-douban-reframe

douban.fm in CLJS+Electron+Re-frame
Clojure
1
star
49

LiSP

Codes for book "Lisp in Small Pieces"
Scheme
1
star
50

java-spi-playground

A simple expression interpreter that use SPI to load user-customized functions
Java
1
star
51

dragon-book-notes

Notes reading the Dragon Book.
C
1
star
52

dwm

My own customization of dwm, starting with dwm-6.0.
C
1
star