e01.org

packrat parser

The Packrat parser is a type of parser that shares similarities with the recursive descent parser in its construction. However, it differs because it takes parsing expression grammars (PEGs) as input rather than LL grammars. In 1970, Alexander Birman laid the groundwork for packrat parsing by introducing the "TMG recognition scheme" (TS), and "generalized TS" (gTS). TS was based upon Robert M. McClure's TMG compiler-compiler, and gTS was based upon Dewey Val Schorre's META compiler-compiler. Birman's work was later refined by Aho and Ullman; and renamed as Top-Down Parsing Language (TDPL), and Generalized TDPL (GTDPL), respectively. These algorithms were the first of their kind to employ deterministic top-down parsing with backtracking. Bryan Ford developed PEGs as an expansion of GTDPL and TS. Unlike CFGs, PEGs are unambiguous and can match well with machine-oriented languages. PEGs, similar to GTDPL and TS, can also express all LL(k) and LR(k). Ford also introduced Packrat as a...

The Packrat parser is a type of parser that shares similarities with the recursive descent parser in its construction. However, it differs because it takes parsing expression grammars (PEGs) as input rather than LL grammars. In 1970, Alexander Birman laid the groundwork for packrat parsing by introducing the "TMG recognition scheme" (TS), and "generalized TS" (gTS). TS was based upon Robert M. McClure's TMG compiler-compiler, and gTS was based upon Dewey Val Schorre's META compiler-compiler. Birman's work was later refined by Aho and Ullman; and renamed as Top-Down Parsing Language (TDPL), and Generalized TDPL (GTDPL), respectively. These algorithms were the first of their kind to employ deterministic top-down parsing with backtracking. Bryan Ford developed PEGs as an expansion of GTDPL and TS. Unlike CFGs, PEGs are unambiguous and can match well with machine-oriented languages. PEGs, similar to GTDPL and TS, can also express all LL(k) and LR(k). Ford also introduced Packrat as a parser that uses memoization techniques on top of a simple PEG parser. This was done because PEGs have an unlimited lookahead capability resulting in a parser with exponential time performance in the worst case. Packrat keeps track of the intermediate results for all mutually recursive parsing functions. Each parsing function is only called once at a specific input position. In some instances of packrat implementation, if there is insufficient memory, certain parsing functions may need to be called multiple times at the same input position, causing the parser to take longer than linear time. INPUT(n) -- return the character at position n RULE(R : Rule, P : Position ) entry = GET_MEMO(R,P) -- return the number of elements previously matched in rule R at position P if entry == nil then return EVAL(R, P); end return entry; EVAL(R : Rule, P : Position ) start = P; for choice in R.choices -- Return a list of choice acc=0; for symbol in choice then -- Return each element of a rule, terminal and nonterminal if symbol.is_terminal then if INPUT(start+acc) == symbol.terminal then acc = acc + 1; --Found correct terminal skip pass it else break; end else res = RULE(symbol.nonterminal , start+acc ); -- try to recognize a nonterminal in position start+acc SET_MEMO(symbol.nonterminal , start+acc, res ); -- we memoize also the failure with special value fail if res == fail then break; end acc = acc + res; end if symbol == choice.last -- check if we have matched the last symbol in a choice if so return return acc; end end return fail; --if no choice match return fail D(1) = 1; P(1) = 1; |- | | | No update because no nonterminal was fully recognized |- | | | No update because no terminal was recognized |- | | | Update: D(4) = 1; P(4) = 1; |- | | | Hit on P(4) Update M(4) = 1 as M was recognized |- | | | No update because no terminal was recognized |- | | | Update: D(6) = 1; P(6) = 1; |- | | | Hit on P(6) Update M(6) = 1 as M was recognized |- | | | Hit on M(6) Update A(4) = 3 as A was recognized Update P(3)=5 as P was recognized |- | | | No update because no terminal was recognized |- | | | Hit on P(3) Update M(1)=7 as M was recognized |- | | | No update because no terminal was recognized |- | | | Hit on M(1) Update A(1)=7 as A was recognized Update S(1)=7 as S was recognized |}

References

  1. ^ Maintained fork of PEG.js Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  2. ^ Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). "Packrat parsers can handle practical grammars in mostly constant space". Proceedings of the 9th ACM SIGPLAN-SIGSOFT workshop on Program analysis for software tools and engineering . ACM. pp. 29– 36. doi : 10.1145/1806672.1806679 . ISBN 978-1-4503-0082-7 . S2CID 14498865 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  3. ^ Aho, Alfred V.; Lam, Monica S.; Sethi, Ravi; Ullman, Jeffrey D., eds. (2007). Compilers: principles, techniques, & tools (2nd ed.). Boston Munich: Pearson Addison-Wesley. ISBN 978-0-321-48681-3 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  4. ^ Norvig, Peter (1991-03-01). "Techniques for automatic memoization with applications to context-free parsing" . Computational Linguistics . 17 (1): 91– 98. ISSN 0891-2017 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  5. ^ Dubroy, Patrick; Warth, Alessandro (2017-10-23). "Incremental packrat parsing" . Proceedings of the 10th ACM SIGPLAN International Conference on Software Language Engineering . SLE 2017. New York, NY, USA: Association for Computing Machinery. pp. 14– 25. doi : 10.1145/3136014.3136022 . ISBN 978-1-4503-5525-4 . S2CID 13047585 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  6. ^ a b Flodin, Daniel. "A Comparison Between Packrat Parsing and Conventional Shift-Reduce Parsing on Real-World Grammars and Inputs" (PDF) . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  7. ^ a b c d e Ford, Bryan (2004-01-01). "Parsing expression grammars" . Proceedings of the 31st ACM SIGPLAN-SIGACT symposium on Principles of programming languages . POPL '04. New York, NY, USA: Association for Computing Machinery. pp. 111– 122. doi : 10.1145/964001.964011 . ISBN 978-1-58113-729-3 . S2CID 7762102 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  8. ^ a b c d Warth, Alessandro; Douglass, James R.; Millstein, Todd (2008-01-07). "Packrat parsers can support left recursion" . Proceedings of the 2008 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation . PEPM '08. New York, NY, USA: Association for Computing Machinery. pp. 103– 110. doi : 10.1145/1328408.1328424 . ISBN 978-1-59593-977-7 . S2CID 2168153 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  9. ^ a b c Ford, Bryan (2006). "Packrat Parsing: Simple, Powerful, Lazy, Linear Time". arXiv : cs/0603077 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  10. ^ a b Science, International Journal of Scientific Research in; Ijsrset, Engineering and Technology. "A Survey of Packrat Parser" . A Survey of Packrat Parser . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
  11. ^ a b Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). "Packrat parsers can handle practical grammars in mostly constant space" . Proceedings of the 9th ACM SIGPLAN-SIGSOFT workshop on Program analysis for software tools and engineering . PASTE '10. New York, NY, USA: Association for Computing Machinery. pp. 29– 36. doi : 10.1145/1806672.1806679 . ISBN 978-1-4503-0082-7 . S2CID 14498865 . Wikipedia revision 1369059820, retrieved 2026-11-20 · 1795129200.0
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"packrat parser". 1786547412. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1369059820

JSON record · API collection · citation policy