TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Writing a C Compiler: Build a Real Programming Language from Scratch

274 点作者 shoggouth9 个月前

22 条评论

signaru9 个月前
Have read the first few chapters and it expects that you either read the accompanying source code or implement your own and pass the tests. The pseudo code presented in the book often look like function calls with the inner details not there in the book. Furthermore, as already pointed out in another comment, the available implementation is in OCaml, which is probably not something many C programmers have experience with.<p>Nevertheless, I think I&#x27;m learning more from this book than most other books I&#x27;ve tried before that are far more theoretical or abstract. I&#x27;m still eager to reach the chapter on implementing C types. I think it&#x27;s a good book, but it requires more effort than something like Crafting Interpreters or Writing a Compiler&#x2F;Interpreter in Go, while also covering topics not in those books.
评论 #41257836 未加载
评论 #41258126 未加载
synack9 个月前
I’ve been working through this book implementing the compiler in Ada. So far, I’m really enjoying it. The book doesn’t make too many assumptions about implementation details, leaving you free to experiment and fill in the blanks yourself.<p>It feels like a more advanced version of Crafting Interpreters.<p>I haven’t looked at the OCaml implementation at all. The text and unit tests are all you need.<p>Discussion on the Ada Forum: <a href="https:&#x2F;&#x2F;forum.ada-lang.io&#x2F;t&#x2F;writing-a-c-compiler&#x2F;1024" rel="nofollow">https:&#x2F;&#x2F;forum.ada-lang.io&#x2F;t&#x2F;writing-a-c-compiler&#x2F;1024</a>
francogt9 个月前
I see many comments saying that the book implements the C compiler in ocaml. In the introduction the author states that the book actually uses pseudo code so you are actually free to implement it in any language. The only recommendation is that you use a language with pattern matching because the pseudo code makes heavy use of it. The reference implementation is in ocaml.
评论 #41260968 未加载
评论 #41261071 未加载
评论 #41258922 未加载
评论 #41261930 未加载
WalterBright9 个月前
I learned how to write a compiler by studying BYTE magazine in the 70&#x27;s which published the source to a complete Pascal compiler as an article!<p><a href="https:&#x2F;&#x2F;archive.org&#x2F;details&#x2F;byte-magazine-1978-09" rel="nofollow">https:&#x2F;&#x2F;archive.org&#x2F;details&#x2F;byte-magazine-1978-09</a> (part 1)<p>All 3 parts of Tiny Pascal:<p><a href="https:&#x2F;&#x2F;albillo.hpcalc.org&#x2F;publications&#x2F;Easter%20Egg%20-%20TBBOP.pdf" rel="nofollow">https:&#x2F;&#x2F;albillo.hpcalc.org&#x2F;publications&#x2F;Easter%20Egg%20-%20T...</a>
评论 #41265298 未加载
评论 #41258833 未加载
评论 #41265014 未加载
hasbot9 个月前
So what&#x27;s different about writing a compiler in 2024 than say 10, 20, or 30 years ago? When I started writing compilers in the 80&#x27;s and 90&#x27;s lex&#x2F;flex and yacc&#x2F;bison were popular. ANTLR came out but I never had a chance to use it. Everything after lexing and parsing was always hand rolled.
评论 #41257656 未加载
评论 #41258956 未加载
评论 #41257132 未加载
评论 #41255924 未加载
评论 #41256508 未加载
评论 #41255195 未加载
jerjerjer9 个月前
I uh misread the title and thought someone built a C compiler in Scratch.<p>On topic, though: wouldn&#x27;t a simpler language (maybe even a pseudo language) be a better target for a first learning compiler. I understand they don&#x27;t build a full C compiler, but still. It looks to me like there&#x27;s a lot of complexity add from choosing such a lofty target.
评论 #41259517 未加载
fuhsnn9 个月前
chibicc[0] complement this book nicely, in addition to a basic compiler, it guides you through writing the preprocessor and driver, which, although not addressed much in literature, are the missing link between the compiler built from the book and real C projects.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;rui314&#x2F;chibicc">https:&#x2F;&#x2F;github.com&#x2F;rui314&#x2F;chibicc</a>
评论 #41266254 未加载
carom9 个月前
I took a compilers course in university and the course culminated in having a compiler for C Minus (a subset of C). The professor noted how each year the line count of the compilers was dropping as students found ways libraries or languages that made it easier. I think the evolution was Java -&gt; Antlr -&gt; Python. I used OCaml and emitted LLVM and blew that metric out of the water.
评论 #41261639 未加载
the_panopticon9 个月前
In Ocaml, interesting. I was similarly surprised when I learned that the firs Rust compiler was written in Ocaml, too <a href="https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;understanding-how-the-rust-compiler-is-built&#x2F;87237&#x2F;7" rel="nofollow">https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;understanding-how-the-rust-com...</a>
评论 #41257657 未加载
评论 #41257633 未加载
Coolbeanstoo9 个月前
This looks cool, been interested in learning more about compilers since I did the basics in college. Lots of things seem to focus on making interpreters and never make it to the code generation part so its nice to see that this features information about that.
评论 #41255505 未加载
shoggouth9 个月前
It also will be available via Amazon after August 20, 2024.<p><a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Writing-Compiler-Programming-Language-Scratch&#x2F;dp&#x2F;1718500424&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Writing-Compiler-Programming-Language...</a>
sergius9 个月前
How does it compare with N.Wirth&#x27;s?<p><a href="https:&#x2F;&#x2F;onlinebooks.library.upenn.edu&#x2F;webbin&#x2F;book&#x2F;lookupid?key=olbp42539" rel="nofollow">https:&#x2F;&#x2F;onlinebooks.library.upenn.edu&#x2F;webbin&#x2F;book&#x2F;lookupid?k...</a>
评论 #41261167 未加载
评论 #41255836 未加载
评论 #41258184 未加载
tzs9 个月前
I don&#x27;t really need to know how to build a compiler, and I&#x27;ve got enough other &quot;don&#x27;t need but am doing out of curiosity&quot; things going on that I don&#x27;t need any more of those, but if it wasn&#x27;t $70 I&#x27;d probably get it anyway. It would be interesting to compare to the last building a compiler book I read back and see how things have changed. Based on the comments here a lot has changed.<p>That last book was Allen Holub&#x27;s &quot;Compiler Design in C&quot;, which is from 1990. Here&#x27;s how the blurb on the back describes it:<p>&gt; Allen I. Holub&#x27;s Compiler Design in C offers a comprehensive, new approach to compilers that proves to be more accessible to computer science students than the other strictly mathematical books.<p>&gt; With this method in mind, the book features three major aspects:<p>&gt; (1) The author develops fully functional versions of lex and yacc (tools available in the UNIX® operating system to write compilers), (2) he uses lex and yacc to develop a complete C compiler that includes parts of C that are normally left out of compiler design books (eg., the complete C &quot;type&quot; system, and structures), and (3) the version of yacc developed here improves on the UNIX version of yacc in two ways (error recovery and the parser, which automatically produces a window-oriented debugging environment in which the parse and value stacks are visible).<p>It&#x27;s out of print, but the author has made a searchable PDF available on his website [1]. I found it quite useful.<p>Holub seems to like the &quot;learn by doing&quot; approach. He&#x27;s got another book, &quot;Holub on Patterns&quot; that teaches all the design patterns from the gang of four book organically by developing two programs that together use all of those patterns. The two programs are an embedded SQL interpreter and a GUI application for Conway&#x27;s Game of Life.<p>PS: Ooh. It occurred to me that No Starch Press books are often available on O&#x27;Reilly Learning. I checked and this one is there. So I guess it is going on my &quot;don&#x27;t need but am doing out of curiosity&quot; pile after all.<p>[1] <a href="https:&#x2F;&#x2F;holub.com&#x2F;compiler&#x2F;" rel="nofollow">https:&#x2F;&#x2F;holub.com&#x2F;compiler&#x2F;</a>
whartung9 个月前
What approach does this book take to error recovery?<p>Several &quot;compiler light&quot; style articles and books kind of walk over that part, and it can be non-trivial to do properly, especially with modern expectations.<p>I remember way back in the day, one of the early C compilers for the PDP, and, honestly, it could almost be argued that ed(1) had better error messages than what that thing produced.<p>A lot of simple compilers hit an error and just give up.<p>So, just curious what the approach was in this book.
badsectoracula9 个月前
Weird that this is about building a <i>C</i> compiler[0] in <i>OCaml</i>. I expected the implementation language to also be C both for consistency but also because i&#x27;m willing to bet that there are more people who can read C than OCaml.<p>[0] actually from the readme in the github repo[1] it seems to be a C subset, not all of C<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;nlsandler&#x2F;nqcc2">https:&#x2F;&#x2F;github.com&#x2F;nlsandler&#x2F;nqcc2</a>
评论 #41255561 未加载
评论 #41258394 未加载
评论 #41256928 未加载
评论 #41256263 未加载
评论 #41255487 未加载
quibono9 个月前
I swear I&#x27;ve seen this cover before... is this a new release or an updated edition of an older book?
评论 #41254669 未加载
评论 #41254526 未加载
评论 #41257857 未加载
评论 #41254868 未加载
评论 #41255001 未加载
sunday_serif9 个月前
I’m working through this book now and really enjoying it!<p>Each chapter of the book includes a test suite to run against the code you’ve written.<p>In some ways, the tests in this book feel very similar to the labs in the book Computer Systems: A programmers perspective — which is high praise!
alok-g9 个月前
I would love to see a book that talks about going all the way to generate machine code, i.e., not stopping at generation of assembly.<p>Alternatively, I would like to learn about not just how to make a compiler, but also simultaneously a debugger, hot-reloading, etc.
评论 #41256595 未加载
评论 #41255797 未加载
sim7c009 个月前
cool, remember some tutorials online i think from the same author (not 100% sure) doing stuff around c compilation in python. shame its not in a language i want to learn. the other book on compilers i got is almost to heavy to lift! :D
i_don_t_know9 个月前
Somewhat unrelated: Is there a book that walks you through building a database system from storage to queries, optimizer, execution, indexing, transactions, etc?
评论 #41256437 未加载
评论 #41254757 未加载
评论 #41255653 未加载
评论 #41255602 未加载
评论 #41254775 未加载
sylware9 个月前
I wonder why there is not the same book for c++... mmmmh... I really wonder... (irony).
评论 #41259879 未加载
viraj_shah9 个月前
Dropping this one here! (no affiliation)<p><a href="https:&#x2F;&#x2F;www.linuxfromscratch.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.linuxfromscratch.org&#x2F;</a><p>&quot;Linux From Scratch (LFS) is a project that provides you with step-by-step instructions for building your own custom Linux system, entirely from source code.&quot;
评论 #41257727 未加载
评论 #41259059 未加载