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 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.…
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
- Maintained fork of PEG.js Wikipedia revision 1369059820, retrieved 2026-11-20 · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Maintained fork of PEG.js Wikipedia revision 1369059820, retrieved 2026-11-20." Maintained fork of PEG. 2026-11-19. https://en.wikipedia.org/w/index.php?oldid=1369059820. Accessed 24 November 2026.
Chicago "Maintained fork of PEG.js Wikipedia revision 1369059820, retrieved 2026-11-20." Maintained fork of PEG. 2026-11-19. https://en.wikipedia.org/w/index.php?oldid=1369059820.
APA Maintained fork of PEG. (2026). Maintained fork of PEG.js Wikipedia revision 1369059820, retrieved 2026-11-20. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1369059820
- Packrat parsers can handle practical grammars in mostly constant space · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Packrat parsers can handle practical grammars in mostly constant space." Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). 2026-11-19. https://dl.acm.org/doi/10.1145/1806672.1806679. Accessed 24 November 2026.
Chicago "Packrat parsers can handle practical grammars in mostly constant space." Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). 2026-11-19. https://dl.acm.org/doi/10.1145/1806672.1806679.
APA Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). (2026). Packrat parsers can handle practical grammars in mostly constant space. Retrieved November 24, 2026, from https://dl.acm.org/doi/10.1145/1806672.1806679
- 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 · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "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." Aho, Alfred V. 2026-11-19. https://en.wikipedia.org/wiki/ISBN_(identifier). Accessed 24 November 2026.
Chicago "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." Aho, Alfred V. 2026-11-19. https://en.wikipedia.org/wiki/ISBN_(identifier).
APA Aho, Alfred V. (2026). 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. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/ISBN_(identifier)
- Techniques for automatic memoization with applications to context-free parsing · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Techniques for automatic memoization with applications to context-free parsing." Norvig, Peter (1991-03-01). 2026-11-19. https://dl.acm.org/doi/abs/10.5555/971738.971743. Accessed 24 November 2026.
Chicago "Techniques for automatic memoization with applications to context-free parsing." Norvig, Peter (1991-03-01). 2026-11-19. https://dl.acm.org/doi/abs/10.5555/971738.971743.
APA Norvig, Peter (1991-03-01). (2026). Techniques for automatic memoization with applications to context-free parsing. Retrieved November 24, 2026, from https://dl.acm.org/doi/abs/10.5555/971738.971743
- Incremental packrat parsing · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Incremental packrat parsing." Dubroy, Patrick; Warth, Alessandro (2017-10-23). 2026-11-19. https://doi.org/10.1145/3136014.3136022. Accessed 24 November 2026.
Chicago "Incremental packrat parsing." Dubroy, Patrick; Warth, Alessandro (2017-10-23). 2026-11-19. https://doi.org/10.1145/3136014.3136022.
APA Dubroy, Patrick; Warth, Alessandro (2017-10-23). (2026). Incremental packrat parsing. Retrieved November 24, 2026, from https://doi.org/10.1145/3136014.3136022
- A Comparison Between Packrat Parsing and Conventional Shift-Reduce Parsing on Real-World Grammars and Inputs · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "A Comparison Between Packrat Parsing and Conventional Shift-Reduce Parsing on Real-World Grammars and Inputs." a b Flodin, Daniel. 2026-11-19. https://uu.diva-portal.org/smash/get/diva2:752340/FULLTEXT01.pdf. Accessed 24 November 2026.
Chicago "A Comparison Between Packrat Parsing and Conventional Shift-Reduce Parsing on Real-World Grammars and Inputs." a b Flodin, Daniel. 2026-11-19. https://uu.diva-portal.org/smash/get/diva2:752340/FULLTEXT01.pdf.
APA a b Flodin, Daniel. (2026). A Comparison Between Packrat Parsing and Conventional Shift-Reduce Parsing on Real-World Grammars and Inputs. Retrieved November 24, 2026, from https://uu.diva-portal.org/smash/get/diva2:752340/FULLTEXT01.pdf
- Parsing expression grammars · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Parsing expression grammars." a b c d e Ford, Bryan (2004-01-01). 2026-11-19. https://doi.org/10.1145/964001.964011. Accessed 24 November 2026.
Chicago "Parsing expression grammars." a b c d e Ford, Bryan (2004-01-01). 2026-11-19. https://doi.org/10.1145/964001.964011.
APA a b c d e Ford, Bryan (2004-01-01). (2026). Parsing expression grammars. Retrieved November 24, 2026, from https://doi.org/10.1145/964001.964011
- Packrat parsers can support left recursion · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Packrat parsers can support left recursion." a b c d Warth, Alessandro; Douglass, James R. 2026-11-19. https://doi.org/10.1145/1328408.1328424. Accessed 24 November 2026.
Chicago "Packrat parsers can support left recursion." a b c d Warth, Alessandro; Douglass, James R. 2026-11-19. https://doi.org/10.1145/1328408.1328424.
APA a b c d Warth, Alessandro; Douglass, James R. (2026). Packrat parsers can support left recursion. Retrieved November 24, 2026, from https://doi.org/10.1145/1328408.1328424
- Packrat Parsing: Simple, Powerful, Lazy, Linear Time · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Packrat Parsing: Simple, Powerful, Lazy, Linear Time." a b c Ford, Bryan (2006). 2026-11-19. https://en.wikipedia.org/wiki/ArXiv_(identifier). Accessed 24 November 2026.
Chicago "Packrat Parsing: Simple, Powerful, Lazy, Linear Time." a b c Ford, Bryan (2006). 2026-11-19. https://en.wikipedia.org/wiki/ArXiv_(identifier).
APA a b c Ford, Bryan (2006). (2026). Packrat Parsing: Simple, Powerful, Lazy, Linear Time. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/ArXiv_(identifier)
- A Survey of Packrat Parser · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "A Survey of Packrat Parser." a b Science, International Journal of Scientific Research in; Ijsrset, Engineering and Technology. 2026-11-19. https://www.academia.edu/17779983. Accessed 24 November 2026.
Chicago "A Survey of Packrat Parser." a b Science, International Journal of Scientific Research in; Ijsrset, Engineering and Technology. 2026-11-19. https://www.academia.edu/17779983.
APA a b Science, International Journal of Scientific Research in; Ijsrset, Engineering and Technology. (2026). A Survey of Packrat Parser. Retrieved November 24, 2026, from https://www.academia.edu/17779983
- Packrat parsers can handle practical grammars in mostly constant space · retrieved 2026-11-19 · rev 1369059820
Cite this reference
MLA "Packrat parsers can handle practical grammars in mostly constant space." a b Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). 2026-11-19. https://doi.org/10.1145/1806672.1806679. Accessed 24 November 2026.
Chicago "Packrat parsers can handle practical grammars in mostly constant space." a b Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). 2026-11-19. https://doi.org/10.1145/1806672.1806679.
APA a b Mizushima, Kota; Maeda, Atusi; Yamaguchi, Yoshinori (2010-05-06). (2026). Packrat parsers can handle practical grammars in mostly constant space. Retrieved November 24, 2026, from https://doi.org/10.1145/1806672.1806679
Cite this article
MLA
"packrat parser." encyclopegia.org, 2026, https://encyclopegia.org/wiki/packrat-parser. Accessed 24 November 2026.
Chicago
"packrat parser." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/packrat-parser.
APA
encyclopegia.org. (2026). packrat parser. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/packrat-parser
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON