Pratt parsing described as a black box on Hacker News
A Hacker News post argues that Pratt parsing is simple to use without understanding its internal implementation details.
An article titled 'Pratt parsing is a black box' was posted on Hacker News. The author claims that Pratt parsing fits into just 9 lines of code when programming in a high-level language such as Rhombus. The author states that one does not need to understand the implementation details of Pratt parsing to use it effectively. The author wrote the backbone of a heavily Pratt-based parsing machinery for a compiler without understanding the core algorithm snippet in detail.
Douglas Crockford stated at GOTO 2013 that Pratt parsing reduces a complicated task to one simple question: 'What do we expect to see to the left of a token?'. In Pratt parsing, a 'nud' is defined as a token that expects nothing to its left. A 'led' is defined as a token that expects something to its left. Traditional parsing techniques register semantic actions to production rules, whereas Pratt parsing registers them to terminals. Pratt parsing avoids left-recursion problems that occur in traditional techniques like LL(k). The Pratt parsing engine does not need to test alternatives or backtrack. Semantic actions in Pratt parsing run exactly once, allowing them to contain side-effects without rollback. Semantic actions in Pratt parsing always know they are the correct match and may look arbitrarily far ahead. Unfilled gaps in Pratt parsing tables become trivial extension points that allow implementing macros or languages that extend their own operators. Pratt parsing requires a separate lexer, unlike some modern engines that work in a scannerless manner. The author notes that Pratt parsing cannot parse statements and must be combined with other techniques for languages containing them. Pratt parsing is a form of recursive descent parsing that runs custom user code for semantic actions. Pratt parsing cannot be used for incremental parsing as done in GLR parsing. Only tokens with associated leds need a value in the binding power table according to the author's argument. Separators such as comma and unknown/error/EOF tokens should have a binding power of zero. The author recommends reading Andy Chu's blog post on Pratt parsing without prototypal inheritance.
Sources
Every sentence of this story was checked against facts quoted from these articles. How we make the news
Spotted an error? Tell us
