hn.today

Precedence levels are dead; long live precedence sets

corsix.org4 points0 comments
Screenshot of Precedence levels are dead; long live precedence sets

The piece argues that operator precedence is better modeled as a set of allowed expression relationships than as a ranked list of precedence levels. A flowchart shows which kinds of expressions can contain which others, while also marking operators such as comparisons and modulo as non-chaining: their operands cannot use the same operator group. This captures relationships that a simple ordering cannot, including groups that are incomparable rather than strictly higher or lower. The author illustrates the idea with Zig’s expression syntax, then simplifies the chart by removing less familiar operators to make the structure easier to inspect.

To translate the chart into a parser, the author first shows the conventional approach: assign each expression category an integer precedence, from primary expressions down to the general expression entry point. That scheme fails to represent the chart’s branching structure. Instead, each category receives a bitmask encoding which categories are reachable from it; the masks form the chart’s reachability matrix. Parser operations then use bitwise techniques - such as isolating or clearing the lowest set bit and testing intersections - in place of arithmetic on precedence numbers. The proposal is not that levels are useless for ordinary, strictly ordered operators, but that sets can express richer grammar relationships and can be used to convert existing level-based parsers. The author hopes language designers adopt this model, following Zig’s example, rather than forcing every operator into a single hierarchy.

Read on corsix.org0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.