A pure embedding of attribute grammars

Anthony M. Sloane, Lennart C.L. Kats, Eelco Visser
2013 Science of Computer Programming  
Attribute grammars are a powerful specification paradigm for many language processing tasks, particularly semantic analysis of programming languages. Recent attribute grammar systems use dynamic scheduling algorithms to evaluate attributes on demand. In this paper, we show how to remove the need for a generator, by embedding a dynamic approach in a modern, object-oriented and functional programming language. The result is a small, lightweight attribute grammar library that is part of our larger
more » ... Kiama language processing library. Kiama's attribute grammar library supports a range of advanced features including cached, uncached, higher order, parameterised and circular attributes. Forwarding is available to modularise higher order attributes and decorators abstract away from the details of attribute value propagation. Kiama also implements new techniques for dynamic extension and variation of attribute equations. We use the Scala programming language because of its support for domain-specific notations and emphasis on scalability. Unlike generators with specialised notation, Kiama attribute grammars use standard Scala notations such as pattern-matching functions for equations, traits and mixins for composition and implicit parameters for forwarding. A benchmarking exercise shows that our approach is practical for realistic language processing. A.M. Sloane et al. / Science of Computer Programming ( ) -has the advantage that attributes are evaluated at most once, since attributes that are never needed will never be evaluated. However, keeping track of which attributes have been evaluated adds runtime overhead. In applications such as integrated development environments, the tradeoff is particularly worthwhile, since not all attributes are needed at all times. Nevertheless, these recent systems are based on generators that add to the learning curve and complicate development processes. We show in this paper how to integrate a dynamically scheduled attribute grammar approach as a library into an existing modern programming language. We use a pure embedding where the syntax, concepts, expressiveness and libraries of the host language are used directly [9, 10] . The high-level declarative nature of the attribute grammar formalism is retained and augmented with the flexibility and familiarity of the host language, both for specification and for implementation of the formalism itself. This work is part of the Kiama project [11] which is investigating pure embedding of language processing formalisms into the Scala programming language [12] . Kiama can be downloaded from http://kiama.googlecode.com, along with documentation and the full code for the examples presented in this paper. Kiama is not restricted to attribute grammars, but its other capabilities, such as strategy-based term rewriting, are beyond the scope of this paper. Scala is an interesting and powerful host language for the Kiama project due to its inclusion of both object-oriented programming and functional programming features, emphasis on scalability and interoperability with the Java virtual machine. Many domain-specific languages have been implemented as Scala libraries, including various testing frameworks (notably ScalaTest [13] and Specs [14]) and ScalaQL [15] for interfacing to databases. Kiama particularly takes advantage of Scala's support for domain-specific languages via features such as higher order functions, general operator notations, flexible syntax [16] and implicit definitions of parameters [12]. Even though Kiama is a library, its attribution features have the same general power as generator-based systems such as JastAdd [5] . Abstract syntax trees are defined by standard Scala classes with only minimal augmentation of the class definitions required to prepare them for attribution. Attribute equations are written as pattern matching functions of abstract tree nodes. As well as providing basic synthesised and inherited attributes, Kiama currently supports various forms of higher order attribute [17, 18] , parameterised attributes [19] , attribute forwarding between tree nodes [20], circular attributes that are evaluated to a fixed point [21] , and abstractions for attribute value propagation patterns [22] . Language extension and modification are achieved using Scala's scalability constructs such as implicit parameters, traits and mixins [12] . Also, in contrast to previous systems, attribute definitions can be adapted at run-time to implement dynamic language variations. Overall, the performance of Kiama attribute evaluators is in the same ballpark as the dynamically scheduled evaluators produced by JastAdd when we compare performance on a limited case study. The paper is structured as follows. First, in Section 2 we present Kiama's basic attribute grammar facilities, their implementation as functions with access to generic structural properties of the tree that is being attributed, and attribute value propagation using decorators. In Section 3, we consider the use of higher order attributes to implement cross-tree references and transformations, encountering tree splicing, parameterised attributes, and forwarding between trees along the way. Section 4 shows how circular attributes can be used to specify computations that execute to a fixed point and Section 5 discusses how Scala's component facilities enable attribute definitions to be modularised. Section 6 compares the performance of Kiama with JastAdd on an attribute grammar that is typical of those used to specify compilers. The paper concludes with a discussion of our approach in the context of other attribute grammar systems in Section 7 and concluding remarks in Section 8. This paper is an extended and revised version of one that appeared in the proceedings of the Ninth Workshop on Language Descriptions, Tools and Applications (LDTA). Apart from extending the description in all sections, we have added new material describing additions to the library since the LDTA paper: attribute propagation patterns, transformation, tree splicing and forwarding. A.M. Sloane et al. / Science of Computer Programming ( ) -A.M. Sloane et al. / Science of Computer Programming ( ) -
doi:10.1016/j.scico.2011.11.005 fatcat:tb6gt3vrsjholkepte3l2vzlya