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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

How I wrote a self-hosting C compiler in 40 days

429 点作者 rui314超过 9 年前

19 条评论

bad-joke超过 9 年前
I really enjoyed reading this. It&#x27;s informative, fun, and has a refreshingly honest tone. Too often, stories passed around by computer scientists entail clever solutions and elegant insight striking the protagonist like lightning in the hour of need. Rarely does the programmer express regret, make self-corrections, and confront fear and doubt along the way:<p>&gt;I should have written beautiful code from the beginning, but because I needed to learn by writing a working code, the rewriting was unavoidable.<p>&gt;I should probably change my mind to implement all the features from end to end. I may find it fun as I&#x27;m approaching the goal. Sometimes, I have to write more code than I want to write in order to achieve a goal.<p>&gt;In a tough situation like this, I probably should recall the fact that the compiler was just in one file, to see how much progress I&#x27;ve made in a month. It just reads an integer with scanf() and prints it out with printf(). Really, I made so much progress in one month. Yeah, I think I can do this.
评论 #10733172 未加载
评论 #10735319 未加载
jaybosamiya超过 9 年前
&gt; I suspect that he [Dennis Ritchie] invented a syntax, wrote code for it, which turned out to be more complicated than he had expected. And that was eventually standardized by the ANSI committee. It&#x27;s hard to implement a standardized language because you have to get everything right. It&#x27;s rather easy to write your own toy language.<p>Love those lines
评论 #10732516 未加载
draugadrotten超过 9 年前
Interesting, and quite funny to read with a sense of humour that reminds me of the movie PI. The author of the compiler goes from rational to something more... spiritual.<p>&quot;Day 52<p>I was looking for a mysterious bug for three days: ...&quot;<p><a href="http:&#x2F;&#x2F;www.imdb.com&#x2F;title&#x2F;tt0138704&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.imdb.com&#x2F;title&#x2F;tt0138704&#x2F;</a>
tomcam超过 9 年前
I was a little surprised that the author was able to manage both C11 and the preprocessor in that time. The preprocessor is hard. But there was existing code from a previous version of it, which makes sense. Still, a fantastic achievement! Congrats to the author!
评论 #10732574 未加载
sdegutis超过 9 年前
Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.
评论 #10731384 未加载
评论 #10731600 未加载
评论 #10731293 未加载
评论 #10731322 未加载
评论 #10731295 未加载
xigency超过 9 年前
Honestly, the most difficult, time consuming, and mundane aspect to this project would have to be the parser, which was apparently written in C by hand. So bravo.<p>Getting to some of the final notes:<p>&gt; ... I&#x27;d choose a different design than that if I were to write it again. Particularly, I&#x27;d use yacc instead of writing a parser by hand and introduce an intermediate language early on.<p>That&#x27;s why I found the LALRPOP post by one of the Rust developers interesting. Writing your own parser generator is actually much easier than writing a parser by-hand (depending on the complexity of the language, here not that complex and still difficult), and I think it&#x27;s more instructive than using a free or open parser-generator or compiler compiler. The downside is that it is less practical, because almost none of the important aspects of language implementation involve the parser.
评论 #10733989 未加载
评论 #10733439 未加载
yeison超过 9 年前
What does it mean to be &#x27;self-hosting&#x27; here? Does it just mean that it&#x27;s a compiler that can compile itself?
评论 #10731704 未加载
评论 #10731707 未加载
评论 #10742751 未加载
评论 #10742753 未加载
peterkelly超过 9 年前
For anyone interested in compiler writing and looking for a good resource to start, probably one of the best is the &quot;Dragon Book&quot;:<p><a href="http:&#x2F;&#x2F;www.amazon.com&#x2F;Compilers-Principles-Techniques-Tools-2nd&#x2F;dp&#x2F;0321486811" rel="nofollow">http:&#x2F;&#x2F;www.amazon.com&#x2F;Compilers-Principles-Techniques-Tools-...</a><p>I highly recommend it, but it&#x27;s <i>heavy</i> stuff. There are probably simpler guides out there that just cover the basics.
评论 #10733017 未加载
评论 #10732136 未加载
评论 #10732256 未加载
评论 #10732890 未加载
评论 #10732162 未加载
评论 #10742248 未加载
jlappi超过 9 年前
This is really interesting, and I was glad to see the author go beyond just the stated 40 days and give insight into where they went after it was &#x27;self-hosting.&#x27;
peter303超过 9 年前
Long ago UNIX had compiler writing tools like yacc and lex. I wonder if they are useful for exercises like this.
评论 #10732067 未加载
评论 #10732219 未加载
评论 #10732084 未加载
评论 #10732591 未加载
评论 #10732964 未加载
评论 #10732595 未加载
allannienhuis超过 9 年前
&gt; I have a mixed feeling — I learned a new stuff, but I could have learned that without spending this much time.<p>Story of my life!
pagade超过 9 年前
Anyone tried using it? How do I use it to generate executable (as per the code it should fork the &#x27;as&#x27;)?<p>Getting following error: [ERROR] main.c:144: (null): One of -a, -c, -E or -S must be specified<p>-c, -E and -S are working fine. Couldn&#x27;t figure out from code what -a does.
评论 #10733833 未加载
sabujp超过 9 年前
1) write compiler 2) get a job at google . . 4) profit
评论 #10732903 未加载
joe563323超过 9 年前
This is awesome. I wish more of top coders post their flow of thoughts so the betas can learn from it.
andrewchambers超过 9 年前
rui314, thanks for posting this, you really were such a big inspiration to me writing my own. You were really quick!
pskocik超过 9 年前
Now repeat for C++.
评论 #10732008 未加载
评论 #10732312 未加载
kleiba超过 9 年前
What do you mean, &quot;as a child&quot;?
评论 #11320263 未加载
评论 #10734035 未加载
评论 #10735624 未加载
andrewvijay超过 9 年前
Unbelievable Jeff!
pinn4242超过 9 年前
Self-hosting is pointless. Go has it--who cares. I wrote a program 1000x faster than the ruby one (at work) with zero bugs in Go, but I still don&#x27;t want to use it(Go). Java is fine. Do I care if Java is self-hosting? No. I&#x27;ll do (another) language in Javacc (my first one is still awesome) or ANTLR.
评论 #10734085 未加载