views:

876

answers:

16

I am looking for a simple compiler that compiles a simple language, I need it to write a paper about it and to learn how compilers work, I am not looking for a sophisticated thing just a simple language (by simple I mean a small code because for example gcc is toooooo big). any help is appreciated.

A: 

Brainfucked is a compiler for the extremely simple-minded language Brainfuck.

Thomas
That implementation is only really useful if you're a Windows assembly programmer. I'd volunteer my implementation but it's currently being rewritten for maintainability. And it's not much of a compiler, it just translates to C (or NASM assembly).
Chris Lutz
Yes, that's the common problem with Braifuck compilers. They aren't really compilers, because the language is "too" simple. The source from such a compiler won't tell you how a real compiler works.
Maximilian Mayerl
A: 

You may look at Calculator example in Bjarne Stroustrup's hilarious book "The C++ programming language".

If you want something more advanced, read the source code of boost::spirit.

Pavel Radzivilovsky
`boost::spirit` is not a compiler, it is a parser generator (AKA compiler-compiler)
AraK
+1  A: 

Depends on your view of simple. You could look at one of the variouse available BrainFuck compilers. That's an extremely simple language and the compilers are veery small. But I don't know how much this will tell you about how a "real" compiler works.

What about looking at a small C compiler? C isn't very compilcated and I think this will give you some insight in compiler construction.

Maximilian Mayerl
The Tiny C Compiler might be useful. http://bellard.org/tcc/
Chris Lutz
Ah, yes, thank you. That'S exactly the thing I thought about, but I couldn't remember the name.
Maximilian Mayerl
Whoah! "C script supported : just add '#!/usr/local/bin/tcc -run'"! I think I've found my new "scripting" language for Linux!
PP
+2  A: 

You should read a book on compiler design; it should have the theory you want to know, as well as some appropriately simple examples.

I recommend "the dragon book": Principles of Compiler Design, by Aho and Ullman. It has been many years since I read it, so I don't recall exactly what examples are available, but it is a very good text.

comingstorm
+1 - you will learn more, and more quickly by reading a book than by attempting to understand a compiler by reading its source code.
Stephen C
The dragon book is kind of hard to read, I think.
Amigable Clark Kant
Anyone that recommends the dragon book to a beginner has obviously never read it.
anon
@Neil: Seconded. It's a great book, but I sure wouldn't recommend it to the OP.
David Thornley
Hey, I have read it, and I did OK with it. I can't think of another intro compiler text I've read that does significantly better at presenting the basic principles -- but maybe it's just been a long time since I've been a beginner...
comingstorm
@Neil: I disagree. I'm a beginner who has been working (off-and-on) on a pet lexer/parser for a while now, and I found taking time to read (or at least skim :) the first few chapters of the dragon book *very* helpful.
bcat
+3  A: 

There are a lot you can use, what you will find easiest will depend on your experience.

Firstly as regards the language:

  1. The simplest is a toy language, for example compiling an arithmetic expressions.
  2. Next is an assembler - again really just translating but shows the basics of parsing and turning into op-codes
  3. Next is probably something like C, which is very close to pure assembler, or something like LISP which is very close to pure theory.

Next, choosing your compiler.

You could start with an assembler - turning assembler into machine code. This was the first step in producing compilers - I'd suggest for a chip like the 6502 or 8080 which are both very simple. Something like the assembler's development kit might work well for you (it comes with examples)

Many people (including me) would argue the easiest languages to write compilers in are functional - nowadays that probably means Haskell, Scheme or Common Lisp. An example of how easy it is is this blog post. He writes a compiler that just compiles arithmetic expressions in a few lines. This might be minimal enough for you.

Almost every introduction to writing compilers at the academic level starts with a minimal language as an example, the Dragon Book is always recommended, but there are other good ones.

At University I used C-- which is like C but even easier to write a compiler for. Lots of resources at: http://www.cminusminus.org/qc--.html

If you wanted a compiler and you know a language like Java I'd suggest something like JavaCC, where the language is specified using grammars. There are lots of example grammars here - pick something simple like C to get started.

Nick Fortescue
+4  A: 

I recommend TinyScheme or jonesforth.

kotlinski
Excecpt TinyScheme is not a compiler. Jonesforth was a good suggestion though, didn't know about that one.
Amigable Clark Kant
+1  A: 

LISPes (Scheme, etc) are the simplest actual languages. You can look how to build a primitive Scheme interpreter in perl with this book (paper version here on Lulu). Parsing, type checking are similar in interpreters and compilers. Then, here is a more hardcore book on the compiler design subject (also available as dead tree on Lulu).

wazoox
A: 

About 1000 lines of code. Compiles Scheme to LLVM assembler or to C. I would say this is an excellent fit for a paper on compilers. If you want to go deeper, I recommend the book "SICP".

Amigable Clark Kant
+1  A: 

The standard Stack Overflow resource for resources on compiler writing is http://stackoverflow.com/questions/1669/learning-to-write-a-compiler

anon
+2  A: 

Look at the simple compiler for PL/0 (a small pascal-like subset - no parameters, only integer data). The source, written in Pascal, is only about 500 lines of code, and is easy to follow. This may be all you need to look at.

However, if you want to go a little farther, once you are comfortable with that, look at the source to Pascal-S. This is a compiler for a larger subset of Pascal, but includes some additional concepts, such as parameter passing, additional data types, and arrays and records (structures). Still it is only about 2000 lines of code, and is easy to follow once you have mastered PL/0.

You can find the sources here:

http://standardpascal.org/source.html

Sammy
+1  A: 

In my former IT school, we had to develop a compiler in C++, but not from scratch : there were steps, learning curve etc..

The concept of the TIGER Compiler and projet assignments

All documents are available, but the code itself isn't, so you'd have to do it all by yourself.

There's a lot of understandable and usable informations, it could be a good start for learning to code a compiler.

Maxime ARNSTAMM
+1  A: 

Google UCSD Pascal. It was a ground-breaker in the 70s. Maybe it's more than you want, but it was easily ported to lot of "micro" chips back then.

Mike Dunlavey
+10  A: 

If you want to look at code, I'm very impressed with Eijiro Sumii's MinCaml compiler.

  • It's only 2000 lines long.

  • It compiles a pretty interesting source language.

  • It generates real machine code, none of this namby-pamby C or LLVM stuff :-)

  • Speed of compiled code is competetive with gcc and the native OCaml compilers.

  • The compiler is designed for teaching.

Did I mention I've been very impressed?

Norman Ramsey
This is pretty neat.
bcat
thanks for a good introduction about MinCaml
coolkid
A: 

Jack Crenshaw, a Ph.D. who has written extensively about practical numerical methods, was scared of compilers for a long time. He finally got tired of being scared, and wrote a multi-part tutorial on compiler construction, based on what he learned as he was teaching himself about the subject.

See "Let's Build a Compiler" for more information. Note that it isn't complete; he ran out of steam before he finished it, but there is a lot of easily-digestible information in there.

John R. Strohm
+1  A: 

I've started a video tutorial on writing an ANTLR 3.x compiler - check out

http://javadude.com/articles/antlr3xtut

I'll be adding more to it soon! -- Scott

Scott Stanchfield
Thank you very much for this video, keep up the good work
Ayoub
Glad you liked it! I'll keep adding as I get time. It's much easier to add video chunks than write a big long piece of text like I did for the ANTLR 2.x tutorial!
Scott Stanchfield
A: 

This one is only 300 lines of normal code and implements a simple universal language link text , is something like that what you were looking for?

nhpy