Advertisement

Attribute grammars as a functional programming paradigm

  • Thomas Johnsson
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 274)

Abstract

The purpose of this paper is twofold. Firstly we show how attributes in an attribute grammar can be simply and efficiently evaluated using a lazy functional language. The class of attribute grammars we can deal with are the most general ones possible: attributes may depend on each other in an arbitrary way, as long as there are no truly circular data dependencies.

Secondly, we describe a methodology based on attribute grammars, where, in a fairly straightforward way, we can develop efficient functional programs where direct, conventional solutions yield less efficient programs. We review two examples from a paper by R. Bird (Using circular programs to eliminate multiple traversals of data, Acta Informatica, 21, 1984) where he transforms simple but inefficient multipass programs into more efficient single pass ones, but which on their own can be very hard to understand. We show how such efficient but tangled programs can have natural formulations as attribute grammars.

We also propose a language construct, called case rec (akin to the case expression in Standard ML and Lazy ML), that defines an attribute grammar over a data structure in the language. In effect, a case rec expression defines a recursion operator that can handle multiple values, both upwards-propagating and downwards-propagating ones.

Keywords

Parse Tree Functional Program Context Free Grammar Graph Reduction Attribute Evaluator 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

References

  1. [Aug84]
    L. Augustsson. A compiler for lazy Ml. In Proceedings of the 1984 ACM Symposium on Lisp and Functional Programming, pages 218–227, Austin, 1984.Google Scholar
  2. [AUS86]
    A. V. Aho, J. D. Ullman, and R. Sethi. Compilers: Principles, Techniques, Tools. Addison-Wesley Publishing Company, Reading, Mass., 1986.Google Scholar
  3. [BD77]
    R. M. Burstall and J. Darlington. A transformation system for developing recursive programs. Journal of the ACM, 24:44–67, 1977.Google Scholar
  4. [Bir84]
    R. S. Bird. Using circular programs to eliminate multiple traverals of data. Acta Informatica, 21:239–250, 1984.Google Scholar
  5. [BMS80]
    R. M. Burstall, D. B. McQueen, and D. T. Sannella. Hope: an experimental applicative language. In Proceedings of the 1980 ACM Symposium on Lisp and Functional Programming, pages 136–143, Stanford, CA, August 1980.Google Scholar
  6. [CM79]
    L. M. Chirica and D. F. Martin. An order-algebraic definition of knuthian semantics. Math. Systems Theory, 13:1–27, 1979.Google Scholar
  7. [DJL85]
    Pierre Deransart, Martin Jourdan, and Bernard Lorho. A Survey on Attribute Grammars, Part III: Classified Bibliography. Rapport de Recherche 417, Inria, Rocquencourt, France, June 1985.Google Scholar
  8. [DJL86a]
    Pierre Deransart, Martin Jourdan, and Bernard Lorho. A Survey on Attribute Grammars, Part II: Review of Existing Systems. Rapport de Recherche 510, Inria, Rocquencourt, France, March 1986.Google Scholar
  9. [DJL86b]
    Pierre Deransart, Martin Jourdan, and Bernard Lorho. A Survey on Attribute Grammars, Part I: Main Results on Attribute Grammars. Rapport de Recherche 485, Inria, Rocquencourt, France, January 1986.Google Scholar
  10. [GG84]
    H. Ganzinger and R. Giegerich. Attribute coupled grammars. In Proceedings of the SIGPLAN '84 Symposium on Compiler Construction, pages 157–170, Montreal, 1984.Google Scholar
  11. [GMW79]
    M. Gordon, R. Milner, and C. Wadsworth. Edinburgh Lcf. Volume 78 of Lecture Notes in Computer Science, Springer-Verlag, 1979.Google Scholar
  12. [Hug85]
    R. J. M. Hughes. Lazy memo-functions. In Proceedings 1985 Conference on Functional Programming Languages and Computer Architecture, Nancy, France, 1985.Google Scholar
  13. [Joh75]
    S. C. Johnson. Yacc—Yet Another Compiler Compiler. Technical Report 32, Bell labs, 1975. Also in Unix Programmer's Manual, Volume 2B.Google Scholar
  14. [Joh84]
    T. Johnsson. Efficient compilation of lazy evaluation. In Proceedings of the SIGPLAN '84 Symposium on Compiler Construction, pages 58–69, Montreal, 1984.Google Scholar
  15. [Joh85]
    T. Johnsson. Lambda lifting: transforming programs to recursive equations. In Proceedings 1985 Conference on Functional Programming Languages and Computer Architecture, Nancy, France, 1985.Google Scholar
  16. [JOR75]
    Mehdi Jazayeri, William F. Ogden, and William C. Rounds. The intrinsically exponential complexity of the circularity problem for attribute grammars. Communications of the ACM, 18:697–706, 1975.Google Scholar
  17. [Jou84]
    M. Jourdan. An optimal-time recursive evaluator for attribute grammars. In Proceedings of 6th Int. Symp. on Programming, LNCS 167, pages 167–178, Springer-Verlag, April 1984.Google Scholar
  18. [Kat84]
    Takuya Katayama. Translation of attribute grammars into procedures. ACM Trans. on Programming Languages and Systems, 6(3):345–369, July 1984.Google Scholar
  19. [KL81]
    R. M. Keller and G. Lindstrom. Applications of feedback in functional programming. Technical Report, University of Utah, Salt Lake City, April 1981.Google Scholar
  20. [Knu68]
    Donald E. Knuth. Semantics of context-free languages. Math. Systems Theory, 2:127–145, 1968.Google Scholar
  21. [May81]
    B. Mayoh. Attribute grammars and mathematical semantics. SIAM J. of Computing, 10(3):503–518, August 1981.Google Scholar
  22. [Mil84]
    R. Milner. Standard ML proposal. Polymorphism: The ML/LCF/Hope Newsletter, 1(3), January 1984.Google Scholar
  23. [Pau82]
    L. Paulson. A semantics-directed compiler generator. In Proc. 9th POPL, 1982.Google Scholar
  24. [Tur76]
    D. A. Turner. Sasl Language Manual. Technical report, University of St. Andrews, 1976.Google Scholar
  25. [Tur85]
    D. A. Turner. Miranda: A non-strict language with polymorphic types. In Proceedings 1985 Conference on Functional Programming Languages and Computer Architecture, pages 1–16, Nancy, France, 1985.Google Scholar
  26. [Udd]
    G. Uddeborg. A Functional Parser Generator. In preparation.Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 1987

Authors and Affiliations

  • Thomas Johnsson
    • 1
  1. 1.Department of Computer ScienceChalmers University of TechnologyGöteborgSweden

Personalised recommendations