5 ms·
I have been following this for the past few weeks in my off-time and would recommend it to everyone. It is also geared towards teaching C. I wrote in C many yea
by cabaalis 8y ago
I have been following this for the past few weeks in my off-time and would recommend it to everyone. It is also geared towards teaching C. I wrote in C many years ago but with the past 8 or so years writing exclusively C# it feels like a homecoming.
It does make use of the author's grammar parser, which is my only minor complaint. Does anyone know a good resource in which the author handcrafts a parser? I'm aware of the various parser generators that exist. I also read the dragon book in my college's compiler design course ~12 years ago. But we also used a parser generator in that class.
- AlexeyBrin 8y ago> Does anyone know a good resource in which the author handcrafts a parser? http://craftinginterpreters.com/contents.html http://craftinginterpreters.com/contents.html the book implements the Lox language in Java and C.
- nur0n 8y agoI read through parts of this book a few months back. I also did not like using that parser because I wanted to keep external dependencies to a minimum. Writing a parser for s-expressions turned out to be relatively simple. You can check out my implementation here: https://github.com/00vareladavid/scheme/blob/master/src/parse.c https://github.com/00vareladavid/scheme/blob/master/src/pars... . I believe the technique is called "recursive descent parsing".
- drblast 8y agohttp://www.lispworks.com/documentation/HyperSpec/Body/02_b.htm http://www.lispworks.com/documentation/HyperSpec/Body/02_b.h... That's the algorithm for the common lisp reader. Writing a recursive descent parser based on that spec is fairly straightforward. And once you hand write your own recursive descent parser you'll probably not want to give up the control that gives you to a parser generator. Yuck.
- deleted 8y ago[deleted]
- _d8fd 8y agoI was playing with a parser generator last summer, and it was immensely frustrating to bump into bugs it had & features it lacked. In the end, it was much easier easier to write the entire parser myself. So if I understand the point you're making, I agree that giving up control can suck!
- Jugglerofworlds 8y agoYou should check out parser combinators. I've also used parser generators in the past and experienced similar frustrations. Parser combinators are just so much easier to use.
- atomical 8y agoHey, I saw your comment on ketamine but couldn't reply. Are you still taking it?
- andrepd 8y agoAnd then you will write a parser generator with Parsec and you will not want to give up the ease compared to writing your own recursive descent parser :)
- fiddlerwoaroof 8y agoYeah, parser combinators are basically magic, and not in an “overly clever code that you’ll regret later” sense. And, while a Haskell-style type system makes them nicer, they work really well in dynamically typed languages too. (e.g. https://github.com/drewc/smug https://github.com/drewc/smug )
- pjc50 8y agoI'd love a good parser generator to save me writing recursive descent ones roughly annually in my career, but the ones I've encountered are awful.
- guntars 8y agoI found LLVM’s Kaleidoscope tutorial to be great for learning how to write a parser from scratch. https://llvm.org/docs/tutorial/LangImpl02.html https://llvm.org/docs/tutorial/LangImpl02.html
- UncleEntity 8y agominischeme[0] has a pretty good parser built into the SECD machine. I've been hacking on it here and there and almost have it r4rs compliant -- replaced the lexer with re2c and was going to do the parser in lemon but the design they already have is just too simple to mess with. [0]https://github.com/catseye/minischeme https://github.com/catseye/minischeme --note: the bog stock original is floating around on the internets if you google hard enough.
- shawn 8y agoHand crafted lisp parser: https://github.com/sctb/lumen/blob/master/reader.l https://github.com/sctb/lumen/blob/master/reader.l Equivalent JavaScript: https://github.com/sctb/lumen/blob/master/bin/reader.js https://github.com/sctb/lumen/blob/master/bin/reader.js It’s 158 lines of lisp, fully bootstrapped, and zero fanciness.
- svat 8y agoThe "Let's Build A Simple Interpreter" series of posts (starting at https://ruslanspivak.com/lsbasi-part1/ https://ruslanspivak.com/lsbasi-part1/) is excellent, geared towards building an interpreter for a Pascal-like language in Python. It does not use a parser generator, and builds up everything incrementally. Unfortunately it's incomplete...
- ModernMech 8y agoHonestly, if I were you I would try to write it blind. It's one of the more rewarding things to write, because when you do it correctly, it collapses down to a bunch of macro calls. You'll start off writing it long hand, and eventually you'll start to see some patterns. Then you'll see the need for certain combinators, and then finally you'll see how everything collapses down. It's one of the more rewarding things to write if you're a fan of DRY, and the fuzzy feeling you get when you properly abstract something.
- ktg 8y agoHand-crafted parser: Building LISP: http://www.lwh.jp/lisp/index.html http://www.lwh.jp/lisp/index.html - Parser: http://www.lwh.jp/lisp/parser.html http://www.lwh.jp/lisp/parser.html Full source: https://github.com/kimtg/ToyLisp https://github.com/kimtg/ToyLisp