Live data from Hacker News

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

nostarch.com

141–150 of 159 posts

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

#141

Earlier quoted context omitted.

My experience (and I admit I may be too biased given years of prior C/C++ experience) is that Rust's syntax is a necessity, since no other mainstream languages besides C/C++ are as low-level as Rust. Most mainstream languages have a GC, and don't support distinguishing between values on the stack or references, don't need to deal with lifetimes or don't provide the safety you get with them, etc. I'm curious though, c…

How do you want to be taken seriously if you don't see the convolution of the syntax of c++ and rust? You are going against an absolute truth.

Oh, I absolutely see it.

My point was that it's necessary. How would you implement the same features Rust and C++ have, without garbage collection, but with simpler syntax?

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

#142

I learned how to write a compiler by studying BYTE magazine in the 70's which published the source to a complete Pascal compiler as an article! https://archive.org/details/byte-magazine-1978-09 (part 1) All 3 parts of Tiny Pascal: https://albillo.hpcalc.org/publications/Easter%20Egg%20-%20T...

The Byte magazine is incredible. First time reading it. The archive.org collection is a gold mine for learning. Thank you very much for posting it.

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

#143

Earlier quoted context omitted.

Thanks, can you please lemme know which part uses pattern matching? I'd assume mostly in the lexer, but the parser should just be something that consume the tokens and spit out AST. Unless of course it combines the two.

Presumably anything that walks the syntax tree.

Thanks. At first I thought it is something like regex, but then I found it's something in functional programming. I need to read a few chapters of the book before buying it because I'm not interested in learning FP at the moment.

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

#144
post #48

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. [0] https://github.com/rui314/chibicc

Thanks, I wish the companion book were ready!

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

#145
post #105

Earlier quoted context omitted.

I've created many programming languages. All the ones I finished and were useful did not have grammars that "I wrote".

do you mean https://github.com/mjburgess/Lyssa and https://github.com/mjburgess/Quazar ? it's true that i can't find a grammar in either of them ( https://github.com/mjburgess/Lyssa/blob/master/src/impl.py#L... seems more forthy than anything else) but (while they are very much the sort of things that i like, thank you for sharing) they also seem somewhat less like 'real-world programming languages' than things like…

Indeed, that's a defunct profile where everything should be private anyway. The reops there are 13/14 years old: these were experiments with using RPython to create languages, I'd guess when I was ~20. The point of those was to profile RPython. I have created real front-ends and compiler backends in C for non-trivial langugaes.

I will soon likely create a probabilistic programming language and compiler.

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

#146
post #107

Earlier quoted context omitted.

I've created many programming languages. All the ones I finished and were useful did not have grammars that "I wrote".

Are you saying your programming languages don’t have a defined grammar?

The parser defines the grammar. This is quite common in mainstream languages -- iirc, only after some years did python get a formal description of a grammar.

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

#147
I don't really need to know how to build a compiler, and I've got enough other "don't need but am doing out of curiosity" things going on that I don't need any more of those, but if it wasn't $70 I'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.

That last book was Allen Holub's "Compiler Design in C", which is from 1990. Here's how the blurb on the back describes it:

> Allen I. Holub'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.

> With this method in mind, the book features three major aspects:

> (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 "type" 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).

It's out of print, but the author has made a searchable PDF available on his website [1]. I found it quite useful.

Holub seems to like the "learn by doing" approach. He's got another book, "Holub on Patterns" 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's Game of Life.

PS: Ooh. It occurred to me that No Starch Press books are often available on O'Reilly Learning. I checked and this one is there. So I guess it is going on my "don't need but am doing out of curiosity" pile after all.

[1] https://holub.com/compiler/

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

#148
post #136
post #131

Earlier quoted context omitted.

mine too! except, not so much of a closed-world system? a lot of what i like about emacs is that it has some of that same live-malleability that smalltalk and oberon have if you've tried godot i'm interested to hear what you think about it

Unfortunately, I have no idea what godot is. I would like to know more though if you're so inclined. (Surely not the game engine? That's the only thing my disambiguation machinery can come up with.)

yup, the game engine!

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

#149

Earlier quoted context omitted.

How do you want to be taken seriously if you don't see the convolution of the syntax of c++ and rust? You are going against an absolute truth.

Oh, I absolutely see it. My point was that it's necessary. How would you implement the same features Rust and C++ have, without garbage collection, but with simpler syntax?

So you are very careful not to depend on any of their toxic complex features. In the end, better not use them at all.

Even plain and simple C99 compilers can be reasonably written by a solo dev, so a motivated small team...

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

#150
post #111

Earlier quoted context omitted.

> it does have a debugger with breakpoints, which even supports time-travel debugging (except on windows obviously), but i've never used it. it even has first-party ide integration: https://ocaml.org/manual/5.2/debugger.html#s:inf-debugger 1. I am developing on windows so that's an issue for me and 2. I don't use emacs, I use VScode and I've not been able to get the experimental debugger working for the VScode plugin…

> John Carmack once pointed out that people who come from the game dev and Windows/PC world use debuggers while people from the linux and web dev world tend not to. It seems to be a matter of preference/taste, and I think FP programmers seem to have a distaste for debuggers and graphical debugging/development environments i think that's true! but i don't think it's purely a matter of preference; it's also a matter of…

If you're literally "from the Linux world", you're probably younger and less experienced, and likely from a hobby background rather than CS or engineering.

Developers in Unix shops before the Linux era used debuggers.

Obviously, the GNU project developed a debugger for an audience. GNU wouldn't be a complete replacement for proprietary systems like Unix with only compilers, but no debugger.

Post reply on HN