Hacker News new | ask | show | jobs
by yortus 1583 days ago
Why not just write it more simply as

    Str = "a" "aa"*
Most languages allow writing tortured and hard to understand logic where simpler alternatives can also be written. I'm not sure why that should make people consider not using them.
2 comments

Because that does not recognize the same set of strings. That matches aaaaa, but my example above does not. That is exactly the point I'm trying to make.
Well, if you wanted all odd-length strings, you'd write what GP wrote. If you wanted whatever it was your grammar accepted, you'd write what you wrote :)
> If you wanted whatever it was your grammar accepted

It is one-line, two-rule PEG grammar. What does it accept, and why? If we can't state this with confidence in such a tiny example, what makes us have any confidence in larger, more complex grammars? We should understand our tools. PEG pretends to be simple and understandable, but it's much less so than it might seem on the surface.

Simple rules can give surprising, complex behaviour.

That's a good point. Unfortunately, it's also true not only of PEGs and recursive-descent parsing, but also of essentially every parsing formalism out there. So it doesn't seem to disqualify PEGs in particular.

(It's also true of programming languages generally: For which inputs does Collatz halt, and why?)

I contest that. The parsers based on the formalism of CFG do not have that problem. The straightforward mental model of the user always matches their behaviour, no complexity, no surprises.
The parse tree from that would be different, though it would match the same strings.
Incorrect, it does not match the same strings.