3 ms·
> I write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome. It's also the most annoying if you're writin
by sph 1y ago
> I write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome.
It's also the most annoying if you're writing a new language. You want to iterate on its ideas, but can't do so until you have a parser done.
I've been designing a few language concepts over the past year, and it feels 80% of this time has been writing and debugging parsers; by the time I get to the meat of language design - the shape of its AST, the semantics of it - any small syntactic change means going back to update the lexer and parser stage. Doesn't help that I can't settle on a syntax.
BTW I first started with PEG, which are nice in theory, but I feel the separation of lexing and parsing stage to very helpful to reduce boilerplate (handling whitespace in PEG is obnoxious). Later, I hand-wrote my parsers (in C), but it's gotten so repetitive I've dedicated a weekend to just learning lex/yacc (actually flex/bison). Even if parsers are easy to write, it's good to have higher level tools to reduce the tedium of it.
- fjfaase 1y agoThere are other tools for prototyping the grammar for a new language. There are several online tools that can interactively parse input based on a given grammar. The one I developed is: https://fransfaase.github.io/MCH2022ParserWorkshop/IParseStudio.html https://fransfaase.github.io/MCH2022ParserWorkshop/IParseStu... The page does contain a sample grammar and sample input. For the grammar for the grammar see: https://fransfaase.github.io/MCH2022ParserWorkshop/index.html https://fransfaase.github.io/MCH2022ParserWorkshop/index.htm...
- thesz 1y ago> You want to iterate on its ideas, but can't do so until you have a parser done. Embed your language into host language. It is simple to do even in C++. Iterate on ideas till your heart content, then add syntax.
- gpderetta 1y agoAlternatively (or concurrently) use s-expressions and add syntactic sugar later.
- WalterBright 1y agoAnecdotally, lex and yacc gets your prototype up and running quickly, but the ongoing adjustments needed winds up with it behind. The time lost comes from: 1. having to learn lex and yacc 2. running into limitations with lex and yacc 3. having a foreign program (i.e. lex and yacc) integrated into your build process 4. requiring a particular version of lex and yacc that may be awkward on multiple platforms 5. optimizing the code. (Once you start doing that, you cannot use lex/yacc to generate a new version.) And so on. At one point, we decided to write a D program that built the D compiler, instead of using make. Well, that turned out to be a greater time sink than just using make.