is there a simple compiler for a small language

2019-03-07 17:35发布

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.

18条回答
smile是对你的礼貌
2楼-- · 2019-03-07 18:27

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).

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-07 18:27

The smallest practical compiler I know is MetaII, which has a 40 line self-description and can compile itself, using a special runtime system which admittedly hides some interesting computation (but then, most of the compilers listed here assume some kind of runtime system, surely bigger than the one for MetaII. I've build real compilers starting with this as a foundation, although meta-self-enhanced rather spectacularly.

There's been work to make that even smaller. I don't think there are any practical compilers based on this direction.

These are worth writing a paper about.

查看更多
小情绪 Triste *
4楼-- · 2019-03-07 18:29

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.

查看更多
走好不送
5楼-- · 2019-03-07 18:30

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

查看更多
▲ chillily
6楼-- · 2019-03-07 18:31

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".

查看更多
做个烂人
7楼-- · 2019-03-07 18:32
登录 后发表回答