Re:Show HN: A work-in-progress C compiler from scratch
by fao_ on Tuesday August 03, 2021 @01:57PM
(#28052104)
Parsing it manually is actually the preferred choice for most compilers, both GCC and Clang do it manually, for example.
Re:Show HN: A work-in-progress C compiler from scratch
by SavantIdiot on Tuesday August 03, 2021 @02:19PM
(#28052326)
Really? That's probably why I failed! Ironically (given my post) I've never looked at Clang's parser. I have looked at GCC's and, well, sheepishly: I don't get it, it is a beast of a project and I wasn't able to crack the architecture. I should try again tho.
Re:Show HN: A work-in-progress C compiler from scratch
by rot13xor on Tuesday August 03, 2021 @04:56PM
(#28054081)
C is almost context-free with the exception of typedef. A custom parser helps with error diagnostics, e.g. missed semicolon or a "." swapped with "->".
https://eli.thegreenplace.net/2007/11/24/the-context-sensiti...
I don't mean this in a bad way, but that tokenizer looks a bit ... simple. ;-)
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/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't fit nicely into BNF and hadn't studied parsers enough to dig myself out of the hole.
Good luck doing it manually, that's gonna be tricky as heck.