[HN Gopher] Thought Experiment: An Introductory Compilers Class ___________________________________________________________________ Thought Experiment: An Introductory Compilers Class Author : ingve Score : 46 points Date : 2020-02-17 17:32 UTC (5 hours ago) (HTM) web link (semantic-domain.blogspot.com) (TXT) w3m dump (semantic-domain.blogspot.com) | abatoir wrote: | I tried to add a comment on the source page, but that's not | possible if you don't have a Google account. So I'll add it here. | | There are two great resources for an introduction to compilers. | First, "The Unix Programming Environment" has a chapter on | programming tools which builds the equivalent of the Unix 'dc' | desk calculator. This includes variables and procedures, so it's | non-trivial. | | The second one was first posted to USENET by (as I recall) Jack | Ganssle called, "Let's Build a Compiler". This is notable because | it builds a recursive-descent compiler, rather than the usual | LALR ones. | | Considering the intent was a course for beginners, I think much | of what that page wants to cover is more suitable for a _second_ | course. If you go through the UPE book first, you'll be in a much | better position to slog through the Dragon book and get all those | low-level things. | mjevans wrote: | I feel like this might still be too high level. A better approach | may be to teach by the example of writing a basic FORTH | environment from machine instructions, then basic helpers written | in the language, and working up from there. | | Various types of better things can be added in once there's | something to optimize. | | Edit: I was probably thinking of this version that I likely read | a couple years ago: | | https://news.ycombinator.com/item?id=13505285 | | Quoting their post | | "" rwmj on Jan 28, 2017 [-] | | This is a how-to-write-a-FORTH tutorial that I wrote a few years | ago. It's particularly nice that you get to see how various | control structures are implemented, like IF, CASE and even | comments! | | Part 1: | http://git.annexia.org/?p=jonesforth.git;a=blob;f=jonesforth... | | Part 2: | http://git.annexia.org/?p=jonesforth.git;a=blob;f=jonesforth... | | (github mirror: | https://github.com/AlexandreAbreu/jonesforth/blob/master/jon... | https://github.com/AlexandreAbreu/jonesforth/blob/master/jon... ) | | Previous HN comments: | https://news.ycombinator.com/item?id=10187248 | | "" | | The immediate reply covers my feelings for how the | __introductory__ class should proceed: minimal assembly and | maximum programmer clarity. | | "" rsaarelm on Jan 28, 2017 [-] | | Working through Jones Forth got me up to speed on both | understanding how Forth works and getting my hands dirty with | some practical assembly coding. | | Once I got a better idea of Forth, I also realized that Jones | stays in assembly for rather long. He builds up the entire | interpreter from raw assembly words, because you need the | interpreter to start parsing textual Forth source. But you if you | could somehow write Forth before the interpreter is put together, | it would be quite natural to switch to Forth much earlier. And it | turns out you can do that. Jones even defines the `defword` | macro. But for some reason he makes very little use of it. | Rewriting most of the code leading up to INTERPRET using defword | was a fun exercise. | | The next step would've been making the whole system bootstrapping | by rewriting the assembler in Forth, but x86 machine code | generation is hairy enough that I bailed out at this point. "" | not2b wrote: | Introductory compilers classes should drop LR parsing as a topic. | It takes a huge amount of time to teach and while it is | mathematically elegant, it isn't really a good approach to | writing a compiler front end that produces high-quality error | messages. Teach recursive descent and move on to more interesting | topics. | andrewflnr wrote: | I disagree a bit with the premise. I feel that the most important | part of a compilers course is communicating that _compilers are | not magic_ , that none of this is magic. Good developers need to | be able to see through their abstractions when necessary, and | it's too easy to treat compilers as a black box. | | As a slightly different concern, I think the curriculum above | runs the risk of giving the impression that even if compilers | aren't magic, they definitely need a lot of heavy duty abstract | math. I'm not in favor of this either. | | With that in mind, my ideal compilers course would probably take | an intuituve approach to lexing, recursive decent parsing, and | extremely simple tree-walking, stack-based code generation. Keep | calm, it's all just data and basic algorithms. | | My college education had the effect of lowering my expectations | of my fellow students, so a more sophisticated course may be | appropriate in some cases. YMMV. | musicale wrote: | > Keep calm, it's all just data and basic algorithms | | This is a good approach for all introductory systems courses. | It's also a good approach for most real-world systems. | joe_the_user wrote: | _" I disagree a bit with the premise. I feel that the most | important part of a compilers course is communicating that | compilers are not magic, that none of this is magic"_ | | It depends what you mean by "magic". I think compilers are one | of the hardest things for a programmer to learn _naively_. I | 've had co-workers who were semi-self taught and who simply | didn't get the idea of an abstract language - it really is a | mathematical construction that's somewhat difficult (making a | language into a "object" is generally a mistake). For example, | it's much more coherent to write a parser that will check | whether X string properly specifies file than to just start | writing loops and checking values (of course, you really want | to just ask the system but that's a different point). | | A smart-ish person can just wing-it for a variety of | programming tasks, say simple string-processing tasks or | computer games, but there are definitely places in compiler | construction where you have to know what you're doing, where | you have to learn an abstract idea and not just a bunch of | specifics. | | What I'd like, in a sense, is a map showing which parts of this | process are parts where nothing standard and which parts are | all kind of the same because it doesn't seem like most compile- | construction mark out their in anything like this way. | microtherion wrote: | My favorite introductory compiler book is Niklaus Wirth's | "Compiler Construction", which implements a compiler end-to-end | with full source code, in a bit more than 100 pages: | https://inf.ethz.ch/personal/wirth/CompilerConstruction/inde... ___________________________________________________________________ (page generated 2020-02-17 23:00 UTC)