is there a simple compiler for a small language

前端 未结 18 2099
有刺的猬
有刺的猬 2021-01-29 22:55

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 thi

相关标签:
18条回答
  • 2021-01-29 23:08

    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?

    0 讨论(0)
  • 2021-01-29 23:08

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

    0 讨论(0)
  • 2021-01-29 23:11

    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?

    0 讨论(0)
  • 2021-01-29 23:12

    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 http://en.wikipedia.org/wiki/Dragon_Book_%28computer_science%29 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.

    0 讨论(0)
  • 2021-01-29 23:15

    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

    0 讨论(0)
  • 2021-01-29 23:16

    I recommend TinyScheme or jonesforth.

    0 讨论(0)
提交回复
热议问题