4 ms·
It's always great to see more people writing their own compilers - a "proof by example" that compilers do not have to be magical and mysterious special software
by userbinator 11y ago
It's always great to see more people writing their own compilers - a "proof by example" that compilers do not have to be magical and mysterious special software that only few experts can write. The theory may be deep, but in practice getting a basic working compiler doesn't require all that much.
I notice you're using a recursive-descent parser, which I think is a good idea because it can actually reduce complexity over one made with parser-generators; in fact it's simple enough that even beginners who have just grasped recursion should be able to understand and write one. Once you have a basic parser that can evaluate arithmetic expressions, extending that to generating AST nodes and parse a (mostly) simple language like C is not so difficult. If I remember correctly, at least one college has an expression evaluator as a first-year CS assignment related to recursion. It's interesting that one of the sections of K&R, on reading and writing C declarations, even contains a simple parser for them --- I'm not aware of any introductory books for other languages which contain small pieces of their implementation.
That said, you can use the "precedence climbing" technique[1] to collapse many of the recursive functions for most of the expression operators into one function with a succinct loop and a table, making your parser even simpler and faster. C4[2] is an example of this technique but with a series of if-else in the body of the function instead of a table, and I think is worth some careful study just for its mindblowingly awesome ridiculous simplicity.
[1] https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing
[2] https://news.ycombinator.com/item?id=8558822 https://news.ycombinator.com/item?id=8558822
- emptybits 11y agointroductory books for other languages which contain small pieces of their implementation I feel like two of the very few contenders for this might be books on Forth or Lisp, though still maybe not an "introductory" book.
- junke 11y agoSuch as Queinnec's Lisp in Small Pieces (https://en.wikipedia.org/wiki/Lisp_in_Small_Pieces https://en.wikipedia.org/wiki/Lisp_in_Small_Pieces)
- andrewchambers 11y agoI actually plan to do exactly that that for the preprocessor's expression parser. Thanks for the info!
- w23j 11y agoYou probably already know this stuff and don't need the info. But maybe somebody else reading this can find the link useful. This is a great tutorial on expression parsing using Pratt Parsers by Bob Nystrom: http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/ http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-e...
- david-given 11y agoC is notoriously painful to parse with a parser generator because it's not context free --- consider this statement: foo * bar; It can't be parsed until you know whether foo is a type or a variable --- indeed, depending on the implementation, it can't even be tokenised correctly. So you need hooks from the symbol table back into the tokeniser/parser. I'm not at all surprised that the author's using a hand-written parser, as it reduces the problem space considerably. (There's a really good article which I've been trying to find which goes into details about all the weird parsing edge cases due to the rules about exactly when a word goes into the symbol table which I've been completely unable to find...)
- andrewchambers 11y agoHere is a good one http://eli.thegreenplace.net/2012/07/05/how-clang-handles-the-type-variable-name-ambiguity-of-cc http://eli.thegreenplace.net/2012/07/05/how-clang-handles-th...
- yepguy 11y agoAs far as I know C is context-free. If C's grammar is ambiguous, that just makes it an ambiguous context-free grammar [1]. [1]: https://en.wikipedia.org/wiki/Ambiguous_grammar https://en.wikipedia.org/wiki/Ambiguous_grammar
- kazinator 11y agoThe context-free terminology doesn't apply to C because the ambiguity rests in not knowing what is the lexical category of foo in "foo * bar". That information comes from semantics. A language whose semantics you have to understand to understand its syntax is outside of the entire formal language realm inside which we formally recognize a "context-free" category. That formal language realm is purely syntactic: the terminal symbols are just "dumb" and stand for themselves. They are subject to rules which indicate how those symbols are combined. C is "quasi context free" in that if we resolve what the raw tokens mean (which one is a type and which one isn't), then the resulting language is context-free. In fact stronger than context free: amenable not only to LALR(1) parsing approaches but to recursive descent, with a suitable refactoring of the left-recursive rules given in the standard.