Live data from Hacker News

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

nostarch.com

31–40 of 159 posts

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

#31
post #22

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. Alternatively, I would like to learn about not just how to make a compiler, but also simultaneously a debugger, hot-reloading, etc.

Writing an simple assembler is trivial. Even macro assemblers are very easy.

However, it's also boring.

Nevertheless the contents of the book cover all the techniques required to write an assembler, if you'd really like to

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

#32
post #17

How does it compare with N.Wirth's? https://onlinebooks.library.upenn.edu/webbin/book/lookupid?k...

The book is a very hands on tutorial whereas Wirths is basic literature for the general case.

While they teach similar content, they have a different approach.

There are literally thousands of compiler design books out there, I don't really see anything particularly comparable between this book and Wirth's

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

#33
post #10
post #3

I swear I've seen this cover before... is this a new release or an updated edition of an older book?

Many compiler related books take inspiration from the "Dragon book" (Compilers: Principles, Techniques and Tools). So with likely lots of books with similar looking covers.

The cover looks nothing like the dragon book however?

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

#34
post #12
post #4

Earlier quoted context omitted.

Book is not yet published but in early access since a couple of years Was featured here a couple of times. Unfortunately the timing of the release is quite unfortunate with regards to the summer holidays. Will take a look at it next year

> Book is not yet published but in early access since a couple of years According to the top post's link, it was released in July 2024.

I seem to be mistaken. The 20th of August as listed by Amazon must be the European release date then

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

#35
post #22

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. Alternatively, I would like to learn about not just how to make a compiler, but also simultaneously a debugger, hot-reloading, etc.

Writing an simple assembler is trivial. Even macro assemblers are very easy. However, it's also boring. Nevertheless the contents of the book cover all the techniques required to write an assembler, if you'd really like to

I understand that assembly file can be parsed in the same way. However, I want to learn about the machine instructions to the level of bits, and likewise the layouts of binary files. Unless I am able to go all the way to machine code loaded in memory, I would not know where in memory to add a breakpoint instruction when a developer wants the same on a line of code.

If there is some library that can help create machine code from assembly instructions on a line by line basis (at least as opposed to invoking a separate program that generates the entire binary collectively from the assembly code), that could also work.

In my case, I already know enough of the lexer, parser, etc., parts. What's missing is going all the way to making a debugger, profiler, etc.

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

#36
post #14

So what's different about writing a compiler in 2024 than say 10, 20, or 30 years ago? When I started writing compilers in the 80's and 90's lex/flex and yacc/bison were popular. ANTLR came out but I never had a chance to use it. Everything after lexing and parsing was always hand rolled.

Compiling today might be done at run time, exploiting dynamic information. The line between a compiler and an interpreter is blurred.

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

#37
post #35

Earlier quoted context omitted.

Writing an simple assembler is trivial. Even macro assemblers are very easy. However, it's also boring. Nevertheless the contents of the book cover all the techniques required to write an assembler, if you'd really like to

I understand that assembly file can be parsed in the same way. However, I want to learn about the machine instructions to the level of bits, and likewise the layouts of binary files. Unless I am able to go all the way to machine code loaded in memory, I would not know where in memory to add a breakpoint instruction when a developer wants the same on a line of code. If there is some library that can help create machin…

>If there is some library that can help create machine code from assembly instructions on a line by line basis

That's what JIT libraries do, for example asmjit: https://github.com/asmjit/asmjit/blob/master/test/asmjit_tes...

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

#38

Weird that this is about building a C compiler[0] in OCaml . I expected the implementation language to also be C both for consistency but also because i'm willing to bet that there are more people who can read C than OCaml. [0] actually from the readme in the github repo[1] it seems to be a C subset, not all of C [1] https://github.com/nlsandler/nqcc2

The bonus of writing a C compiler in C is that you get to being able to experiment with self-compilation.

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

#39

Earlier quoted context omitted.

Parser-generators were always academic projects that had little relevance to making real-world programming languages -- where parsing is very easy to write, and necessarily benefits from doing it (ie., you can get better error handling/etc.). Today most languages are front-ends for LLVM IR, but LLVM is very slow and takes a long time to optimize. Many new languages target x86/arm directly with their own weakly optimi…

> Parser-generators were always academic projects Were they? GCC abandoned bison in favour of their own parser relatively recently.

"relatively recently" as in around 20 years ago, in GCC 3.x according to sources I found.

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

#40
post #18

Earlier quoted context omitted.

Parser-generators were always academic projects that had little relevance to making real-world programming languages -- where parsing is very easy to write, and necessarily benefits from doing it (ie., you can get better error handling/etc.). Today most languages are front-ends for LLVM IR, but LLVM is very slow and takes a long time to optimize. Many new languages target x86/arm directly with their own weakly optimi…

Could you give some specific examples of those new languages with their own backends for faster builds?

I was immediately thinking of Jai, Zig, .. but I've seen a few
Post reply on HN