News for hackers. Stuff that matters.
Re:Just Write the Parser by chrisseaton on Thursday October 22, 2020 @06:47PM (#24864097)
> The result is often that one of your users will discover the ambiguity instead, and then it may be too late.

But there are no ambiguities in recursive descent! They won't discover them because... they don't exist! It's literally impossible.

> As an example too well-known to actually occur, suppose you parse...

Great example - and the good thing about recursive descent means it's impossible to write this ambiguously - you must prefer one or the other.

> It’s not ambiguous

Correct. So why are you telling me the problems of ambiguity?

> but your users won’t be happy when they find out the unambiguous rule

Why won't they be happy? You can tell them exactly how their code is going to be parsed. I thought ambiguities was bad but now you're complaining about ambiguity as well?!

Re:Just Write the Parser by wrs on Friday October 23, 2020 @01:10AM (#24866207)
We seem to be talking past each other. :) Of course there are no ambiguities in any parser (unless it returns multiple parses, which would hardly be practical). But that’s almost always because the designer had to resolve some ambiguities present in the grammar.

The question is, did the designer resolve them deliberately or accidentally? And were they resolved in an ergonomic way?

The grammar fragment “expr :: IF expr THEN expr ELSE expr” is ambiguous. If I write a statement like the example, I expect (from 60 years of language tradition and simple ergonomics) a language author to choose to resolve that ambiguity by associating an ELSE with the nearest IF. Telling your puzzled users “it’s not ambiguous, it always goes with the farthest IF!” isn’t going to make them happier. It also won’t work to say “well, + has higher precedence than /, but that’s OK, it always does that”. Those are just bugs in the language design caused by a bad resolution of ambiguity.

If you use a tool to flag the ambiguities in your grammar, then you can be sure all the resolutions in your parser are deliberate and not accidental.

Re:Just Write the Parser by chrisseaton on Friday October 23, 2020 @05:41AM (#24867598)
> But that’s almost always because the designer had to resolve some ambiguities present in the grammar.

Not if they started with a grammar that didn't have any ambiguity in the first place.

I think you're coming from the angle that you always start with a CFG, resolve ambiguity, then write a parser.

Imagine that I never write a CFG for my language. No CFG exists! Instead - I start by writing a PEG, and then I write a recursive descent parser from that. At no point in this process have I had to resolve ambiguity. I didn't start with an ambiguous CFG and then write a PEG from it. I started with a PEG. It's never been ambiguous, and never will be ambiguous. There's no ambiguity.

> The grammar fragment “expr :: IF expr THEN expr ELSE expr” is ambiguous.

Right.... but I wouldn't write that because I'm not starting with a CFG I'm starting with a PEG.

> Telling your puzzled users “it’s not ambiguous, it always goes with the farthest IF!” isn’t going to make them happier.

I can't understand this - if I give them a simple well-defined rule that tells them what the code means they'll be happy. What do you think they'd want instead? No rule? A badly defined rule?