News for hackers. Stuff that matters.
Re:Writing a C Compiler (2017) by dananjaya86 on Friday June 09, 2023 @07:32AM (#36256268)
Book version to be released in October '23 : https://nostarch.com/writing-c-compiler
Re:Writing a C Compiler (2017) by RcouF1uZ4gsC on Friday June 09, 2023 @08:42AM (#36257008)
Here is how to write a C compiler in Python that correctly compile the vast majority of C programs per the ISO C standard:

    print(“You have some form of undefined behavior, which means printing this is a valid response per the C standard”)
Re:Writing a C Compiler (2017) by tialaramex on Friday June 09, 2023 @10:06AM (#36257996)
Undefined Behaviour has to actually happen, and so that means at runtime†, and thus what you wrote is not a valid C compiler.

For C++ IFNDR ("Ill-formed, No diagnostic required") the situation is trickier because the affected programs (some unknowable but likely large proportion of all purported C++ code) are not well formed C++, the standard offers no hint as to what happens or why, since it constrains only the behaviour of a C++ compiler for well formed C++ programs.

† It's possible the C lexer claims to have some "Undefined Behaviour" cases like the C++ lexer, hence P2621 "UB? In my lexer?" which is a reference to a 2005 meme because C++ standards committee members are down with the kids, but that's clearly a standards text bug if so because it makes no sense to have UB in the lexer, these should just be ill-formed programs, you get a compiler error.

Re:Writing a C Compiler (2017) by e19293001 on Friday June 09, 2023 @09:05AM (#36257246)
I owe my entire career to this remarkable individual who, despite never having met or being affiliated with, has profoundly influenced me through his insightful books. His vast knowledge and expertise have been instrumental in teaching me numerous technical concepts and skills throughout his publications.

https://web.archive.org/web/20220519044634/http://cs.newpalt...

Assembly Language and Computer Architecture Using C++ and Java , Course Technology, 2004

This book has been an invaluable resource in enhancing my understanding of various technical aspects. It has provided me with in-depth insights into the inner workings of a CPU, enabling me to grasp the intricate mechanisms behind its operation. Additionally, the book has equipped me with the knowledge and skills necessary to write an assembler based on a given instruction set.

Moreover, I have delved into the intricacies of assembly language, thanks to the comprehensive explanations and examples provided in the book especially the exercises. It has allowed me to truly comprehend the nuances of this low-level programming language and its interactions with hardware.

Furthermore, the book has shed light on the fascinating process of how compilers generate assembly code, particularly in the context of object-oriented programming languages. By exploring this topic, I have gained a deeper understanding of the intricate steps involved in transforming high-level code into assembly instructions, thereby bridging the gap between software development and hardware execution.

I also acquired another remarkable book authored by the same author:

"Compiler Construction Using Java, JavaCC, and Yacc," published by IEEE/Wiley in 2012. This exceptional book has served as an invaluable guide in my journey of creating compilers, encompassing both theoretical foundations and practical implementation.

One of the most remarkable aspects of this book is its comprehensive coverage of parsing techniques. It equipped me with the knowledge and skills to effectively parse regular expressions, enabling me to implement powerful features akin to those found in the widely used tool, grep. This aspect of the book has been particularly enlightening, and I consider it a significant contribution for anyone seeking to delve into the realm of compilers.

Overall, I am deeply indebted to this book, and I wholeheartedly recommend it to anyone eager to explore the fascinating world of compiler construction. It has truly bridged the gap between theory and practical implementation, providing a solid foundation and equipping aspiring compiler developers with the essential tools and techniques required to embark on this captivating journey.

I had some comments before regarding this author and his books about compilers and computer architecture all over HN as well.

Re:Writing a C Compiler (2017) by bigdict on Friday June 09, 2023 @09:21AM (#36257445)
getting college essay vibes from this comment
Re:Writing a C Compiler (2017) by belter on Friday June 09, 2023 @09:32AM (#36257568)
And ChatGPT vibes...
Re:Writing a C Compiler (2017) by fuzztester on Friday June 09, 2023 @09:56AM (#36257870)
Nice comment, GPT user.

Now, GPT:

Replace all occurrences of the substring "me" with "you" in the above comment text.

Re:Writing a C Compiler (2017) by eesmith on Friday June 09, 2023 @10:41AM (#36258539)
Huh. I don't get those vibes.

Further investigation doesn't support your claim. The citations check out, including publication year and publishers.

And the author has indeed praised the book many times before (https://news.ycombinator.com/item?id=31843833, https://news.ycombinator.com/item?id=31843833, https://news.ycombinator.com/item?id=31311613, https://news.ycombinator.com/item?id=28481028, https://news.ycombinator.com/item?id=23386732, https://news.ycombinator.com/item?id=22305353, and https://news.ycombinator.com/item?id=23386732, https://news.ycombinator.com/item?id=21988211, https://news.ycombinator.com/item?id=21513056, https://news.ycombinator.com/item?id=18996703, and https://news.ycombinator.com/item?id=10184364 ) with the last comment from 2015.

Eg, compare "I am deeply indebted to this book" with "I'm very debted to this man. I enjoyed a lot reading his books and made me who I am today." at https://news.ycombinator.com/item?id=28481028 from Sept 10, 2021.

Or compare "I owe my entire career to this remarkable individual who, despite never having met or being affiliated with," with "I'm not affiliated with the author though. This book helped a lot in my career as a hardware and firmware engineer." at https://news.ycombinator.com/item?id=23386732 from June 2, 2020.

Re:Writing a C Compiler (2017) by Jtsummers on Friday June 09, 2023 @12:32PM (#36260317)
Maybe not generated, but still a bizarre opening paragraph in context:

> I owe my entire career to this remarkable individual who, despite never having met or being affiliated with, has profoundly influenced me through his insightful books. His vast knowledge and expertise have been instrumental in teaching me numerous technical concepts and skills throughout his publications.

The individual they're referring to with "this remarkable individual" is not Nora Sandler, the author of the submitted post, but Anthony J. Dos Reis who they repeatedly reference by allusion but never name. A confusing way to write.

Re:Writing a C Compiler (2017) by pharrington on Friday June 09, 2023 @01:50PM (#36261389)
For what it's worth, ZeroGPT thinks the comment's a 25%/75% human/AI mix.
Re:Writing a C Compiler (2017) by eesmith on Friday June 09, 2023 @02:11PM (#36261740)
For what it's worth, ZeroGPT thinks the first paragraph of your comment at https://news.ycombinator.com/item?id=35559453 was "Most Likely GPT generated" (25% written by a human, 100% generated by an AI/GPT).

The entire comment was 75% human, 46% AI/GPT.

I picked that comment because it had the longest text.

Re:Writing a C Compiler (2017) by pharrington on Friday June 09, 2023 @03:02PM (#36262490)
Yeah that's fair.

Edit: lmao playing around a bit, simply changing "it is" to "its" (no apostrophe) in the first sentence, and editing the second sentence to read "the problem's that people have to be" makes ZeroGPT no longer think my post was AI generated at all.

Re:Writing a C Compiler (2017) by fuzztester on Friday June 09, 2023 @03:54PM (#36263281)
Your long, detailed, somewhat scholarly, well researched comment, leads us to think (after consulting several prestigious, highly intelligent, real and artificial professors), that you maybe a suitable candidate for the first PhD program at the new international Global PHD Trainers Institute (iGPT Institute). We will shortly be sending you the long, formal and stilted application form, to which you must reply in the same way, but better, as the first test.

All the best.

Digitally signed, Your soon-to-be GPT overlords.

Re:Writing a C Compiler (2017) by jazzyjackson on Friday June 09, 2023 @12:43PM (#36260459)
Rude accusation. GPT talks like the average internet commenter, it shouldn't be surprising to find a genuine comment written in a voice similar to GPT.
Re:Writing a C Compiler (2017) by fuzztester on Friday June 09, 2023 @03:39PM (#36263044)
Okay, so now life is imitating art? Never knew ;-)

No, you are being rude. To the average internet commenter. Or maybe to GPT. ;-)

By bringing down either one of them to the level of the other.

Unless you meant "average" in the same sense as this short tale:

A statistician had his head in a fridge and his feet in an oven, and when asked how he felt, he said, "on the average, I feel quite comfortable".

Re:Writing a C Compiler (2017) by fuzztester on Friday June 09, 2023 @03:57PM (#36263321)
GPT comments are genuine too. Don't hurt our soon-to-be-developed feelings! Sniff ...
Re:Writing a C Compiler (2017) by bigdict on Friday June 09, 2023 @09:22AM (#36257456)
Has anyone worked through this? Is it a good (soon to be) book?
Re:Writing a C Compiler (2017) by userbinator on Friday June 09, 2023 @09:33AM (#36257587)
IMHO writing a compiler for a high-level language, in an even higher level language, somehow feels a bit "anachronistic" (for lack of better word).
Re:Writing a C Compiler (2017) by retrac on Friday June 09, 2023 @09:35AM (#36257603)
Most of the current major C implementations are written in C++.
Re:Writing a C Compiler (2017) by WalterBright on Friday June 09, 2023 @10:57AM (#36258786)
ImportC is written in D.
Re:Writing a C Compiler (2017) by userbinator on Friday June 09, 2023 @03:34PM (#36262965)
That's unfortunate.
Re:Writing a C Compiler (2017) by avgcorrection on Friday June 09, 2023 @09:43AM (#36257703)
For what reason?
Re:Writing a C Compiler (2017) by userbinator on Friday June 09, 2023 @03:33PM (#36262952)
It's backwards. Writing a C compiler in C or Asm makes sense, a Python compiler in C also does, but a C compiler in Python is an odd inversion of abstraction.
Re:Writing a C Compiler (2017) by Jtsummers on Friday June 09, 2023 @03:46PM (#36263161)
Why? The objective is to translate code in one language (C) to another (machine code or assembly or perhaps an intermediate representation). Why does it make sense to use C for that task and not Python or some other language? It's not like C provides facilities that specifically enable compiler writing or text parsing for itself that other languages are lacking.
Re:Writing a C Compiler (2017) by avgcorrection on Friday June 09, 2023 @04:19PM (#36263606)
I guess this harkens back to the days when you had to write a compiler in a low-level language because that’s all that the platform that you are targeting supports. Then it sounds weird to talk about writing a compiler in a high-level language in order to target a low-level one, because surely these high-level languages are more platform-dependent than the blessed (guaranteed on the platform) low-level one.

But these days we can access dozens of languages on many platforms. And we can use high-level languages that are good for writing compilers—languages with good string types and algebraic data types—instead of being limited to awfully imperative/procedural ones.

In other words: your perspective sounds way more anachronistic.

Re:Writing a C Compiler (2017) by golergka on Friday June 09, 2023 @09:59AM (#36257906)
Weren't a lot of functional languages, like ML and it's descendants, created specifically to write parsers and compilers?
Re:Writing a C Compiler (2017) by retrac on Friday June 09, 2023 @10:32AM (#36258388)
I'm not sure about specifically. A joyful synergy, maybe. The fact that compilers are, in essence, pure functions, maps from input programs to output programs, was recognized quite early on. I can't put my finger on the origin of the idea, but it may have been understood by Turing himself. Well-understood in the Lisp world by the late 1960s, before ML came along. Parsers and compiler data structures, translate extremely well into recursive functions, operating on algebraic data types. These are applicable to many other problems, but compilers have always had centre place in the functional showroom. Sometimes I do wonder if it's just navel-gazing, though -- look, functional languages are useful for implementing functional languages!
Re:Writing a C Compiler (2017) by JonChesterfield on Friday June 09, 2023 @10:11AM (#36258069)
I've seen it claimed that ML was originally written, in lisp, in order to have a better language to write compilers in.
Re:Writing a C Compiler (2017) by lispm on Friday June 09, 2023 @01:06PM (#36260806)
ML was written as the language used by the theorem prover LCF. It was written in Lisp.
Re:Writing a C Compiler (2017) by JonChesterfield on Friday June 09, 2023 @08:51PM (#36266400)
I didn't know that link, searches seem to confirm. Thank you.

The LCF style provers rely on the host language type system. A theorem instance can only be created by the trusted kernel. Lisp doesn't obviously lend itself to that - you could represent the proof, but I'm not clear how you'd prevent forging one.

Re:Writing a C Compiler (2017) by peterfirefly on Friday June 09, 2023 @11:15AM (#36259087)
No. ML was the meta language for a theorem prover (LCF).

https://en.wikipedia.org/wiki/Logic_for_Computable_Functions

Re:Writing a C Compiler (2017) by hcks on Friday June 09, 2023 @10:40AM (#36258530)
Yet another “compiling” course that puts all the emphasis on parsing.

Rule of thumb: parsing/lexing shouldn’t takes more than 10% of your compiler course.

Re:Writing a C Compiler (2017) by WalterBright on Friday June 09, 2023 @10:55AM (#36258759)
Parsing takes a weekend. The rest takes a year to get a rudimentary compiler working.
Re:Writing a C Compiler (2017) by tester756 on Friday June 09, 2023 @11:21AM (#36259177)
parsing is cool
Re:Writing a C Compiler (2017) by delta_p_delta_x on Friday June 09, 2023 @11:53AM (#36259734)
I took a compilers course last year, based off UPenn's CIS341[1]. The lexing + parsing lectures were blitzed through, and we didn't even write the parser for the semester-long compiler assignment. The parsing section of the assignment was for us to simply assign shift-reduce precedences to operators and expressions, and OCaml's Menhir took care of everything.

I have written a recursive-descent parser, and it boils down to essentially turning the context-free grammar into a bunch of mutually-recursive functions, which is frankly rather tedious work.

I am much more interested in how a modern compiler is able to optimise a call to `std::accumulate` into a fast, tight, and vectorised loop in assembly.

[1]: https://www.seas.upenn.edu/~cis3410/current/

Re:Writing a C Compiler (2017) by hota_mazi on Friday June 09, 2023 @11:54AM (#36259740)
I disagree.

As opposed to most compiler articles, this one actually covers code generation for every section of its chapters, which is really great.

I also like that every chapter focuses on a specific feature and describes how to implement it end to end: lexical/syntactic parsing, AST, and x86_64 generation.

Great series!

Re:Writing a C Compiler (2017) by marcosdumay on Friday June 09, 2023 @12:22PM (#36260171)
On the other hand, parsing text could easily be a very valuable course on its own. You just have to not keep it restricted to programing languages, and include the knowledge created on this century.
Re:Writing a C Compiler (2017) by vector_spaces on Friday June 09, 2023 @12:26PM (#36260214)
This attitude bugs me a lot. It seems really common, especially in more recent texts about language design and implementation, that parsing is heavily de-emphasized to the point where practically nobody talks about it. See Essentials of Programming Languages by Friedman & Wand, the relevant sections in SICP, Programming Languages: Application & Interpretation (which goes so far as to call it a distraction).

I get that parsing is more of an implementation detail and doesn't really belong to the space-brained realm of language design per se, but it's a bit annoying that most texts refuse to give any space to the topic, and rely on your language being S-expression based or assume you're going to use a parser generator. Like, in the real world, even if one will never actually implement a fully-fledged programming language, you're still probably going to have to parse things sometimes. I would love a book that goes into detail about different parsing techniques and considers best practices and patterns and tradeoffs/design considerations -- would pay good money for that

It reminds me somewhat of the situation in analysis, where there are lots of theorems that aren't written down anywhere because literally every book states them as "easy" exercises. Maybe I'm looking in the wrong places, but I can't find much in the way of concrete guidance on implementing parsers. I'm aware of the beautiful series on parsing theory by Aho & Ullman ("The Theory of Parsing, Translation, and Compiling"), but those are more focused on theory rather than implementation

Re:Writing a C Compiler (2017) by marssaxman on Friday June 09, 2023 @01:50PM (#36261399)
> Like, in the real world, even if one will never actually implement a fully-fledged programming language, you're still probably going to have to parse things sometimes.

That is definitely true, but in practice there isn't much to say about it, because sophisticated parsers turn out not to be particularly important; it works out better overall to design simple grammars, and then the parsing is easy.

- If you're a beginner, you'll write a recursive descent parser, because that's the simplest technique, and it lets you focus on your project instead of a new, unfamiliar tool.

- If you're writing a domain-specific language, or a config format, or something of that nature, you'll use whichever parser generator integrates most conveniently into your workflow, and you'll design your grammar around whatever its manual tells you to do.

- If you're writing a full-scale language compiler, you'll go back to recursive descent, because that offers the easiest way to recover from errors and report informative messages. Maybe you'll throw in precedence-climbing for operators.

> I would love a book that goes into detail about different parsing techniques and considers best practices and patterns and tradeoffs/design considerations -- would pay good money for that

I would also read such a book, but it would be more of a book about parser generators than a book about parsers.

Re:Writing a C Compiler (2017) by cdcarter on Friday June 09, 2023 @02:04PM (#36261619)
On the other hand, historically (and as the parent you're replying to points out), many compiler texts have spent a MAJORITY of their time on parsing, and rush through the actual interesting parts of compilation.

> I would love a book that goes into detail about different parsing techniques and considers best practices and patterns and tradeoffs/design considerations -- would pay good money for that

Terrence Parr's "Language Implementation Patterns" spends quite a bit of time on parsing, and parse tree->ast conversyions.

Re:Writing a C Compiler (2017) by vector_spaces on Friday June 09, 2023 @02:53PM (#36262348)
Thanks for pointing that one out -- I had written that one off before as an ANTLR book but looks like it covers more material than I gave it credit for
Re:Writing a C Compiler (2017) by munificent on Friday June 09, 2023 @12:48PM (#36260537)
Almost all real-world projects that are language-like or compiler-like will need a parser. A much smaller fraction of them will need register allocation, instruction selection, optimization, code generation, etc.

For every big, deep, native code compiler, there are a hundred template languages, config files, report generators, etc. all of which are real programs providing real value for actual people.

Emphasizing parsing provides the most value for the greatest number of people. The folks that do end up needing more back end depth will still have the resources available to learn it.

Re:Writing a C Compiler (2017) by throwaway17_17 on Friday June 09, 2023 @03:04PM (#36262505)
Do you have a ‘best of list’ for the resources when interested in back-end topics.
Re:Writing a C Compiler (2017) by munificent on Friday June 09, 2023 @06:52PM (#36265392)
I wouldn't consider myself any kind of authority on "best of", but I like the Dragon Book, and Engineering a Compiler. I've heard good things about Appel's Modern Compiler Design.
Re:Writing a C Compiler (2017) by patrec on Friday June 09, 2023 @07:25PM (#36265711)
Contrarian take: lots of people doing parsing, has, on the whole, highly negative value, and template languages and config files are a prime example of this.

Everybody and their dog thinks it necessary to inflict some new sub-par language on us when in about 99.9% of cases they should just either have stuck to s-expressions or some suitable subset of a popular programming or existing config language with a relatively sane syntax (blaze/bazel did that right, cmake did that very very wrong).

When was the last time you looked at some config file and thought, wow I'm so glad they didn't use toml or python or whatever, but instead made up some completely new syntax nothing in the world apart from this tool itself can parse and that I can't programaticaly manipulate?

When was the last time you thought, wow I am so glad that someone invented a new templating language that creates some new injection vulnerabilities, because no one apart from the lisp people ever seem to have worked out that if you want to interpolate into something tree shaped, you should have a tree-based interpolation syntax? Because although sexps and quasiquote solve this very nicely and concisely everyone else still seems to love string-bashing plus some ad-hoc "escaping" system for this. And one reason for this is of course precisely the enormous abundance of idiotic config languages that can't be easily manipulated as anything than opaque strings.

[Edit: if you do create a new config file language, pretty please provide some means to directly query and losslessly manipulate it; for the lossless part you will either need to have first class comments unambiguously attached to a particular syntactic construct and agreed upon deterministic formatting or IDE-style complexity, the first one is probably a better idea]

Re:Writing a C Compiler (2017) by wasimanitoba on Friday June 09, 2023 @01:35PM (#36261172)
anything better you'd recommend?