3 ms·
Thank you very much for all that information - much appreciated! The Portable C Compiler was written using Yacc. [1] >I think if you're writing say your first
by delhanty 9y ago
Thank you very much for all that information - much appreciated!
The Portable C Compiler was written using Yacc. [1]
>I think if you're writing say your first compiler you should try to use lex and yacc.
I've actually started with something higher level still: BNFC [2], which has taken me quite a long way quite quickly. It generates lexer, parser, operator precedence and AST along the lines of the Appel books for a variety of backends: C, Java, OCAML, Haskell etc.
But now I'm running into bugs and limitations in BNFC. I may fix some of those if I can, but at some point I will probably dump BNFC and then hand-edit the generated Flex/Bison.
Maybe that's what people do eventually - start with a high-level tool and then move lower?
[1] https://en.wikipedia.org/wiki/Portable_C_Compiler https://en.wikipedia.org/wiki/Portable_C_Compiler
[2] https://github.com/BNFC/bnfc https://github.com/BNFC/bnfc
- chubot 9y agoOK BNFC is interesting! I think that kind of tool is the holy grail, but they all have limitations. You kind of have to design your language AROUND it, and C obviously doesn't fit in that category. For example, I mentioned that Awk was designed with yacc. It depends how ambitious you want to be. You can tweak C to be more easily parsed, but then you will parse fewer real programs. It's not surprising to me at all that BNFC will have problems with C. So I guess I would say you should use high level tools like BNFC / lex / yacc until you can't. FWIW here is another little problem you will run into with C: https://en.wikipedia.org/wiki/Dangling_else https://en.wikipedia.org/wiki/Dangling_else I guess the point is that parser generators and even grammars themselves are leaky abstractions! To effectively use parsing tools you have to be familiar with their internals and the underlying algorithm. If there are two levels of code generation like BNFC, then that makes this task harder! I think it makes sense to start high and go low. It also makes sense to start low-level and go high. Once you are sick of writing parsers by hand, you will understand how to write a better parser generator than the ones that currently exist :)