News for hackers. Stuff that matters.
Re:Pratt Parsers: Expression Parsing Made Easy (2011) by o11c on Saturday January 20, 2024 @12:05PM (#39069731)
These are certainly useful, but there's the major caveat that the "easy" stops when you have anything but the simplest precedence rules. For example, if unary `NOT` has a weaker precedence than other unary operators, or if `a < b < c` is to be treated differently than `(a < b) < c`, or if you want to forbid bitwise operators mixing with relationals to avoid confusion from fact that they have a different precedence in C (for historical reasons) than in many other languages.

Note also that Bison with precedence tags is more efficient than writing out the same grammar without precedence. And `bison --xml` lets you run the (trivial) machine yourself so you can do it in any language.

Re:Pratt Parsers: Expression Parsing Made Easy (2011) by ReleaseCandidat on Saturday January 20, 2024 @01:12PM (#39070484)
> anything but the simplest precedence rules. For example, if unary `NOT` has a weaker precedence than other unary operators

Unary operátors use precedence levels (binding powers) too, they are no different (regarding the existence of precedence levels) from infix functions. This is just a simplification of the precedence table in the linked article (which I haven't read).

> or if `a < b < c` is to be treated differently than `(a < b) < c`

If you want "<" to be right associative, that's exactly one of the things that's easy to do with a Pratt parser, change the binding power on one side for the "<".

> if you want to forbid bitwise operators mixing with relationals

I don't know how using a Pratt parser would interfere with throwing an error?

Re:Pratt Parsers: Expression Parsing Made Easy (2011) by orthoxerox on Saturday January 20, 2024 @03:39PM (#39072057)
> If you want "<" to be right associative, that's exactly one of the things that's easy to do with a Pratt parser, change the binding power on one side for the "<".

It's not about associativity at all. "a < b < c" is a ternary operator that compares three operands.

Re:Pratt Parsers: Expression Parsing Made Easy (2011) by ReleaseCandidat on Saturday January 20, 2024 @04:22PM (#39072484)
That's what I would think too (as it doesn't make much sense to compare a boolean to c), but what OP wrote doesn't make much sense else:

   or if `a < b < c` is to be treated differently than `(a < b) < c`
Re:Pratt Parsers: Expression Parsing Made Easy (2011) by int_19h on Saturday January 20, 2024 @06:23PM (#39073566)
OP is saying that you can write (a < b < c) in C and the likes, but it behaves as if ((a < b) < c), which is pretty much never what you want - hence why ternary and N-ary comparison operators like the Python one are desirable in the first place.
Re:Pratt Parsers: Expression Parsing Made Easy (2011) by vidarh on Sunday January 21, 2024 @06:40AM (#39077588)
Semantic actions plus a specific node for grouping (parentheses) makes handling your examples easy (e.g. for your a < b < c example, you just need to be able to distinguish ot from the grouped expression. You can do it by adding various flags to change the parser behavior too, but that certainly easily turns into a mess.