TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: A work-in-progress C compiler from scratch

159 pointsby r1chardnlalmost 4 years ago

10 comments

jjicealmost 4 years ago
This is just an anecdote, but I had to write a SQL DDL and DML parser during my last semester. I wrote the DDL parser by hand, and it wasn&#x27;t as bad as I expected, but it was time consuming. I managed to convince the professor to give us the option of using a parser generator for the next phase (DML) since the point of the class wasn&#x27;t parsing context free grammars and more focused on executing the SQL.<p>I used Flex and Bison since the project was in C. Getting up and running and understanding how the tools have to be set up with different options was a bit tricky, but after that, my parser was up and running in about two hours, compared to probably four times that for the hand written DDL. Our DML subset was also much larger and more complex than our DDL, so I was very happy with the development speed increase.<p>I had this idea that using a parser generator was slow and wasteful since many modern tutorials online write them by hand and speak against parser generators (possibly because there isn&#x27;t a catch all for all languages). Turns out dev speed is way more important to me up front, because in the case that I notice parsing speed actually being an issue I should be happy that my MVP has gotten enough use.<p>It&#x27;s also nice because a lexer and parser can be pretty easily black-boxed and swapped out for your hand written, just keep the AST and API the same and you should be good.<p>All that said, that&#x27;s personal preferences and writing the parser by hand is definitely good experience and more extensible, especially for error handling. Nice work!
评论 #28051990 未加载
评论 #28055150 未加载
评论 #28052390 未加载
MisterTeaalmost 4 years ago
&gt; <i>Programming language that compiles into a x86 ELF executable.</i><p>&gt; <i>a compiler somewhat resembling C</i><p>&gt; <i>Add new or borrow from other language(s) features ontop of C, deviate away from just C</i><p><pre><code> function strlen(const char *s) </code></pre> This does not appear to be a C compiler. The title should reflect this.
评论 #28049129 未加载
评论 #28051069 未加载
评论 #28052889 未加载
thameralmost 4 years ago
Just a few small suggestions:<p>1. Some code seems to be duplicated (although with slightly different formatting) across multiple files, like dd&#x2F;dw&#x2F;db in elf.c and x86.c for example. I&#x27;d suggest consolidating those.<p>2. It helps to define types when you have variables that can take a limited set of possible values; typedef works but enums are better. I&#x27;m thinking of variables like the `type` parameter to `primitive_data_type_size`, for example. The compiler can help you detect a missing case statement if you use an enum, but not an int.<p>3. It&#x27;s a matter of preference, but typedef&#x27;ing your structs can make your code more concise and not have it littered with struct keywords.<p>4. You seem to have a mix of tabs and spaces in some files (e.g. in `elf.c`). I would recommend configuring your editor to use just one of the two or it&#x27;ll start to be an issue as your code base grows.<p>5. On a related point, I&#x27;d suggest picking a formatting style and sticking to it. You have functions like `accept` in `ast.c` declared without spaces after the opening `(` and others like `read_file` in the same file that has spaces.<p>Good luck! This is a great way to learn.
muth02446almost 4 years ago
Shameless plug: if you are having a lot of fun with the &quot;front-end&quot; part of language design but neither like the idea of writing your own backend nor integrating with LLVM, maybe Cwerg is right for you: <a href="https:&#x2F;&#x2F;github.com&#x2F;robertmuth&#x2F;Cwerg" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;robertmuth&#x2F;Cwerg</a>
justinlloydalmost 4 years ago
Great job! Writing a compiler from scratch is a massively valuable learning experience. I wrote one in BASIC for the 6502, that became self-hosting on April 24th, 1982. I&#x27;m going to watch your project with interest and relive old memories.
SavantIdiotalmost 4 years ago
Every time I see a compiler, the first thing a zero in on is the parser.<p>I don&#x27;t mean this in a bad way, but that tokenizer looks a bit ... simple. ;-)<p>I say that because ~26 years ago I had a go at writing a C preprocessor with the goal of creating a ToC and Index for my C projects. I sat down with a BNF of C and lex&#x2F;yacc and got ground up in the details, then found some software that did exactly what I wanted and moved on. I just remember a few parts of the C syntax that don&#x27;t fit nicely into BNF and hadn&#x27;t studied parsers enough to dig myself out of the hole.<p>Good luck doing it manually, that&#x27;s gonna be tricky as heck.
评论 #28052104 未加载
quantumOctopusalmost 4 years ago
Incredibile timing as I&#x27;m working on my own language as well (just started a week ago) and I&#x27;m literally starting from 0. So happy that in just a week I can understand at least half of this thread. Your project looks a bit too wild for me but I&#x27;m sure there&#x27;s plenty I can learn there so thank you so much for sharing!
kinga254almost 4 years ago
Any specific resources you&#x27;ve used that you could recommend in line with compiler construction?
评论 #28051507 未加载
评论 #28050687 未加载
评论 #28050782 未加载
评论 #28051590 未加载
gwbas1calmost 4 years ago
What are your goals? Is this a learning project? Are you trying to experiment with some kind of compiler architecture?
评论 #28051003 未加载
bruce343434almost 4 years ago
Good luck with your project and language!
评论 #28049097 未加载