C Compiler from Scratch
github.com
C Compiler from Scratch
1–10 of 68 posts
Re: C Compiler from Scratch
#2Re: C Compiler from Scratch
#3Re: C Compiler from Scratch
#4I'm curious why BNF was chosen vs EBNF. I'm new to parsers and grammar. Isn't EBNF easier/simpler to write complex rules?
Re: C Compiler from Scratch
#5Any ideas what subset of C this supports? Or is it fully C89/C99/C11 compliant?
Just from cruising the lesson titles and poking around a few of them, substantially less than full C89. I don't see floating point, bitfields, or function pointers anywhere. Apparently, variadic functions and short (!) aren't supported either. Most of the new things in C99 (save perhaps //-style comments, mixed declaration and code, and long long) are unlikely to be supported, let alone C11.
Judging from its reference to C-, it might also well not support the full C declarator madness.
Re: C Compiler from Scratch
#6This is fantastic work. I believe learning to write a compiler is similar to learning a functional programming language. It completely changes the way you approach problems. I'm curious why BNF was chosen vs EBNF. I'm new to parsers and grammar. Isn't EBNF easier/simpler to write complex rules?
I can't speak to the specific reasons the OP chose, but I can speak to generalities.
Generally speaking, every parser engine out there that supports EBNF supports different extensions of EBNF [+]. They aren't portable from one to another, and that can lock you in. So if you get frustrated with the engine at some point and want to ditch it, it becomes harder to.
BNF does make it harder to write complex rules, but if your goal is a self-hosted compiler, like this one appears to be, you might use BNF for the host compiler, and then choose something more expressive now you can use the language you've built.
There are tradeoffs in everything. BNF tends to be faster, and more portable with more documentation. EBNF handles more complex cases, but you might have to learn one specific tool rather than a standard you can use everywhere.
[+] There is an EBNF standard. However, parser/lexer engines still extend it in their own incompatible ways. ISO tried to standardise it, and just ended up adding to the chaos, and now even they don't recommend their own.
Re: C Compiler from Scratch
#7This is fantastic work. I believe learning to write a compiler is similar to learning a functional programming language. It completely changes the way you approach problems. I'm curious why BNF was chosen vs EBNF. I'm new to parsers and grammar. Isn't EBNF easier/simpler to write complex rules?
> I'm curious why BNF was chosen vs EBNF. I'm new to parsers and grammar. Isn't EBNF easier/simpler to write complex rules? I can't speak to the specific reasons the OP chose, but I can speak to generalities. Generally speaking, every parser engine out there that supports EBNF supports different extensions of EBNF [+]. They aren't portable from one to another, and that can lock you in. So if you get frustrated with t…
Direct link to the standard: https://standards.iso.org/ittf/PubliclyAvailableStandards/s0...
But of course, as shanka commented, it isn't a standard that is followed practically. This is just a demonstration of https://xkcd.com/927/ in real life.
Re: C Compiler from Scratch
#8[0]: https://lagunita.stanford.edu/courses/Engineering/Compilers/...
Re: C Compiler from Scratch
#9This is fantastic work. I believe learning to write a compiler is similar to learning a functional programming language. It completely changes the way you approach problems. I'm curious why BNF was chosen vs EBNF. I'm new to parsers and grammar. Isn't EBNF easier/simpler to write complex rules?
He wrote a BNF grammar, but his parser is closer to a handwritten parser for an EBNF grammar. Generally speaking, when people write recursive descent parsers by hand in a procedural style, they parse sequences directly rather than using a recursive descent following the grammar.