← Back to context

Comment by torginus

5 days ago

Imo BNF (or some other formal notation) is quite useful for defining your syntax, my biggest gripe with BNF in particular is the way it handles operator precedence (through nested recursive expressions), which can get messy quite fast.

Pratt parsers dont even use this recursion, they only have a concept of 'binding strength', which means in laymans terms that if I'm parsing the left side of say a '' expression, and I managed to parse something a binary subexpression, and the next token I'm looking at is another binary op, do I continue parsing that subexpression, which will be the RHS of the '' expression, or do I finish my original expression which will then be the LHS of the new one?

It represents this through the concept of stickiness, with onesimple rule - the subexpression always sticks to the operator that's more sticky.

This is both quite easy to imagine, and easy to encode, as stickiness is just a number.

I think a simpler most straightforward notation that incorporates precedence would be better.

> I think a simpler most straightforward notation that incorporates precedence would be better.

For example YACC provides a way to specify how a shift/reduce conflict

> https://www.gnu.org/software/bison/manual/html_node/Shift_00...

and a reduce/reduce conflict

> https://www.gnu.org/software/bison/manual/html_node/Reduce_0...

should be resolved; see also

> https://www.gnu.org/software/bison/manual/html_node/Mysterio...

This is actually a way to specify operator precedence in a much simpler and more straightforward way than via nested recursive expressions.