Translation. Simple but Powerful Pratt Parsing [jsr-0017]

tips: Click on the translation to expand and view the original text. 点击译文可展开查看原文。
Translated according to the version last updated on Apr 13, 2020.
根据2020年4月13日更新的版本翻译。

欢迎阅读我关于 Pratt 解析的文章——语法分析的 monad 教程。关于 Pratt 解析的文章数量如此之多,以至于还有一篇概览文章 :)
Welcome to my article about Pratt parsing — the monad tutorial of syntactic analysis. The number of Pratt parsing articles is so large that there exists a survey post :)

本文的目标:
The goals of this particular article are:

  • 提出一个问题:所谓的左递归问题被夸大了。
  • 抱怨 BNF(巴科斯-瑙尔范式)在表达中缀表达式方面的不足。
  • 提供对 Pratt 解析算法的描述和实现,坚持核心概念,没有引入类似 DSL 的抽象。
  • 希望让自己最后一次试图理解这个算法。我曾经实现过一个生产级的 Pratt 解析器,但我现在不再能立即理解那段代码了 :-)
Raising an issue that the so-called left-recursion problem is overstated.
Complaining about inadequacy of BNF for representing infix expressions.
Providing a description and implementation of Pratt parsing algorithm which sticks to the core and doesn’t introduce a DSL-y abstraction.
Understanding the algorithm myself for hopefully the last time. I’ve implemented a production-grade Pratt parser once, but I no longer immediately understand that code :-)

本文假设读者对解析技术有一定的了解,例如,不会在这里解释 上下文无关文法 ( context free grammar ) 是什么。
This post assumes a fair bit of familiarity with parsing techniques, and, for example, does not explain what a context free grammar is.