Skip to main content
Log in

FliPpr: A System for Deriving Parsers from Pretty-Printers

  • Research Paper
  • Published:
New Generation Computing Aims and scope Submit manuscript

Abstract

When implementing a programming language, we often write a parser and a pretty-printer. However, manually writing both programs is not only tedious but also error-prone; it may happen that a pretty-printed result is not correctly parsed. In this paper, we propose FliPpr, which is a program transformation system that uses program inversion to produce a CFG parser from a pretty-printer. This novel approach has the advantages of fine-grained control over pretty-printing, and easy reuse of existing efficient pretty-printer and parser implementations.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6
Fig. 7

Similar content being viewed by others

Notes

  1. The Syn system [7] is capable of handling non-contextual layouts, which can be seen as a limited form of prettiness.

  2. In this paper, we write “space” for the space character and write “whitespace” for the space character and the new line character. Other kinds of spaces such as horizontal tabs are not discussed as they do not yield new insight.

  3. Available at https://bitbucket.org/kztk/flippr/.

  4. To also make strings such as " 1-1" parsable, we can add a declaration \(f~x = \textit{nil} \mathbin {{\texttt {<>}}}\textit{ppr}~x \mathbin {{\texttt {<>}}}\textit{nil}\). However, this addition does not give any new insight, and is omitted for simplicity.

  5. https://www.gnu.org/software/bison/.

  6. https://www.haskell.org/happy/.

  7. As a slight difference, the original paper [35] uses transformations on parse trees (or more precisely, derivation trees of productions), instead of semantic actions.

  8. Because of the linearity, Wadler’s deforestation [51] and (positive) supercompilation [47] coincide for the surface language FliPpr. In addition, notice that we assume non-deterministic pattern-matching and thus need not convey negative information in the deforestation process. Thus, generalized partial computation (GPC) [18, 19] also coincide with the above two for FliPpr [46].

  9. https://hackage.haskell.org/package/wl-pprint.

  10. Strictly speaking, program inversion and inverse computation are different notions; the former is a transformation to yield an inverse program while the latter is an interpretation method to run a program in an inverse way. The difference between the two become rather unimportant for grammar-based inversion that produces grammars with additional transformations, as whether we transform (as parser generators) or interpret (as the most of parser combinators) the grammars is left for the later process.

