Interesting Bits of Postgres Grammar

59 sbdchd 5 6/23/2025, 3:09:21 PM steve.dignam.xyz ↗

Comments (5)

o11c · 40m ago
Postgres's identifier-quoting is almost what standard SQL requires, except that it folds in the wrong direction (only relevant if you're introspecting or mixing quoted with unquoted identifiers).

Many (Most?) other SQL implementations violate the standard horribly.

PaulHoule · 44m ago
What I want is a PEG grammar generator that lets you set operator precedence with either numbers or partial orderings.
o11c · 16m ago
You really don't want PEG, even if you think you do. Maybe especially if you think you do. PEG gives up on both performance and correctness in case of ambiguity; LL and LR are the main families that can be trusted (though not LL(*) unless it's actually LL(1) after converting token trees). If you're just parsing expressions however, you don't need anything near that complicated though.

Operator-precedence parsers can handle partial orderings just fine if you think about it - just toposort them, then explicitly list the set of acceptable children on each side (which must be less than the current precedent, or possibly equal on the left side, or right if you can reassociate which requires remembering parens) rather than just subtracting 1 like most implementations do. In many cases it suffices to just specify a single number (plus a single bit to allow more of the same level) instead of a set, e.g. if you're just fixing the ambiguous level of the bitwise or comparison operators between languages.

Note also that Bison has an XML output mode which is really useful since LR machine runtimes are trivial to write yourself; the conversion from a grammar to tables is the hard part. Unfortunately there is no similar story for lexers.

lovich · 4h ago
Was not aware you can execute lambda calculus in sql. neat article
cryptonector · 35m ago
SQL is Turing complete.