Remix.run Logo
jasperry 4 days ago

> If I was routinely working in a language that had a well respected de facto standard parser generator and lexer, and regularly building parsers for little languages for my programs, it would probably be worth mastering these tools.

In OCaml, a language highly suited for developing languages in, that de facto standard is the Menhir LR parser generator. It's a modern Yacc with many convenient features, including combinator-like library functions. I honestly enjoy the work of mastering Menhir, poring over the manual, which is all one page: https://gallium.inria.fr/~fpottier/menhir/manual.html

debugnik 3 days ago | parent | next [-]

I gave up on Menhir after I understood how allocation-heavy it is during the hot path, at least in the incremental API which is needed for proper errors; and how much of a giant hack you need to force extra lookahead, which shouldn't be such a big deal for parser generators.

These days I just handroll recursive descent parsers with a mutable stream record, `raise_notrace` and maybe some combinators inspired by FParsec for choices, repetition and error messages. I know it's not as rigorous, but at least it's regular code without unexpected limitations.

jasperry 3 days ago | parent [-]

Could be, I'm not that far along yet. I've only just peeked into the incremental API. I'm still using the error token to try to improve my messages. It's just for syntax errors anyway, right?

fuzztester 4 days ago | parent | prev [-]

>In OCaml, a language highly suited for developing languages in,

What makes OCaml suited for that?

mjburgess 4 days ago | parent | next [-]

algebraic datatypes (tagged unions + pattern matching); compiled, garbage collected (you dont really need memory management for a compiler), statically typed with inference

hibikir 3 days ago | parent | next [-]

Yeah, the same reasons Scala has a built in parser combinator module in the standard library: Just easy to use with those features in the language

fuzztester 3 days ago | parent | prev [-]

thanks.

greggyb 4 days ago | parent | prev [-]

ML, the language heritage from which OCaml derives, was explicitly designed with interpreters and compilers in mind.