News for hackers. Stuff that matters.
Re:Operator precedence by textual substitution by choeger on Monday May 16, 2022 @03:38AM (#31394453)
It's only not hard if you have a parser that allows you to carry that kind of context. Admittedly, operator precedence is so important that most parser (generators) have some canonical way to handle it. But try to implement a language with user-defined binary operators and consider the mess at your hands.
Re:Operator precedence by textual substitution by kryptiskt on Monday May 16, 2022 @04:29AM (#31394679)
Pratt parsing (as described in https://matklad.github.io//2020/04/13/simple-but-powerful-pr...), handles operator precedence and different operator types (prefix, infix, postfix and mixfix) in a very neat manner, and it's easy enough to stuff user-defined operators in there.
Re:Operator precedence by textual substitution by w4rh4wk5 on Monday May 16, 2022 @03:51AM (#31394505)
What complexity?! The precedence is a simple integer value. Even when you include associativity, there are only two options: right-to-left, left-to-right.

Doesn't matter whether that information is language defined or user defined.

For instance in Haskell, that's all the information you provide. You'd use either `infixl` or `infixr` (depending on which associativity you want) followed by an integer defining the precedence for the given operator.

The parser just needs a table to look up the precedence (and associativity) for a given operator. But a parser already needs a bunch of different tables in order to work, so that's certainly not a problem.

Re:Operator precedence by textual substitution by choeger on Thursday May 19, 2022 @04:37AM (#31432154)
> The parser just needs a table to look up the precedence (and associativity) for a given operator. But a parser already needs a bunch of different tables in order to work, so that's certainly not a problem.

Think about how you'd implement an independent parser for such a language, say for the sake of pretty printing it to html. Module A uses a user-defined operator that has been defined in Module B. You effectively cannot parse A correctly before you parse and load B, but the fact that you need to parse and load B is "hidden" in some import declaration that in turn needs to be parsed first. That's what I call a mess.

Re:Operator precedence by textual substitution by ghusbands on Monday May 16, 2022 @10:09AM (#31397458)
What alternatives do you believe offer easier support for user-defined binary operators? Almost all parsing technologies are more rigid and more complicated that numerical precedence offers.
Re:Operator precedence by textual substitution by choeger on Thursday May 19, 2022 @04:38AM (#31432158)
I have no idea, really. If I would, I would try to submit a paper to POPL or so.