[HN Gopher] Regular, Recursive, Restricted
___________________________________________________________________
Regular, Recursive, Restricted
Author : todsacerdoti
Score : 15 points
Date : 2024-06-04 12:03 UTC (1 days ago)
(HTM) web link (matklad.github.io)
(TXT) w3m dump (matklad.github.io)
| thefaux wrote:
| This is sensible, but I don't fully understand why there is such
| pressure to support inherently ambiguous infix operations. I get
| that 1 + 2 * 3 might be marginally nicer to type out but 1 + (2 *
| 3) is completely unambiguous no matter what language I'm using.
| This, for me, improves readability, especially because I don't
| have to guess what the intention was if I'm ever confused by the
| result. Also, it's one thing in a math or science paper where the
| reader can correct the result if there is an ambiguity, but if a
| compiler handles ambiguity wrong, that can throw the entire
| program off quite significantly.
|
| The ratio of time spent on precedence parsing in compilers to
| utility feels very low to me. That being said, I do appreciate
| the satisfaction of being able to implement it declaratively as
| the author does here.
| armchairhacker wrote:
| IMO the "not equals" is easy to misinterpret as _permitting_ the
| tree.
|
| I think something like this would clearer: Expr
| = 'number' | '(' Expr ')' | Expr
| '+' Expr (bind-order: 2, assoc: left) | Expr '-' Expr
| (bind-order: 2, assoc: left) | Expr '*' Expr (bind-
| order: 1, assoc: left) | Expr '/' Expr (bind-order: 1,
| assoc: left)
|
| or if you want relative precedences: Expr =
| 'number' | '(' Expr ')' | Expr '+' Expr
| (add, assoc: left) | Expr '-' Expr (sub, assoc: left)
| | Expr '*' Expr (mul, assoc: left) | Expr '/' Expr
| (div, assoc: left) bind-order: mul
| equals div add equals sub mul before add
|
| or if you want to completely separate conflict resolution (and
| shorter bind-order syntax, and raise a syntax error if certain
| operators are mixed...this one I like the most):
| Expr = 'number' | '(' Expr ')' |
| Expr '+' Expr (add) | Expr '-' Expr (sub) |
| Expr '*' Expr (mul) | Expr '/' Expr (div) |
| Expr '&&' Expr (land) | Expr '||' Expr (lor)
| | Expr '==' Expr (eq) | Expr '!=' Expr (ne) |
| Expr '.' Expr (haskellDot) | Expr '..=' Expr (range)
| assoc: left: add, sub, mul, div, land, lor, eq, ne
| right: haskellDot forbid: range bind-
| order: mul | div add | sub range
| eq | ne land | lor haskellDot
| forbid: eq | ne land | lor
| practal wrote:
| This approach works very well, as far as I can tell so far:
|
| https://practal.com/press/aflap/1/ (mainly starting at "Syntactic
| Categories")
|
| I got it right only after finishing the draft of the article, so
| you need to make it somehow until "Post Scriptum" for the right
| method.
___________________________________________________________________
(page generated 2024-06-05 23:01 UTC)