• 0 Posts
  • 1 Comment
Joined 1 year ago
cake
Cake day: June 26th, 2023

help-circle
  • As someone who’s spent way too much time languishing over picking the perfect parsing technique for my own language, I’m actually gonna go against the norm and recommend figuring out the parser later. Instead you should start with building your language’s ASTs directly in memory and then from there, either build a backend for converting ASTs to LLVM IR, or what I’d actually do first is just start with an interpreter executing directly on the ASTs. This way you figure out the mechanics of your language, and then when they’re well established, you can worry about your syntax, and how to parse it into ASTs. There’s a lot you learn about your language by doing it this way that you don’t necessarily think about if you just start from the parser. It also let’s you see real progress/output sooner which I think is key for staying motivated on these kinds of projects.

    When it comes time to actually write the parser, I recommend either just hand crafting a parser directly, or using an existing parser generator tool like gnu bison, etc. I do not recommend trying to write your own parser generator (e.g. LR(k), LALR, LL(k), etc.) unless your language’s syntax is particularly simple. Speaking from experience, real languages have many common syntax features we take for granted that are hard to deal with in parser generators. In my case, I spent years bogged down exploring/implementing several state of the art parser algorithms (I’m a fan of generalized parsing, so Earley, GLR, SRNGLR, GLL, etc), and really only recently made any decent progress when I decided to trash them and just hand write a dumb recursive-descent-esque parser. Once things are working, it’s pretty easy to go back and swap out the parser if you want something more fancy.