← Back to context

Comment by sieve

2 days ago

Compilers are some of the simplest "complicated" programs out there if you start by throwing yacc/bison/antlr/parser generators in the garbage.

Production compilers are complicated because of the feature set of languages and performance requirements.

You can write a lexer + parser + treewalk interpreter for a simple language in a day if you know what you are doing.

The hardest part (IMHO) about getting a simple language running, particularly for beginners, is the “expression”. Mostly for your typical algebraic style infix expressions with precedence.

Just getting the grammar straight on the naturally recursive structures can be a trick. They also touch a large portion of the code generation and run time.

Get:

  (a + c/2) * sqrt(b)

working and you’re 80% there.

  • > The hardest part (IMHO) about getting a simple language running, particularly for beginners, is the “expression”

    This is why teaching material should be tailored towards teaching rather than implementing demented stuff from the real world just because it is "there." You can easily implement a parser that ignores precedence and tell the reader that we are using brackets to force precedence instead.

    In a real-word parser, you might use something like this[1] or some other home-grown algorithm if you didn't know about it. Doesn't matter as long as the parser works.

    [1] Top-Down operator precedence (Pratt) parsing https://eli.thegreenplace.net/2010/01/02/top-down-operator-p...

  • Is that really the hard part? I figured that part out before I knew much programming: made a simple Excel type formula parser. (A horrible, horrible solution based around replacing strings and manually counting parentheses... but it worked!)

    I never got much farther than that though, I've looked into making a compiler many times and got overwhelmed every time.

    • Good to know I wasn't alone in writing terrible expression parsers. Since I knew absolutely nothing about parsing, my parser/interpreter consisted of splitting on whitespace, followed by linear scanning to find the most high precedence operation, repeated until a single token remains (basically how a human would do it).

    • It's hard if:

      1. You dont recognise the recursive nature of the problem,

      Or

      2. You don't recognise the stack-based nature in the problem (shunting yard algo, etc).

      IOW, you did it the hard way :-).

  •     (a + c/2) * sqrt(b)
    

    is not a simple language.

        (* (+ a (/ c 2)) (sqrt b))
    

    would be a simple and precise to parse language, and the typical compiler for this is written in a day. Search for SIOD

  • well, in my personal experience, parsing is the easiest part. it's what you do with the AST once you have one that is the hard part. but for some reason an overwhelming amount of writing is about frontends when you could teach the concepts to a layman with some boxes and arrows.

  • I think the hardest part is function calls. Implementing the standard calling convention is really fiddly and annoying when you have to save and restore registers around function calls if you want to do it efficiently (keeping track of which registers are dirty, doing parallel loads of registers into the right argument registers, spilling when appropriate, etc. It is fiddly.

    The alternative is going to an IR and doing full register allocation but then you need to implement Lengauer-Tarjan to get into SSA, all the same parallel load stuff for phi functions/block arguments, out-of-SSA, reconstruct in optimisation passes all the information you discard by going to a linear IR, etc.

    • I'd argue that Lengauer-Tarjan is overrated. While it is neat, it's premature optimization IMHO. Fully maximal SSA (at the entry of each block insert a phi-node for each variable) + dead code elimination (we need it anyways) is much simpler.

      6 replies →

> lexer + parser + treewalk interpreter

If all you do is construct an AST and interpret it it's not really a compiler is it. It doesn't compile from source to target. At most you're describing a compiler front end (arguably), or an interpreter.

I would expect:

lexer + parser + AST definition and construction + semantic analysis/type checking + X + codegen to ASM/WASM/C

where X includes

  - definition of an intermediate representation (IR)
  - lowering AST to IR
  - static analysis
  - improvers/optimisation passes (at least some simple stuff)
  - code gen including register allocation

This can still be a simple "complicated" program, but there's more to a compiler than an AST interpreter.

EDIT: I notice that the author of the original article has also started work on an optimizing compiler: https://ssloy.github.io/tinyoptimizer/

  • > If all you do is construct an AST and interpret it it's not really a compiler is it.

    What if I dumped the AST to disk, called it "bytecode" and updated the "interpreter" to run this "bytecode"? Would you consider that to be a compiler? :-)

    > where X includes

    I don't consider any of these things to be essential. There are entire languages out there which run on interpreters of some kind. There are others which compile down to C/JS. Many compilers use LLVM as their backend. By your definition, none of them use (or are) compilers.

    • > Would you consider that to be a compiler? :-)

      :) No. Under my definition there needs to be some non-trivial transformation into or out of an intermediate and/or target representation (either syntax-directed directly out of the parser, or from a materialized AST). Personally I would argue that even if you trivially "compiled" to sequential bytecode, if the bytecode is then interpreted it is hard to argue that you have created a compiler (the pre-JIT cpython interpreter is still an interpreter, even though it includes a bytecode representation). But I can see that this point can be argued, and historically a school exercise of writing a Pascal-to-pcode converter would be called a compiler, so sure, you can take that perspective if you like. Just don't get confused about whether you are learning to build a compiler (definition 1) or a compiler (definition 2).

      6 replies →

Honestly, antlr made this pretty straightforward to me. I didn't want to work in java, but they have a ton of targets. You can definitely write it all yourself, and that's a great learning exercise. But I wanted to get a parser for a language idea I had in mind and it took a couple of days with antlr (https://github.com/chicory-lang/chicory)

  • Problem with these tools is, you have to spend time wrangling them instead of learning to write a lexer/parser yourself.

    A recursive-descent parser is a beautiful thing and can be implemented very quickly.

5 days if you don't, but have the right tutor.

  • If you know basic programming (js/python), a day is more than enough to get the concept across using a tiny language. Could probably be done in an hour.

    The problem, always, is people are given terrible reading recommendations which makes the subject more complicated than it is.

    • I never took a compilers course. The first reading recommendation I got was The Dragon Book. Perhaps not the worst starting place at the time (mid 90s) but my memory of it was that the book was front-loaded with automata and parser theory. At the time I didn't really make it past the front end. It would be interesting to see a compiler tutorial that started at codegen and worked backwards (or started at the ends: lexing and codegen, and worked towards the middle in alternating chapters).

      6 replies →