Left Recursion in Parsing Expression Grammars

  • Sérgio Medeiros
  • Fabio Mascarenhas
  • Roberto Ierusalimschy
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7554)


Parsing Expression Grammars (PEGs) are a formalism that can describe all deterministic context-free languages through a set of rules that specify a top-down parser for some language. PEGs are easy to use, and there are efficient implementations of PEG libraries in several programming languages.

A frequently missed feature of PEGs is left recursion, which is commonly used in Context-Free Grammars (CFGs) to encode left-associative operations. We present a simple conservative extension to the semantics of PEGs that gives useful meaning to direct and indirect left-recursive rules, and show that our extensions make it easy to express left-recursive idioms from CFGs in PEGs, with similar results. We prove the conservativeness of these extensions, and also prove that they work with any left-recursive PEG.


parsing expression grammars parsing left recursion natural semantics packrat parsing 


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Cooney, D.: Problem with nullable left recursion and trailing rules in Packrat Parsers Can Support Left Recursion. PEG Mailing List (2009),
  2. 2.
    Ford, B.: Packrat parsing: Simple, powerful, lazy, linear time. In: Proceedings of the 7th ACM SIGPLAN International Conference on Functional Programming, ICFP 2002, pp. 36–47. ACM, New York (2002)CrossRefGoogle Scholar
  3. 3.
    Ford, B.: Parsing expression grammars: a recognition-based syntactic foundation. In: POPL 2004: Proceedings of the 31st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pp. 111–122. ACM, New York (2004)CrossRefGoogle Scholar
  4. 4.
    Frost, R.A., Hafiz, R., Callaghan, P.: Parser Combinators for Ambiguous Left-Recursive Grammars. In: Hudak, P., Warren, D.S. (eds.) PADL 2008. LNCS, vol. 4902, pp. 167–181. Springer, Heidelberg (2008), CrossRefGoogle Scholar
  5. 5.
    Gosling, J., Joy, B., Steele, G., Bracha, G.: The Java Language Specification. Addison-Wesley Professional (2005)Google Scholar
  6. 6.
    Grune, D., Jacobs, C.J.: Parsing Techniques – A Practical Guide. Ellis Horwood (1991)Google Scholar
  7. 7.
    Hanson, D.R.: Compact recursive-descent parsing of expressions. Software: Practice and Experience 15(12), 1205–1212 (1985), CrossRefGoogle Scholar
  8. 8.
    Hutton, G.: Higher-order Functions for Parsing. Journal of Functional Programming 2(3), 323–343 (1992)MathSciNetzbMATHCrossRefGoogle Scholar
  9. 9.
    Ierusalimschy, R.: A text pattern-matching tool based on Parsing Expression Grammars. Software - Practice and Experience 39(3), 221–258 (2009)CrossRefGoogle Scholar
  10. 10.
    Ierusalimschy, R., de Figueiredo, L.H., Celes, W.: Lua 5.1 Reference Manual. Lua.Org (2006)Google Scholar
  11. 11.
    Kahn, G.: Natural Semantics. In: Brandenburg, F.J., Wirsing, M., Vidal-Naquet, G. (eds.) STACS 1987. LNCS, vol. 247, pp. 22–39. Springer, Heidelberg (1987)CrossRefGoogle Scholar
  12. 12.
    Medeiros, S., Ierusalimschy, R.: A parsing machine for PEGs. In: DLS 2008: Dynamic Languages Symposium, pp. 1–12. ACM, New York (2008)CrossRefGoogle Scholar
  13. 13.
    Medeiros, S., Mascarenhas, F., Ierusalimschy, R.: From Regular Expressions to Parsing Expression Grammars. In: SBLP 2011: Brazilian Programming Languages Symposium (2011)Google Scholar
  14. 14.
    Mizushima, K., Maeda, A., Yamaguchi, Y.: Packrat parsers can handle practical grammars in mostly constant space. In: Proceedings of the 9th ACM SIGPLAN-SIGSOFT Workshop on Program Analysis for Software Tools and Engineering, PASTE 2010, pp. 29–36. ACM, New York (2010)CrossRefGoogle Scholar
  15. 15.
    Parr, T.: ANTLR’s left-recursion prototype. PEG mailing list (2011),
  16. 16.
    Parr, T., Fisher, K.: LL(*): the foundation of the ANTLR parser generator. In: Proceedings of the 32nd ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2011, pp. 425–436. ACM, New York (2011), Google Scholar
  17. 17.
    Redziejowski, R.R.: Parsing expression grammar as a primitive recursive-descent parser with backtracking. Fundamenta Informaticae 79(3-4), 513–524 (2008)MathSciNetGoogle Scholar
  18. 18.
    Redziejowski, R.R.: Some aspects of parsing expression grammar. Fundamenta Informaticae 85, 441–451 (2008)MathSciNetzbMATHGoogle Scholar
  19. 19.
    Ridge, T.: Simple, Functional, Sound and Complete Parsing for All Context-Free Grammars. In: Jouannaud, J.-P., Shao, Z. (eds.) CPP 2011. LNCS, vol. 7086, pp. 103–118. Springer, Heidelberg (2011), CrossRefGoogle Scholar
  20. 20.
    Tisher, G.: IronMeta parser generator (2012),
  21. 21.
    Tratt, L.: Direct left-recursive parsing expression grammars. Tech. Rep. EIS-10-01, School of Engineering and Information Sciences, Middlesex University (October 2010)Google Scholar
  22. 22.
    Warth, A.: OMeta squeak left recursion? OMeta Mailing List (June 2008),
  23. 23.
    Warth, A., Douglass, J., Millstein, T.: Packrat parsers can support left recursion. In: PEPM 2008: Proceedings of the 2008 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation, pp. 103–110. ACM, New York (2008)CrossRefGoogle Scholar
  24. 24.
    Warth, A., Piumarta, I.: OMeta: an object-oriented language for pattern matching. In: DLS 2007: Proceedings of the 2007 Symposium on Dynamic Languages, pp. 11–19. ACM, New York (2007)CrossRefGoogle Scholar
  25. 25.
    Winskel, G.: The Formal Semantics of Programming Languages: An Introduction. Foundations of Computing. MIT Press (1993)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2012

Authors and Affiliations

  • Sérgio Medeiros
    • 1
  • Fabio Mascarenhas
    • 2
  • Roberto Ierusalimschy
    • 3
  1. 1.Department of Computer ScienceUFSAracajuBrazil
  2. 2.Department of Computer ScienceUFRJRio de JaneiroBrazil
  3. 3.Department of Computer SciencePUC-RioRio de JaneiroBrazil

Personalised recommendations