Exception Handling for Error Reporting in Parsing Expression Grammars

  • André Murbach Maidl
  • Fabio Mascarenhas
  • Roberto Ierusalimschy
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8129)


Parsing Expression Grammars (PEGs) are a new formalism to describe a top-down parser of a language. However, error handling techniques that are often applied to top-down parsers are not directly applicable to PEGs. This problem is usually solved in PEGs using a heuristic that helps to simulate the error reporting technique from top-down parsers, but the error messages are generic. We propose the introduction of labeled failures to PEGs for error reporting, as labels help to produce more meaningful error messages. The labeled failures approach is close to that of generating and handling exceptions often used in programming languages, being useful to annotate and label grammar pieces that should not fail. Moreover, our approach is an extension to the PEGs formalism that is expressive enough to implement some previous work on parser combinators. Finally, labeled failures are also useful to compose grammars preserving the error messages of each separate grammar.


parsing error reporting parsing expression grammars packrat parsing parser combinators 


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Aho, A.V., Lam, M.S., Sethi, R., Ullman, J.D.: Compilers: Principles, Techniques, and Tools, 2nd edn. Addison-Wesley Longman Publishing Co., Inc., Boston (2006)Google Scholar
  2. 2.
    Ford, B.: Packrat parsing: a practical linear-time algorithm with backtracking. Master’s thesis, Massachusetts Institute of Technology (September 2002)Google Scholar
  3. 3.
    Ford, B.: Packrat Parsing: Simple, Powerful, Lazy, Linear Time. In: ICFP 2002: Proceedings of the 7th ACM SIGPLAN International Conference on Functional Programming, pp. 36–47. ACM, New York (2002)Google Scholar
  4. 4.
    Ford, B.: Parsing Expression Grammars: A Recognition-Based Syntactic Foundation. In: POPL 2004: Proceedings of the 31st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages, pp. 111–122. ACM, New York (2004)CrossRefGoogle Scholar
  5. 5.
    Grune, D., Jacobs, C.J.: Parsing Techniques: A Practical Guide, 2nd edn. Springer Publishing Company, Incorporated (2010)Google Scholar
  6. 6.
    Hutton, G.: Higher-Order Functions for Parsing. Journal of Functional Programming 2(3), 323–343 (1992)MathSciNetCrossRefzbMATHGoogle Scholar
  7. 7.
    Ierusalimschy, R.: LPeg - Parsing Expression Grammars for Lua (2008), (visited on March 2013)
  8. 8.
    Ierusalimschy, R.: A Text Pattern-Matching Tool based on Parsing Expression Grammars. Software - Practice & Experience 39(3), 221–258 (2009)CrossRefGoogle Scholar
  9. 9.
    Jeffery, C.L.: Generating LR Syntax Error Messages from Examples. ACM Transactions on Programming Languages and Systems 25(5), 631–640 (2003)CrossRefGoogle Scholar
  10. 10.
    Leijen, D., Meijer, E.: Parsec: Direct Style Monadic Parser Combinators For The Real World. Technical Report UU-CS-2001-35, Department of Computer Science, Utrecht University (2001)Google Scholar
  11. 11.
    Louden, K.C.: Compiler Construction: Principles and Practice. PWS Publishing Co., Boston (1997)Google Scholar
  12. 12.
    Medeiros, S., Mascarenhas, F., Ierusalimschy, R.: From Regular Expressions to Parsing Expression Grammars. In: Brazilian Symposium on Programming Languages (2011)Google Scholar
  13. 13.
    Medeiros, S., Mascarenhas, F., Ierusalimschy, R.: Left Recursion in Parsing Expression Grammars. In: de Carvalho Junior, F.H., Barbosa, L.S. (eds.) SBLP 2012. LNCS, vol. 7554, pp. 27–41. Springer, Heidelberg (2012)CrossRefGoogle Scholar
  14. 14.
    Mizushima, K., Maeda, A., Yamaguchi, Y.: Packrat Parsers Can Handle Practical Grammars in Mostly Constant Space. In: PASTE 2010: Proceedings of the 9th ACM SIGPLAN-SIGSOFT Workshop on Program Analysis for Software Tools and Engineering, pp. 29–36. ACM, New York (2010)Google Scholar
  15. 15.
    Partridge, A., Wright, D.: Predictive parser combinators need four values to report errors. Journal of Functional Programming 6(2), 355–364 (1996)CrossRefzbMATHGoogle Scholar
  16. 16.
    Piumarta, I.: Peg/leg — recursive-descent parser generators for C (2007), (visited on March 2013)
  17. 17.
    Röjemo, N.: Efficient Parsing Combinators. Technical report, Department of Computer Science, Chalmers University of Technology (1995)Google Scholar
  18. 18.
    Swierstra, S.D., Duponcheel, L.: Deterministic, Error-Correcting Combinator Parsers. In: Launchbury, J., Sheard, T., Meijer, E. (eds.) AFP 1996. LNCS, vol. 1129, pp. 184–207. Springer, Heidelberg (1996)CrossRefGoogle Scholar
  19. 19.
    Winskel, G.: The Formal Semantics of Programming Languages: An Introduction. MIT Press, Cambridge (1993)zbMATHGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2013

Authors and Affiliations

  • André Murbach Maidl
    • 1
  • Fabio Mascarenhas
    • 2
  • Roberto Ierusalimschy
    • 1
  1. 1.Department of Computer SciencePUC-RioRio de JaneiroBrazil
  2. 2.Department of Computer ScienceUFRJRio de JaneiroBrazil

Personalised recommendations