6 ms·
I don't think one can understand compilers in a "week-end".
by zvmaz 2y ago
I don't think one can understand compilers in a "week-end".
- haqreu 2y agoThe "week-end" part refers to the fact that I wrote it in one week-end with little to no experience in compiler design. You can check the commit history, 12 to 14 Jan, 2024 :)
- not_a_bot_4sho 2y agoI think the "week-end" reference up above was a little tongue in cheek, since the modern spelling is a compound word: https://linguix.com/english/common-mistake/week_end_hyphen https://linguix.com/english/common-mistake/week_end_hyphen Not to take away from this awesome sharing.
- mattgreenrocks 2y agoOne weekend is enough to survey the general concepts. That’s extremely useful on its own, as now you have a better idea of how to orient yourself.
- ramon156 2y agoYou set your own bar. you could understand "how a compiler" in a day, but how much do you want to know?
- sieve 2y agoCompilers 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.
- sitkack 2y ago5 days if you don't, but have the right tutor.
- sieve 2y agoIf 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.
- RossBencina 2y agoI 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).
- sieve 2y agoThe Dragon Book is terrible as an introduction. There are better books I would probably recommend, but not to a beginner. The best "complete" intro out there today is Nystrom's Crafting Interpreters.[1] [1] https://www.craftinginterpreters.com/contents.html https://www.craftinginterpreters.com/contents.html
- RossBencina 2y ago> There are better books I would probably recommend I'm curious what you'd recommend. For what it's worth my goal was to compile to machine code. Anything less would have seemed insufficient. Later I got Appel's "Modern Compiler Implementation in Java" and Allen and Kennedy "Optimizing Compilers for Modern Architectures". Cooper and Torczon "Engineering a Compiler" was recommended here recently. I haven't seen it.
- jcuenod 2y agoHonestly, 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 https://github.com/chicory-lang/chicory)
- sieve 2y agoProblem 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.
- whartung 2y agoThe 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.
- andai 2y agoIs 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.
- lelanthran 2y agoIt'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 :-).
- PhilipRoman 2y agoGood 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).
- sieve 2y ago> 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-precedence-parsing/ https://eli.thegreenplace.net/2010/01/02/top-down-operator-p...
- RossBencina 2y ago> 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/ https://ssloy.github.io/tinyoptimizer/
- sieve 2y ago> 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.
- RossBencina 2y ago> 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).
- duped 2y agoAll a compiler does is take a well defined data structure and transform it into another, sometimes by encoding or sometimes by decoding. Really it's bread and butter stuff that every engineer does everyday but with the benefit that you can say something is undefined/out of scope way easier than talking to APIs you don't control. The fun part are the magic algorithms you sometimes get to use
- jcranmer 2y ago> All a compiler does is take a well defined data structure and transform it into another Sorry, having worked on language semantics topics, "well-defined" is not how I would describe either a programming language or the behavior of machine code.
- storoj 2y agoThere should be a well-defined list of undefined behaviors.
- deleted 2y ago[deleted]