News for hackers. Stuff that matters.
Re:Compiler Development: Rust or OCaml? by nu11ptr on Monday August 07, 2023 @03:38PM (#37039812)
I'm just saying a compiler is a program, and while the more important things are heavy algorithm related, supporting libraries like those for error handling, etc. all still matter and add up. No problem if you disagree - just my perspective. This isn't so much an objective thing as it is a personal opinion.
Re:Compiler Development: Rust or OCaml? by brmgb on Monday August 07, 2023 @05:13PM (#37041024)
Personally, it’s not so much that I disagree. It’s more that Ocaml has a top notch ecosystem for compiler writing. That’s probably by far its strongest point with library like Menhir having few equivalent in different ecosystem.

Not that there is anything wrong with Rust if you are ready to pay the price of having so much low level things to deal with.

Re:Compiler Development: Rust or OCaml? by hardwaregeek on Monday August 07, 2023 @05:24PM (#37041150)
Can you explain what Menhir does better than other parser frameworks? For what it's worth, I'm not a huge fan of parser frameworks. I tend to prefer hand written parsers, either via a combinator library or fully manually.

Rust has a pretty darn good ecosystem too btw. chumsky for parsing, rowan for syntax trees, salsa for incremental computation, miette for errors, inkwell/walrus for code generation.

Re:Compiler Development: Rust or OCaml? by brmgb on Monday August 07, 2023 @05:31PM (#37041242)
It can generate elegant and efficient parsers for LR(1) grammars.

> I tend to prefer hand written parsers, either via a combinator library or fully manually.

That’s common with people used to languages which provide poor parser generators.

Re:Compiler Development: Rust or OCaml? by hardwaregeek on Monday August 07, 2023 @05:40PM (#37041342)
What makes them more elegant than the average parser? How’s the error recovery? Can you parse into high fidelity syntax trees efficiently?

I don’t know of many production compilers that use parser generators

Re:Compiler Development: Rust or OCaml? by klodolph on Monday August 07, 2023 @08:26PM (#37042883)
You use the generated parser as a platform for experimentation.

If you know the language, and you have a bunch of users, and you are writing a parser for it, by all means, write a parser by hand and give it the best error recovery that you can muster. If you are developing a language and want to do a bunch of experiments, it pays dividends to use a parser generator. And then there is the whole space of DSLs and mini-languages you encounter, where beautiful error messages are a nice-to-have, but you would rather ship a generated parser and move on to more important work.

It’s easy to focus on compilers from the perspective of familiar languages like writing compilers for Rust or for OCaml, but you may end up writing a compiler that gets used by a much smaller number of people, for smaller tasks.

Re:Compiler Development: Rust or OCaml? by hardwaregeek on Monday August 07, 2023 @09:22PM (#37043267)
That's great and I agree for these use-cases a parser generator makes sense. But that doesn't answer my question about what makes these parsers particularly elegant. Nor does it seem to be a benefit specific to Mehnir, since any parser generator has the quick iteration speed.

I don't mean to blame you for that; you are answering a question about something that you did not say. But I find it frustrating that Mehnir seems to be cited as this fantastic cornerstone of the OCaml ecosystem when I haven't been presented with a good example of how it's better than any other parser generator. Not just my parent comment (who also decided to cast aspersions on my knowledge), but others in the OCaml community too.

Re:Compiler Development: Rust or OCaml? by klodolph on Tuesday August 08, 2023 @02:06AM (#37045098)
I didn’t intend to answer your question—your comment had multiple aspects to it and I am responding to one part of it, rather than every single part of it.

My main take on the “which language do I write my compiler in” conversation is that some of the various code transformation passes are just more convenient to write in a language which makes it easy to use both mutation and garbage collection, which puts OCaml above both Rust and Haskell. I think parsing is an easier problem to solve in the first place.

Re:Compiler Development: Rust or OCaml? by brmgb on Tuesday August 08, 2023 @03:22AM (#37045625)
> with a good example of how it's better than any other parser generator.

I already told you that it has full support for disambiguating LR(1) grammar and still generating a parser which is easy to read. How do you want me to paste a full parser in a HN comment?

Most generators only support LALR(1) grammar which is limiting and don’t deal with corner cases as gracefully.

I get that you are hell bent on wanting Rust to prevail here but Rust will always be a subpar experience for wiring anything which doesn’t strongly benefit for its low level primitive. Rust has annoying semantics and a convoluted syntax. I can bear with that when the performances are needed but writing a compiler in it is just unnecessary pain. It’s also one of the only thing for which I would actually use Ocaml.

Re:Compiler Development: Rust or OCaml? by hardwaregeek on Tuesday August 08, 2023 @10:48AM (#37049798)
Okay…so it accepts more grammars than other parser generators. That doesn’t seem massive if I’m being honest. If you had said mehnir works with your IDE (navigating C code inside bison drove me nuts), and had good support for error recovery and idk, gave you syntax highlighting for free, I’d agree. But a minor upgrade in grammars? Not exactly Christmas here.

I have a question for you then: why is it that so many projects that are not performance bound, that are not low level systems projects, why do they use Rust and not OCaml? OCaml had a 22 year head start after all.

Re:Compiler Development: Rust or OCaml? by moomin on Monday August 07, 2023 @06:49PM (#37042009)
I mean, it’s also common with people who value great feedback in their tools.
Re:Compiler Development: Rust or OCaml? by 59nadir on Tuesday August 08, 2023 @02:11AM (#37045136)
> That’s common with people used to languages which provide poor parser generators.

The vast majority of real languages have hand-rolled parsers and there is no real reason they shouldn't.

Re:Compiler Development: Rust or OCaml? by derriz on Tuesday August 08, 2023 @04:39AM (#37046146)
I don’t think so - I’ve written parsers using flex/lex yacc/bison, antlr, a bunch of functional combinator libraries and maybe others I’ve forgotten but now would never consider anything except hand-written recursive descent with an embedded Pratt parser for expressions and precedence.

Simple to write, debug, recover from errors, provide decent error messages, unit test, integrate into build systems, IDEs etc.

I also believe that nearly all the popular compilers these days do something similar - gcc was rewritten a few years ago in this fashion because of the technical benefits I’ve listed above.

Re:Compiler Development: Rust or OCaml? by foderking on Wednesday August 09, 2023 @07:10AM (#37060984)
>That’s common with people used to languages which provide poor parser generators.

lmao

Re:Compiler Development: Rust or OCaml? by arcticbull on Monday August 07, 2023 @06:42PM (#37041938)
Wow those library recommendations are fantastic, especially Chumsky. Thanks for sharing.
Re:Compiler Development: Rust or OCaml? by hardwaregeek on Monday August 07, 2023 @07:22PM (#37042285)
Yeah chumsky is fantastic, albeit with some rough compile errors and some unsafe. Still my favorite parser library by far and I’ve used a bunch by now.