Memoizing Purely Functional Top-Down Backtracking Language Processors

Abstract Language processors may be implemented directly as functions. In a programming language that supports higher-order functions, large processors can be built by combining smaller components using higher-order functions corresponding to alternation and sequencing in the BNF notation of the grammar of the language to be processed. If the higher-order functions are defined to implement a top-down backtracking parsing strategy, the processors are modular and, owing to the fact that they resemble BNF notation, are easy to understand and modify. A major disadvantage of this approach is that the resulting processors have exponential time and space complexity in the worst case owing to the reapplication of processors during backtracking. This paper shows how a technique called memoization can be used to improve the efficiency of such processors whilst preserving their modularity. We show that memoized functional recognizers constructed for arbitrary non-left-recursive grammars have O(n3) complexity where n is the length of the input to be processed. The paper also shows how the initial processors could have been memoized using a monadic approach, and discusses the advantages of reengineering the definitions in this way.

[1]  Peter G. Harrison,et al.  Functional Programming , 1988 .

[2]  Jay Earley,et al.  An efficient context-free parsing algorithm , 1970, Commun. ACM.

[3]  Peter Norvig,et al.  Techniques for Automatic Memoization with Applications to Context-Free Parsing , 1991, CL.

[4]  Hans Leiß On Kilbury's modification of Earley's algorithm , 1990, TOPL.

[5]  DONALD MICHIE,et al.  “Memo” Functions and Machine Learning , 1968, Nature.

[6]  Philip Wadler,et al.  Monads for functional programming , 1995, NATO ASI PDC.

[7]  Rene Leermakers,et al.  The Functional Treatment of Parsing , 1993 .

[8]  Eugenio Moggi,et al.  Computational lambda-calculus and monads , 1989, [1989] Proceedings. Fourth Annual Symposium on Logic in Computer Science.

[9]  Simon L. Peyton Jones,et al.  Report on the programming language Haskell: a non-strict, purely functional language version 1.2 , 1992, SIGP.

[10]  A. Augusteijn,et al.  Functional programming, program transformations and compiler construction , 1993 .

[11]  Philip Wadler,et al.  Packrat parsing:: simple, powerful, lazy, linear time, functional pearl , 2002, ICFP '02.

[12]  Philip Wadler,et al.  Comprehending monads , 1990, Mathematical Structures in Computer Science.

[13]  John Hughes,et al.  Lazy Memo-functions , 1985, FPCA.

[14]  Richard A. Frost,et al.  Constructing Programs as Executable Attribute Grammars , 1992, Comput. J..

[15]  Jon Fairbairn Making form follow function: An exercise in functional programming style , 1987, Softw. Pract. Exp..

[16]  William H. Burge,et al.  Recursive Programming Techniques , 1975 .

[17]  Graham Hutton,et al.  Higher-order functions for parsing , 1992, Journal of Functional Programming.

[18]  Jay Earley,et al.  An efficient context-free parsing algorithm , 1970, Commun. ACM.