References

  1. Abramov, S.M., Glück, R., Klimov, Y.A.: An universal resolving algorithm for inverse computation of lazy languages. In: Virbitskaite, I., Voronkov, A., (eds.) Ershov Memorial Conference, Lecture Notes in Computer Science, vol. 4378, pp. 27–40. Springer (2006)

  2. Adams, M.D.: Principled parsing for indentation-sensitive languages: revisiting landin’s offside rule. In: Giacobazzi, R., Cousot, R., (eds.) The 40th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’13, Rome, January 23–25, pp. 511–522. ACM (2013)

  3. Aho, A.V., Ullman, J.D.: The theory of parsing, translation, and compiling. Prentice-Hall Inc, Upper Saddle River (1972)

    Google Scholar 

  4. Antoy, S., Echahed, R., Hanus, M.: A needed narrowing strategy. J. ACM 47(4), 776–822 (2000)

    Article  MathSciNet  MATH  Google Scholar 

  5. Baker, B.S.: Composition of top-down and bottom-up tree transductions. Inf. Control 41(2), 186–213 (1979)

    Article  MathSciNet  MATH  Google Scholar 

  6. Bernardy, J.: A pretty but not greedy printer (functional pearl). PACMPL 1(ICFP), 6:1–6:21 (2017)

  7. Boulton, R.J.: Syn: a single language for specifiying abstract syntax tress, lexical analysis, parsing and pretty-printing. Technical Report UCAM-CL-TR-390, University of Cambridge Computer Laboratory (1996)

  8. Brabrand, C., Møller, A., Schwartzbach, M.I.: Dual syntax for XML languages. Inf. Syst. 33(4–5), 385–406 (2008)

    Article  MATH  Google Scholar 

  9. Comon, H., Dauchet, M., Gilleron, R., Löding, C., Jacquemard, F., Lugiez, D., Tison, S., Tommasi, M.: Tree automata techniques and applications (2007). http://www.grappa.univ-lille3.fr/tata

  10. Danielsson, N.A.: Correct-by-construction pretty-printing. In: Weirich, S. (ed.) Workshop on Dependently-typed programming, DTP, pp. 1–12. ACM, New York (2013)

    Google Scholar 

  11. Duregård, J., Jansson, P.: Embedded parser generators. In: Proceedings of the 4th ACM symposium on Haskell. Haskell ’11, pp. 107–117. ACM, New York (2011)

  12. Duregård, J., Jansson, P., Wang, M.: Feat: functional enumeration of algebraic types. Haskell symposium. Haskell ’12, pp. 61–72. ACM, New York (2012)

    Chapter  Google Scholar 

  13. Engelfriet, J., Vogler, H.: Macro tree transducers. J. Comput. Syst. Sci. 31(1), 71–146 (1985)

    Article  MathSciNet  MATH  Google Scholar 

  14. Foster, J.N., Greenwald, M.B., Moore, J.T., Pierce, B.C., Schmitt, A.: Combinators for bidirectional tree transformations: a linguistic approach to the view-update problem. ACM Trans. Program. Lang. Syst. 29(3), 17 (2007)

    Article  MATH  Google Scholar 

  15. Foster, J.N., Pilkiewicz, A., Pierce, B.C.: Quotient lenses. In: Hook, J., Thiemann, P. (eds.) ICFP, pp. 383–396. ACM (2008)

  16. Frost, R.A., Hafiz, R., Callaghan, P.: Parser combinators for ambiguous left-recursive grammars. In: Hudak, P., Warren, D.S. (eds.) PADL, Lecture Notes in Computer Science, vol. 4902, pp. 167–181. Springer (2008)

  17. Fülöp, Z., Vogler, H.: Syntax-Directed Semantics: Formal Models Based on Tree Transducers, 1st edn. New York Inc., Secaucus, Springer-Verlag (1998)

    Book  MATH  Google Scholar 

  18. Futamura, Y., Konishi, Z., Glück, R.: Program transformation system based on generalized partial computation. New Gener. Comput. 20(1), 75–100 (2002)

    Article  MATH  Google Scholar 

  19. Futamura, Y., Nogi, K., Takano, A.: Essence of generalized partial computation. Theor. Comput. Sci. 90(1), 61–79 (1991)

    Article  MATH  Google Scholar 

  20. Glück, R., Kawabe, M.: Derivation of deterministic inverse programs based on LR parsing. In: Kameyama, Y., Stuckey, P.J. (eds.) FLOPS, Lecture Notes in Computer Science, vol. 2998, pp. 291–306. Springer (2004)

  21. Gomard, C.K., Jones, N.D.: A partial evaluator for the untyped lambda-calculus. J. Funct. Program. 1(1), 21–69 (1991)

    Article  MathSciNet  MATH  Google Scholar 

  22. Hanus, M.: Functional logic programming: From theory to curry. In: Voronkov, A., Weidenbach, C. (eds.) Programming Logics—Essays in Memory of Harald Ganzinger, Lecture Notes in Computer Science, vol. 7797, pp. 123–168. Springer (2013)

  23. Hu, Q., D’Antoni, L.: Automatic program inversion using symbolic transducers. In: Cohen, A., Vechev, M.T. (eds.) Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2017, Barcelona, June 18–23, pp. 376–389. ACM (2017)

  24. Hughes, J.: The design of a pretty-printing library. In: Jeuring, J., Meijer, E. (eds.) Advanced Functional Programming, Lecture Notes in Computer Science, vol. 925, pp. 53–96. Springer (1995)

  25. Jansson, P., Jeuring, J.: Polytypic data conversion programs. Sci. Comput. Program. 43(1), 35–75 (2002)

    Article  MathSciNet  MATH  Google Scholar 

  26. Jones, N.D., Gomard, C.K., Sestoft, P.: Partial evaluation and automatic program generation. Prentice Hall international series in computer science. Prentice Hall, Upper Saddle River (1993)

    MATH  Google Scholar 

  27. Jonsson, P.A., Nordlander, J.: Positive supercompilation for a higher order call-by-value language. In: Shao, Z., Pierce, B.C. (eds.) POPL, pp. 277–288. ACM (2009)

  28. Kiselyov, O., Peyton Jones, S.L., Sabry, A.: Lazy v. yield: Incremental, linear pretty-printing. In: Jhala, R., Igarashi, A. (eds.) Programming Languages and Systems—10th Asian Symposium, APLAS, Lecture Notes in Computer Science, vol. 7705, pp. 190–206. Springer (2012)

  29. Knuth, D.E.: Semantics of context-free languages. Mathematical Systems Theory 2(2), 127–145 (1968)

    Article  MathSciNet  MATH  Google Scholar 

  30. Ko, H., Zan, T., Hu, Z.: BiGUL: a formally verified core language for putback-based bidirectional programming. In: Erwig, M., Rompf, T. (eds.) Proceedings of the 2016 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM 2016, St. Petersburg, January 20–22, pp. 61–72. ACM (2016)

  31. Kobayashi, N., Tabuchi, N., Unno, H.: Higher-order multi-parameter tree transducers and recursion schemes for program verification. In: Hermenegildo, M.V., Palsberg, J. (eds.) POPL, pp. 495–508. ACM (2010)

  32. Kühnemann, A., Glück, R., Kakehi, K.: Relating accumulative and non-accumulative functional programs. In: Middeldorp, A. (ed.) RTA. Lecture Notes in Computer Science, vol. 2051, pp. 154–168. Springer (2001)

  33. Matsuda, K., Inaba, K., Nakano, K.: Polynomial-time inverse computation for accumulative functions with multiple data traversals. In: Kiselyov, O., Thompson, S. (eds.) PEPM, pp. 5–14. ACM (2012)

  34. Matsuda, K., Inaba, K., Nakano, K.: Polynomial-time inverse computation for accumulative functions with multiple data traversals. High. Order Symb. Comput. 25(1), 3–38 (2012)

    Article  MathSciNet  MATH  Google Scholar 

  35. Matsuda, K., Mu, S.C., Hu, Z., Takeichi, M.: A grammar-based approach to invertible programs. In: Gordon, A.D. (ed.) ESOP, Lecture Notes in Computer Science, vol. 6012, pp. 448–467. Springer (2010)

  36. Matsuda, K., Wang, M.: FliPpr: a prettier invertible printing system. In: Felleisen, M., Gardner, P. (eds.) ESOP, Lecture Notes in Computer Science, vol. 7792, pp. 101–120. Springer (2013)

  37. Matsuda, K., Wang, M.: HOBiT: Programming lenses without using lens combinators. In: Ahmed, A. (ed.) Programming Languages and Systems—27th European Symposium on Programming, ESOP 2018, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2018, Thessaloniki, April 14–20, Proceedings, Lecture Notes in Computer Science, vol. 10801, pp. 31–59. Springer (2018)

  38. McPeak, S., Necula, G.C.: Elkhound: a fast, practical GLR parser generator. In: Duesterwald, E. (ed.) Compiler Construction, 13th International Conference, CC 2004, Held as Part of the Joint European Conferences on Theory and Practice of Software, ETAPS 2004, Barcelona, March 29–April 2, Proceedings, Lecture Notes in Computer Science, vol. 2985, pp. 73–88. Springer (2004)

  39. Might, M., Darais, D., Spiewak, D.: Parsing with derivatives: a functional pearl. In: Chakravarty, M.M.T., Hu, Z., Danvy, O. (eds.) Proceeding of the 16th ACM SIGPLAN international conference on Functional Programming, ICFP 2011, Tokyo, September 19–21, pp. 189–195. ACM (2011)

  40. Pombrio, J., Krishnamurthi, S.: Resugaring: lifting evaluation sequences through syntactic sugar. In O’Boyle, M.F.P., Pingali, K. (eds.) ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI ’14, Edinburgh, June 09–11, pp. 361–371. ACM (2014)

  41. Rendel, T., Ostermann, K.: Invertible syntax descriptions: unifying parsing and pretty printing. In: Gibbons, J. (ed.) Haskell, pp. 1–12. ACM (2010)

  42. Sands, D.: Proving the correctness of recursion-based automatic program transformations. Theor. Comput. Sci. 167(1&2), 193–233 (1996)

    Article  MathSciNet  MATH  Google Scholar 

  43. Scott, E., Johnstone, A.: GLL parsing. Electron. Notes Theor. Comput. Sci. 253(7), 177–189 (2010)

    Article  Google Scholar 

  44. Seki, H., Matsumura, T., Fujii, M., Kasami, T.: On multiple context-free grammars. Theor. Comput. Sci. 88(2), 191–229 (1991)

    Article  MathSciNet  MATH  Google Scholar 

  45. Søndergaard, H., Sestoft, P.: Non-determinism in functional languages. Comput. J. 35(5), 514–523 (1992)

    Article  MathSciNet  Google Scholar 

  46. Sørensen, M.H., Glück, R., Jones, N.D.: Towards unifying partial evaluation, deforestation, supercompilation, and GPC. In: Sannella, D. (ed.) Programming Languages and Systems—ESOP’94, 5th European Symposium on Programming, Edinburgh, April 11–13, Proceedings, Lecture Notes in Computer Science, vol. 788, pp. 485–500. Springer (1994)

  47. Sørensen, M.H., Glück, R., Jones, N.D.: A positive supercompiler. J. Funct. Program. 6(6), 811–838 (1996)

    Article  MATH  Google Scholar 

  48. Swierstra, S.D., Chitil, O.: Linear, bounded, functional pretty-printing. J. Funct. Program. 19(1), 1–16 (2009)

    Article  MATH  Google Scholar 

  49. Tomita, M.: An efficient context-free parsing algorithm for natural languages. In: Joshi, A.K. (ed.) Proceedings of the 9th International Joint Conference on Artificial Intelligence, Los Angeles, August 1985, pp. 756–764. Morgan Kaufmann (1985)

  50. Vijay-Shanker, K., Weir, D.J., Joshi, A.K.: Characterizing structural descriptions produced by various grammatical formalisms. In: Sidner, C.L. (ed.) 25th Annual Meeting of the Association for Computational Linguistics, Stanford University, Stanford, July 6-9, pp. 104–111. ACL (1987)

  51. Wadler, P.: Deforestation: Transforming programs to eliminate trees. Theor. Comput. Sci. 73(2), 231–248 (1990)

    Article  MathSciNet  MATH  Google Scholar 

  52. Wadler, P.: A prettier printer. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming. Palgrave Macmillan (2003)

  53. Yellin, D.M.: Attribute Grammar Inversion and Source-to-source Translation, Lecture Notes in Computer Science, vol. 302. Springer (1988)

  54. Zaytsev, V., Bagge, A.H.: Parsing in a broad sense. In: Dingel, J., Schulte, W., Ramos, I., Abrahão, S., Insfrán, E. (eds.) Model-Driven Engineering Languages and Systems—17th International Conference, MODELS 2014, Valencia, September 28–October 3. Proceedings, Lecture Notes in Computer Science, vol. 8767, pp. 50–67. Springer (2014)

  55. Zhu, Z., Zhang, Y., Ko, H., Martins, P., Saraiva, J., Hu, Z.: Parsing and reflective printing, bidirectionally. In: van der Storm, T., Balland, E., Varró, D. (eds.) Proceedings of the 2016 ACM SIGPLAN International Conference on Software Language Engineering, Amsterdam, October 31–November 1, pp. 2–14. ACM (2016)

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Kazutaka Matsuda.

Additional information

This work was mainly done when the K. Matsuda was at University of Tokyo, and the M. Wang was at Chalmers University of Technology. We thank Nils Anders Danielsson for his critical yet constructive comments on an earlier version of this work, without which the surface language probably would not exist. We also thank Janis Voigtländer and Akimasa Morihata for their insightful comments on deforestation. This work was partially supported by JSPS KAKENHI Grant numbers 24700020, 15K15966, and 15H02681. Part of this research was done when the K. Matsuda was visiting Chalmers University of Technology supported by Study Program at the Overseas Universities by Graduate School of Information Science and Technology, the University of Tokyo.

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Matsuda, K., Wang, M. FliPpr: A System for Deriving Parsers from Pretty-Printers. New Gener. Comput. 36, 173–202 (2018). https://doi.org/10.1007/s00354-018-0033-7

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s00354-018-0033-7

Keywords

Navigation