3 ms·
I've been teaching a compiler class for 5 years. My experience has been that writing and teaching to write compiler is more efficient using horizontal step rath
by p4bl0 5y ago
I've been teaching a compiler class for 5 years. My experience has been that writing and teaching to write compiler is more efficient using horizontal step rather than vertical steps.
What I call vertical steps are lexing, parsing, typing, compiling, emitting output code.
What I call horizontal steps are language features going through all the vertical steps.
For example it is far better to start with a compiler for the "int language" (valid programs consist in a single integer and that's it), and then progressively add horizontal layers (features) to the language, being able to tests these new features each time. For example adding additions, then other binary operation, then multiple instructions, then variables and affectation, etc.
Next fall I will try the backward approach in my course (still using horizontal steps for building the project, but teaching the vertical steps backward). I'm quite convinced it will work better since I read this article: https://blog.sigplan.org/2021/02/23/teaching-compilers-backward/ https://blog.sigplan.org/2021/02/23/teaching-compilers-backw...
- Zababa 5y agoThanks for the advice, I've tried before to build a compiler with the vertical approach and it got really frustrating at some point. I will try again with the horizontal approach.
- p4bl0 5y agoCool :). The thing is to have an always start-to-end working compiler for an increasingly (even if the steps are very small) complex language. Another point would be to use simple tools at least to begin with. Racket [1] provides good lexer/parser tools [2] and you can use pattern matching [3] to browse and transform your AST. You can also target MIPS assembly [4] using the SPIM platform [5] for example, it is easy to learn and has very good debugging tools (basically the whole platform behaves like a gdb session). For the compiler implementation language OCaml is also a very good choice (even better because static typing will actually help you a lot with the horizontal approach) but it is less easier to setup as you need to install a lot more tools: ocaml itself, ocamllex (lexer), menhir (parser), ocamlbuild (automated build system), etc. [1] https://racket-lang.org/ https://racket-lang.org/ [2] https://docs.racket-lang.org/parser-tools/ https://docs.racket-lang.org/parser-tools/ [3] https://docs.racket-lang.org/guide/match.html https://docs.racket-lang.org/guide/match.html [4] http://www.cburch.com/cs/330/reading/mips-ref.pdf http://www.cburch.com/cs/330/reading/mips-ref.pdf [5] http://spimsimulator.sourceforge.net/ http://spimsimulator.sourceforge.net/
- Zababa 5y agoThanks for all the pointers, I didn't know about MIPS assembly, I'll take a look at it.