Digitized by the Internet Archive in 2013 http://archive.org/details/algebraicdefinit881chir r/inrn yj/a/t UIUCDCS-R-77-881 An Algebraic Definition of Knuthian Semantics by Laurian M. Chirica David F. Martin June 1977 5 UILU-ENG 77 1739 TteW*** tftW SEP 3 1977 9t ,t r hAna- l ' na, " u DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF ILLINOIS AT URBANA-CHAMPAIGN URBANA, ILLINOIS An Algebraic Definition of Knuthian Semantics by Laurian M. Chirica Department of Computer Science University of Illinois Urbana, Illinois 61801 and David F. Martin Computer Science Department University of California Los Angeles, California 90024 This research was supported in part by the National Science Foundation, Grant No. MCS 03633 A04 and by the U.S. Energy Research and Development Administration, Contract No. E(04-3)-34, PA 214. Abstract This paper presents an algebraic definition of Knuthian semantic systems (K-systems or attribute grammars) with both synthesized and inherited attributes. The approach is based on the initial algebra semantics principle formulated by Goguen, Thatcher, Wagner and Wright. Given a K-system semantic definition for a context-free grammar G, it is shown how to construct a many-sorted algebra 3C such that the semantic mapping from G-derivation trees into their "meaning" is the unique homomorphism from the initial algebra T« of derivation trees intoCJC . The practical implications of the algebraic definition of K-systems are discussed, and the combined use of Knuth's original formulation and the algebraic approach for the development of semantic definitions is advocated. The usefulness of the algebraic formu- lation of K-systems is demonstrated by its application to proving the equivalence of K-systems having the same underlying grammar. It is shown that such proofs may require verifying that a K-system possesses certain properties. To this end, a principle of structural induction on many- sorted algebras is formulated, justified, and applied. Page 2 1. Introduction The purpose of this paper is to present an algebraic treatment of Knuth's approach to the specification of the semantics of context-free languages [14, 15, 16], Knuthian systems, hereinafter called K-systems , have been called declarative semantics [6, 7, 31 , 32], K-grammars [1] and attributed grammar s [3, 13, 20, 21]. The latter term seems to dominate in the more recent litera- ture, but it emphasizes a syntactic point of view. We view K-systerns as Knuth originally intended, namely as a method for assigning meanings to derivation trees on context-free grammars. This viewpoint is in full agreement with the algebraic approach to semantics. K-systems have been used in many Computer Science investigations, such as programming language translation [20], program correctness [9], program optimization [24], question-answering systems [25], semantics of programming languages such as SIMULA 67 [31] and PL360 [6], and programming language design [1]. Also, various formal models for programming language syntax and translation related to Knuthian semantics have been proposed [5, 17, 23, 30]. Goguen and Thatcher [11] have given an initial algebra formulation of K-systems in which only synthesized attributes can appear. They leave open the problem of including inherited attributes in an algebraic formulation of K- systems. We propose a solution to this open problem in this paper. Our solution, a formulation of K-systems within the framework of initial algebra semantics, combines the intuitive appeal of K-systems with the theoretical power of algebraic methods. This approach permits a simple and precise definition of ; K-systems, and has been used by Chirica [4] in proofs of correctness i of programming language translators. Although we argue that it is possible to 'convert any K-system definition (including a circular one) into an equivalent jalgebraic definition, we examine the more useful alternative of using both Page 3 methods for writing semantic definitions. We are thus freed from the combi- natorial aspects of K-systems such as tree traversal, topological sorting and especially the circularity problem, while taking full advantage of their intuitive appeal. A more recent version of [11], [12], mentions two unpublished suggestions for solving the problem discussed in this paper. One, by Goguen and Zamfir, is different than our approach; the other, by Burstall, appears to be more related to our work. In this paper we assume a knowledge of K-systems, many-sorted (hetero- geneous) algebras [2, 11], and Scott's lattice-theoretic approach to computa- tion as modified by Gordon [10]. Section 2 briefly reviews the initial algebra approach to semantics. The fundamentals of chain-complete posets and K- systems are discussed in Section 3. Section 4 presents an algebraic formula- tion of K-systems together with discussion and an example. In Section 5 we discuss the practical implications of the algebraic definition of K-systems, where we also argue in favor of the combined use of Knuth's original formula- tion and the algebraic approach for the development of semantic definitions. Section 6 demonstrates the usefulness of the algebraic formulation of K-systems by applying it to the problem of proving that two K-systems are equivalent. A principle of structural induction on many-sorted algebras useful in carry- ing out such equivalence proofs is formulated, justified, and applied. ; Page 4 2. Notation and Mathematical Preliminaries In this section we briefly review the initial algebra approach to semantics and the elementary theory of complete posets. In the sequel, e denotes the empty string, $ the empty set, and N the non-negative integers. N together with its natural order relation is denoted w. 2.1 Initial Algebra Approach to Semantics Many-Sorted Algebras Let^ be a set of symbols called sorts . An ^-sorted operator domain (or signature ) E is a family {E } .*» of sets. An element o e Z is called an operator symbol of type , arity w, ( target ) sort s, and rank lg(w). 2.1.1 Definition (Many Sorted Algebra) Let E be ani-sorted operator domain. An (i-sorted) E-algebra consists of a family X = {X } « of sets together with a function a Y : X w -> X for S S^n AS each a e E , w e£*> s e%. If w = s-,s 9 »«»s , where n>0 and s. e^, ly the same name). The functions a, are called operations in the algebra X and are said to have the same type , arity , sort , and rank as the operator symbol a with which a x is associated. An operation g„ of type , is a "unction o^,: {e} -> X g , called a constant (or nullary ) operation . It is )ften convenient to identify o. with the element oAz) of X ; in this case ' x is called a constant of sort s. Many-sorted algebras (E-algebras) are ilso called heterogeneous algebras [2]. -.1.2 Definition (Homomorphism) Let X and Y be E-algebras. A E-homomorphism is a family f When w is a string, lg(w) denotes the length of Page 5 h = (h : X, ■> Y } o of functions (abbreviated h: X -*■ Y) such that for all s s s se ;d e,** xi , o e E , and x e X w , h s (a x (x)) = a y (h w (x)) , where if w = s,s 9 »»»s , n>0, s. e & , l and I n h (x) = . When w = e, h (x) = e and the above equation s 1 i s p n reduces to h (o x ) = Oy. H Initial Many-Sorted Algebras The key property for the application of E-algebras to programming language semantics is given by the following. 2.1.3 Definition (Initial Many-Sorted Algebra) A Z-algebra X is initial in the category of E algebras and E-homomor- phisms if and only if there exists a unique E-homomorphism from X to any other E-algebra. IS 2.1.4 Proposition For each operator domain E, there exists an initial E-algebra T„. K The proof of the existence of T„ is constructive, and is typical of word algebra constructions [ 2], In order to apply the property of initiality to the semantics of context-free languages, we define a class of many-sorted algebras associated with each context-free grammar. Let G = be a context-free grammar (CFG), and let V = V., u Vj. Following [12], we define a class of many-sorted algebras, called G-algebras, associated with G. Define a string mapping nt: V* ■*■ VjJ such that nt(x) is the string of nonterminals, in their same order, appearing in x. Let II = {tt p c P} be a set of symbols in one-to-one correspondence with P; for convenience, the symbols of 11 win be indexed by production numbes. Page 6 We define a V.,-sorted operator domain, also called G to identify it with the grammar G. For all A e V.,, w e Mt 9 let G w,A = {7r p € n I p £ P ' p = A "* x ' nt ^ = w} and G = {G .}. The elements of the initial G-algebra T r represent the W,A b derivation trees on CFG G. The fundamental precept of initial algebra semantics of context-free languages is that any G-algebra X can provide a semantic definition for the context-free language L(G) with (abstract) syntax T~; the unique homomorphism h: T fi '•*■ X maps each G-derivation tree into its "meaning" in a syntax-directed manner. 2.1.5 Example Consider the CFG G = , where V N = {S, L, A, B}, Vy = {a, b}, and P consists of 1. S -* AL 4. A -> aA 2. L -> LB 5. A -> a 3. L + B 6. B -* bB 7. B ■> b The initial algebra T fi has carriers T G D for all D e V,,, where T~ D is the set of linear representations of all D-rooted derivation trees with terminal frontiers. The operations of T~ are defined as follows. Let A -*■ x be the p-th production, where nt(x) = A,A 2 »»»A , n>0. The corresponding operation is (Vt g (V". t n ) = V-VV' n>0 { \\ = \ ' n = °' where t i c T Q A , l. of elements in P we write Ix. to designate the lub of the 1 lew l-r-A 1 set U(x-). If tne empty set has a lub in P it is called the least or the irN 1 Page 8 bottom element of P and is denoted i p or simply 1 when there is no danger of confusion. i p has the property that i p < p for all p e P. A poset with a least element is called strict . A subset X c P of a poset Pisa chain in P iff for x, y e X, either x < y or y < x (total order). The number of elements in a finite chain is called the length of the chain. The height ht(P) of a poset P without infinite chains is the least upper bound (in u) of the lengths of the chains in P. A poset of finite height is called elementary . The notion of height of an elementary poset has more intuitive appeal if we think of it in re- lation to the usual graph representation for a poset (Hasse diagram), in which case the height is the number of nodes in the longest upward-oriented path in the graph. A poset is ( chain ) complete iff every non-empty chain has a Tub. n Since any finite chain x-, < ... < x , n > 0, has a lub Ix. = x , it fol- lows immediately that any elementary poset is complete. A chain complete poset will be called a domain . If a complete poset is also strict (elemen- tary) then it is called a strict (elementary) domain. We use the following facts in the sequel : (a). Any set together with its equality relation is an elementary domain of height 1 ; (b) For any set A, the disjoint union A = A u {i} with the order relation a < b iff a = i or a = b, is a strict elementary domain of height 2; (c) If A,,..., A are elementary domains, then so is A, x ... x A , and n ht(A 1 x ... x A n ) = i ht(A.) - n + 1. 1 n i=1 l Page 9 C ontinuous Functions on Posets Let P and Q be posets. A function f: P -> Q is monotonic iff for all p, p' e P, p < p' implies f(p) < f(p'). A function f : P -> Q is ( chain ) continuous iff for any non empty chain X with lub in P, the set f(X) has a lub in Q and f(l Jx) = J f (X) . The set of all continuous functions from P Y Q to Q, denoted [P -+ Q], is a complete poset. For e^jery strict and complete poset P there exists a continuous function Fix p : [P -> P] ■*• P, called the least fixed point operator , defined as 00 Fix p (f) = M fVp) where f = l p (identity function on P) and f = f • f ] . For strict posets P and Q, a function f: P -*- Q is called strict if f(x p ) = ±q. In the remainder of this paper, completeness means chain completeness and continuity means chain continuity. Page 10 3. Knuthian Semantic Systems (K-Systems) In this section we review and discuss K-systems as originally presented by Knuth. We then impose a few restrictions (which result in no loss of generality) on K-systems which make them more easily adapted to their algebraic formulation in Section 4. The use of the word "domain" in this section generally refers to unordered sets of values, and not to the domains of Section 2.2. The basic purpose of K-systems is the syntax-directed specification of a mapping from derivation trees on a context-free grammar to corresponding semantic objects (which can be vector-valued). K-systems ignore how deri- vation trees are obtained from the terminal "source" strings which consti- tute their frontier. Nevertheless, K-systems are sometimes regarded as defining a relation (since the CFG may be ambiguous) between source strings and semantic objects, as depicted in Figure 1. SOURCE STRINGS DERIVATION TREES SEMANTIC OBJECTS Figure 1 - Relations Defined by K-systems Informally, K-systems as presented by Knuth in [14] consist of (1): a CFG G = in which S appears in the left part of exactly one production and in the right part of no productions; (2): two disjoint sets of symbols associated with each symbol of G, called inherited and synthesized attributes, where S must have at least one synthesized and no inherited Page 11 attributes; (3): a semantic domain D for each attribute a; (4): a set of semantic rules associated with each production. Each semantic rule is a function f . : D x ... x D -> D which defines the value of an attribute pka a-, a. a a of an instance k of a symbol appearing in production p in terms of the values of attributes a-,,..., a, associated with other instances of symbols in the same production. In [16], no attributes are associated with terminal symbols. This is a reasonable restriction, since the association of attributes with terminal symbols is unnecessary and even bad because it binds the semantic definition to concrete rather than abstract syntax. We now discuss how the semantic rules are specified. Let A -> a Q B 1 a 1 ...B r a r , r > 0, where a Q ,..., a e Vi and A,B, ,...,B e V.,, be a production (say the p-th) of G (strictly speaking, r depends upon p, but for typographical convenience this dependency is not explicitly indicated). The B., l a Q is called a terminal production ; in this case equations (1) are absent and equation (2) reduces to y.A = G (x.A). When A = S, x.S does not appear in the above equations. A K-system in which only synthesized attributes appear is called purely synthesized . F . (x.A, y.B,,..., y.B ) is a vector of terms interpreted to define semantic functions f , for all inherited attributes a of B. , l* L y X D y = {a,b}* A - X B - X Productions Semantic Rules 1. S + AL x.S = x.L x.A y.L = x.A 2. h *■ L z B x.L 1 = x.L^y.L^x.B y.L 2 = y.L ] 3. L -> B . x.L = y.L n x.B 4. A 1 -> aA 2 x.A, = a^x.A 2 5. A -> a x.A = a 6. B 1 ■*■ bB 2 x.B 1 = b^x.B 2 7. B -> b x.B = b The primitive semantic functions in this K-system are string concatenation ( ) and the string constants "a" and "b". The semantic objects are elements of U x = {a,b}*. This K-system defines the source string-to-semantic object relation Page 15 { | k,m,n>0, i .>0, Uj L x.S = x.L^z.L 2. L l * L 2 B x.L ] = x.L 2 ^z.L 2 °x.B 3. L ■* AB x.L = x.A*^ x.B z.L = x.A 4. A, ■* aA 2 X«M-i — Q X«Mq 5. A -> a x.A = a 6. B 1 ■+ bB 2 x.B, = b ^ x.B 2 7. B -> b x.B = b Page 16 Note that the grammar underlying this K-system is not the same as that underlying the K-system of Example 3.1. 3.4 Discussion The problem with defining functions by terms in a certain language is that a term does not quite define a function in the sense that we do not really know the intended domain of definition of that function. For instance, the term "z+1" over integers may define the function f: Z -> Z or the function f ' : Z x Z -*■ Z, where f = Az.z+1 and f = Az-jZ^.z, + 1. This may not seem very important, but the concept of attribute dependency jj_ important in K- systems and it is based on the specification of the domain of definition of semantic functions. According to Knuth [14, p. 133], "(an) attribute a is defined to have the value v ... if, in the corresponding semantic rule f . : D x«»-x D -> D pja a, a. a all of the attributes a,,..., a. have previously been defined to have the respective values v -,,..., v. at the respective nodes,... and v = f pj - ot (v 1 ,... , v t )." This means that if f: Z x Z -> Z is defined as f(z,,z ? ) = z, + 1 , then f(u,v) cannot be computed if u is defined but v is not. In current practical use of K-systems, which use the specification language described previously, the information about the intended domain of definition of semantic functions is not usually provided. Without the know- ledge about the intended domain of definition, we cannot establish the graph model of attribute dependency in the sense proposed by Knuth. Most often this information is deduced on syntactic grounds; e.g., "z+1" defines a function f: Z ->■ Z of one variable since there is only one variable in the term "z+1". This approach fails if we wish to be able to evaluate the term Page 17 "if x>0 then x+1 else y" when x > and y is undefined. The solution to these problems is based on the idea of formalizing the concept of a variable being defined or undefined: we simply add a new value 1 to the sets D such that an "undefined" attribute has the value a i. By extending the primitive semantic functions to D u {i} we can say that an attribute a, directly depends on attribute a~ iff the semantic function defining a, is strict in the argument corresponding to ou. This does not conform to Knuth's attribute dependency definition, but seems to be better suited for practical applications where we do not want to specify intricate domains of definition. In our definition all semantic functions attached to the same production will have identical domains of definition. These domains need not be specified since they will be implied by the form of the production. By proper extension of the primitive semantic functions any desired dependency among attributes can be achieved (e.g., the primitive semantic function Xpxy.rf p then x else y can be extended in various ways, making it strict in all or some of its arguments). The last problem to be considered is how to compute the attribute values. This problem is not given a full formal treatment in [14, 16]. In [14] it is indicated that well known tree manipulation algorithms may be used for attribute evaluation by supplementing these algorithms with the necessary attribute computation steps. The work of Fang [7], Bochmann [3], and Kennedy and Warren [18] shows that this is not a trivial task. The main contribution of the algebraic definition given in Section 4 is a precise mathematical definition of this aspect of K-systems. Furthermore, the definition is independent of any tree manipulation algorithm. Page 18 4. Algebraic Definition of Knuthian Semantics In this section we propose an algebraic definition of K-systems [14, 15, 16] and discuss the implications and the use of this definition for semantics and translation of programming languages. 4.1 Definition (K-System) A Knuthian Semantic System ( K-system ) consists of: (a) A context-free grammar G = in which S occurs in the left part of only one production and in. the right part of no productions; (b) A Z-algebra D for a given set of sorts & and a given ^-sorted operator domain Z. The carriers D , s e^, of D are called semantic domains and the operations f e E , e£* x %> of D are called primitive semantic functions ; (c) Two functions ~~ : V N -> £* and _: V N -*-,£*, called attribute type assignment rules , which associate with each nonterminal A e V. two strings A and A in^* called, respectively, the inherited attribute type and the synthesized attribute type of A. It is assumed that S = c and S^ f e; (d) Semantic functions . For every production (say the p-th) f A -> a Q B 1 a 1 ... B p a r in P, where a Q , a,,..., a e V£, A, B, ,..., B e V.,, r>0, there is given a function G of type iff A j- e, and for p — i — r — — every k, l iff B. f e. All semantic functions must be derived operators of the indicated type in algebra D in the sense of [12], M If A = s r ..s n e^ + , then D A = D s x... xD_. If A = e then D A = {c}. A 1 Sn A The set D is called the inherited attribute domain of A. D-, called the t For typographical convenience, the dependence of r on p is not shown. Page 19 % ^ h ^ L3til ^^^ of A, is defined similarly. The functions F pk are of type , i.e., R B B. F^XD" 1 x...xD^D k . pk If K - s r - V then F is a B k ^P le of fUnCti ° nS "^ n F,,.: 9*xlf' x ... »»^» s .. lfii * 1 pki l *■ a c-imii a rlv Note that for terminal productions The functions G are defined similarly. Note j.. „,. c and the semantic functions G A ■> a Q there are no semantic functions F pk and the p are of type . 4 2 SpecJficjiM^JlIJ^^^^ It is clear that a specification language is built into Definition 4.1, The tasic idea is that of assigning variable symbols to every nonterminal ,„- stance in a production, according to the nontenninal type as defined by the functions « and - «. Then we use terms of the appropriate type to speedy semantic functions. The specification language outlined here is the same a5 the one discussed in Section 3, only here it results naturally from the Hebraic definition. (In a sense, Definition 4.1 formalizes the semantics of Knuth's specification language [14].) 43 ^emlemanUcsJorm^^ He must now make precise what attribute computation means, ..,, how to use a K-system for semantic definition of context-free languages. The idea is simple and is based upon the following observation about how a Knuthian def inition (in the informa, sense) works For any given nonten.na A £ v , A f S, and any given G-derivation tree t* with root A, the values of the synthesized attributes y.A of node A are obtained in a unic^e way fro ,„ the values of its inherited attributes x.A. If the definition is Page 20 circular then the value of some attributes cannot be computed (i.e., they are undefined), but those which are defined are uniquely obtained from x.A. This observation is true first because in any Knuthian definition (in the informal sense) if the attribute values can be computed then they are unique [14, p. 135], and second because of conditions (iii) and (iv) laid down in Section 3. The correspondence x.A ^ y.A is a partial function. If we use a special value i to represent undefined attributes, then the correspondence x.A m- y.A is a total function. Accordingly, we assume that an element i^ s has been added to every carrier D of Z-algebra D. We also assume that every D , s e*5, is made into a strict elementary domain (D ) as indicated in Section 2.2. Finally we assume that the operations of D have been extended to continuous functions. In the terminology of [12], D is a (chain) con- tinuous Z-algebra. From Proposition 4.13 of [12], derived operators of continuous algebras are continuous. Therefore, the semantic functions 4.1(d) are continuous functions on their respective domains. In view of the previous discussion, the semantics of a tree t with A A root A in grammar G is a function a. A e [D ■*■ D-] which maps inherited attri- butes into synthesized attributes of node A. Therefore, the problem is to define the correspondence t h-o.A between derivation trees t and functions a. This discussion applies to trees with roots other than S. For trees with root S the semantics continue to be y.S e D— , i.e., the value of the synthesized attributes of node S. We write a.S for uniformity. An appropriate tool for mapping derivation trees into their semantics is provided by the initial algebra semantics. We define a G-algebra X with carriers K A , A e V^ and operations p , p e P such that the unique homomorphism h: Tg ->X maps derivation trees t into semantic objects o as discussed above. Page 21 The choice of carriers has already been made by the previous discussions, i.e., K A = [D -* D-] for all A e v ' In order to complete the definition of J£ we must define the operations , for every pep. Consider a production p = At a B-,a,...B a , r > 0. The operation d is r F k *k a function : K R x . . . x Kg -* K» mapping < a.B. e [D -* D ] > l be the least fixed-point solution of system (4) with respect to x.B k , l^k3C, h = {h„ A e V.,}, which maps derivation trees on G into their semantics, where h A : T G>A -> [D*-l£], A e \I {V A f S, h S : T G,S * & The reader might now expect to see a proof that the algebraic defi- nition of K-systems is equivalent to Knuth's original method of semantic definition, perhaps as a theorem of the form "For any t e T~ s , h s (t) = y.S, where y.S e D- are the synthesized attribute values obtained using Knuth's method." However, K-systems as presented in [14, 15, 16] give only an informal idea about how attribute values are to be computed. Consequently, the above theorem has no mathematical meaning. The purpose of the algebraic definition given in this section is to provide a formal treatment of this aspect of K-systems. Of course, one may use the necessary tree manipulation algorithms, as suggested by Knuth, and give an algorithm for attribute computation. This algorithm, which would have to be accepted on intuitive grounds, could then Page 23 be used as an "operational" definition of K-systems. In this case we can raise the question of proving a theorem relating the algebraic approach to f the "operational" approach. We leave this possibility open. Instead, we claim that the algebraic definition ij_ the formalization of Knuth's method. 4.4 Fixed-point computation of attribute values We now examine the fixed-point computation implied by the solution of equations (4). Equations (4) can be viewed as a system of equations in unknowns x.B, , lD K , l x.A pi ' ' pr B l B r B l B r H .: D x ... x D -> D x ... x D X . M A f l F r are continuous for eyery x.A e D . Let X = D x ... x D . Then H .: X -* X, as a continuous function, has a least fixed-point in X defined X • M t This is another instance of a well known problem: proving the equivalence of operational and mathematical semantics. An excellent analysis of this problem can be found in [10]. Page 24 as oo (9) Fi *x< H x.A> = u h; iA u). Therefore the solutions E: D -> X of system (4) are E = Ax e D .FiXw(H ). We know that with Knuth's approach, even if there is a circularity in the definition, the attribute computation must terminate (unsuccessfully) with some attributes being undefined. It is disturbing therefore to have introduced the least fixed-point function Fix x in equation (8) since this function, from a computational point of view, may lead to a nonterminating computation. We now briefly argue that the least fixed-point computation always terminates in a predetermined number of steps and, moreover, it suggests an algorithm for computing attributes in derivation trees. Given a continuous function f: Y -> Y on any strict domain Y the least fixed-point of f, FiXy(f), is the least upper bound of the ascending sequence of elements in Y i = f°(i) < f\±) < f 2 (i) < ... This property suggests a computational procedure for FiXw(f) (provided that everything else is computable in some sense): iterate the application of f to itself, starting from the element i e Y. This computation may or may not terminate, and in this sense, Fix v is a partial function. However if Y is. an elementary domain (has no infinite chains) then the computation always terminates (if f^i) = f 1+1 (±) forborne i then Fix„(f) = fU)). B l B r The domain X = Dx...xDj_s_ elementary since it is a cartesian product of elementary domains. If we evaluate the height of X we obtain an upper bound for the number of steps in which the computation of Fix„(H „) terminates. The sets D , s e »§ , have been made into strict elementary domains by adding the elements i Q , and by introducing the order relation < defined s Page 25 in Section 2.2. The height of each D $ is ht(D $ ) = 2. If the length of B k B k is £., l o.B 1 (x 1 .B 1 ),..., a.B r (x r B r )), l v.B = 2. B -* 1 v.B = 2 t s.B 3. L -> B v.L = v.B s.B = s.L £.L = 1 4. L 1 •> L 2 B v.L^ = v.L 2 + v.B s.L 2 = s.L^ + 1 £.L, = £.l_ 2 + 1 s.B = s.L, 5. N -> L v.N = v.L s.L = 6. N -*■ L 1 • L 2 v.N = v.L-. + v.L 2 s.L ] = ^ ♦ La "" "X/» La Attribute s is inherited; v and £ are synthesized. The Z-algebra D for this K-system (Definition 4.1) consists of sorts A = { int , ral } , carriers D. . = Z (the integers) and D , = Q (the rationals), and the operations of binary ra i — addition and unary subtraction on integers, binary addition on rationals, exponentiation of rationals by integers to yield rationals, and constants 0,1 e Z n Q. The attribute type strings for each nonterminal are Inherited B" = int L = int N = e The attribute valu? domains (and the variable names ranging over these domains) are Syn thesized B = ral L = ral int N = ral tas defined in Section 3. Page 29 D B = z (s.B) D L = i (s.L, s.L r s.L 2 ) $ - (c> I D§-=Q (v.B) dL . Q x z (. ) qN » Q (v.N) The semantic functions need not be .ade explicit since their do.ain is implied by Definition 4.1, and they are given by the se.antic equations interpreted in the algebra D. I„ order to obtain the new purely synthesized K-system and the associate G-algebra*. we first assume that Z and Q have been replaced by l x and Q^ and that the operations of addition, etc. have been replaced by their strict extensions. The carriers K ft - [D A * lAL A . ¥„. of *are K N = [{c} - QJ = Qj. where = denotes isomorphism of complete posets. Ue use K„ - Q, and K L - [2 * Q ] X [2 * Z ] for convenience, to avoid the use of projection function, .n'its semantic equations, the new pureiy synthesized K-system uses the notation o y .N . K,; V L. V L V a v .L 2 . U x + Q^S V L ' °r L l> V L 2 £ L Z i" Z i ]; V B £ K B- Page 30 The new purely synthesized K-system : 1. B ■* a y .B - As.O 2. B ■+ 1 a y .B = As. 2 t s 3. L -> B a y .L = Xs.(a v .B(s)) a..L = As.l 4. L 1 - L 2 B a y .L 1 = As. (a v .L 2 (s+l ) + a y .B(s)) a £ .L 1 = As.(a v .L 2 (s+l) +1) 5. N -> L a y .N = a v .L(0) 6. N - L ] . 1_ 2 a y .N = ^.L^O) + a v .L 2 (Fix z (-a £ .L 2 )) We now show how to obtain the new semantic equation for production 6. Equations (4) corresponding to production 6 of the original K-system are (9) s.L 1 = s.Lp = -ap.L 2 (s.l_ 2 ) the solution of which is = <0, Fix z (-a £ .L 2 )> e Z ± x Z . Thus a.N = a .L-,(E, ) + a .L 2 (E 2 ), which yields the desired equation. The new semantic equations for the other productions are obtained similarly. The associated G-algebra operations are 8, = As.O G 2 = As. 2 + s 8 3 (v) = As. VV *!» v 2 ) = ^.^(s+l) + v 2 (s), £-,(s+l) + 1> e 5 (v,£) = v(o) 8 6 (v 1 , 9. ]t v 2 , l 2 ) = v^O) + v 2 (Fix z (-& 2 )), Page 31 where v, v ] , v 2 e [Z ± ■* Qj, SL t i ]t l^ e [Z ± -> Z ± ], and s e Z ± . The meaning of the string 1101.01, whose derivation tree is t = ^6^4^ 7r 4^4^3^2^2^1 ^2^4-^3-^ 1^2^ ' iS h N (t) = e 6 (e 4 (e 4 (e 4 (e 3 (e 2 ),e 2 ),e 1 ),e 2 ),e 4 (e 3 (e 1 ),e 2 )) , which evaluates to h N (t) = 2 3 + 2 2 + 2° + 2" 2 = 13.25. Details are left to the reader. Page 32 5. Some practical considerations The algebraic definition of K-systems has some important consequences concerning the methodology for developing semantic definitions in general, and the use of K-systems in particular. First of all, since we are convinced that the algebraic formulation of K-systems is at least as powerful as Knuth's original approach, we can work directly in the algebraic framework as suggested in [12]. There is no loss in power or convenience, and by making evident the algebraic structure of the semantic domain, our understanding can only gain from such defini- tions. Moreover, the algebraic approach has none of the "well -definability" problems which appear when using Knuth's approach, and we also benefit from established proof techniques available in the algebraic method. An even better approach is the combined use of both methods, algebraic and Knuth's, for the following reasons. The algebraic method, however precise and elegant, offers little aid to the intuition, since it is not always obvious which semantic algebra is required in a particular application. On the other hand, Knuth's approach is more intuitive, especially when computability is of great concern, such as in translator applications. In these cases Knuth's approach should help us discover what kind of algebra is needed in a particular application. It is a definite aid to the intuition if we can think in terms of attribute values being passed up and down in derivation trees and write our intuition in a precise algebraic manner. There is an important observation to be made concerning the combined use of the Knuthian and algebraic approaches. We do not recommend first writing a Knuthian definition and then trying to express it algebraically as in Section 4.3. This was done only to show that any Knuthian definition can be given an algebraic formulation, including circular K-systems„ This Page 33 led to recursive systems of equations which in actual practice can and should be avoided in the interest of simpler definitions. The recursive nature of system (4) in Section 4.3 has two origins: one in the fact that Knuthian definitions can be circular , the other in our choice of carriers as K„ = [D ■*■ D— ], A e V... Assuming carriers of this form may lead to recursive systems even though the definition is not cir- cular. This happens because we are assuming that e\jery synthesized attribute of A is a function of all inherited attributes of A. In practice, however, not all of this functional dependency may actually exist. To determine the actual dependency, the following definition is useful: Let x..A be an inherited, and y..A a synthesized attribute of A e V... We say that y..A depends on x..A iff there exists a derivation tree with root A such that there exists a path from x..A to y..A in the • j attribute dependency graph on that tree. Determining, for each A e V.,, the dependency of the synthesized attributes of A upon the inherited attributes of A requires an algorithm \/ery similar to Knuth's K-system circularity detection algorithm. Once the dependencies were determined, the algebraic carriers could be defined accordingly. Un- fortunately, such an algorithm has exponential complexity [13] and therefore may not be practical . We advocate a different approach which imposes a reasonable additional requirement on the K-system specifier. In addition to the requirements of Definition 4.1, the specification of a K-system would include for each A c V^ a specification of the dependency of the synthesized attributes of A upon the inherited attributes of A, expressed as a directed graph d„. The nodes of d. are labeled with the attributes of A, and an arc is contained in d fl iff y...A depends on x..A. This information is well M J 1 Page 34 known to the system specifier, since he is well aware of the desired dependency between attributes in his K- system definition. The dependency graphs d„, A e V.., and Knuth's graphs D , p e P [14, p. 135] can be used to check the consistency and completeness of a K-system definition as follows. For each production (say the p-th) A ->• a Q B-, •••B r a r : (a) Construct the directed graph union d_ = D u d R u ••• u d R ; (b) Consistency Check : d is acyclic; (c) Completeness Check : If there exists a path from x..A to y..A in d , then there must exist an arc in d fl . 1 J M This algorithm has the same complexity (considerably less than exponential [8]) as a transitive closure algorithm. Alternatively, in an algebraic definition of a K-system, the consis- tency check can be expressed as the condition that the equation systems (4) of Section 4.3 can be solved by successive elimination of the variables x.B, , l e^ * x^ , to continuous functions. 5.1 Example We now repeat parts of Example 4.5 in light of the foregoing discus- sion. In addition to the previous specification, the dependency specification is . _ V.N . s.L v.L Ji.L . s.B v.B N * L V / * V Page 35 This specification is consistent with the semantic equations of the original K-system. In particular, it indicates that Undo es not depend on ^L, and therefore, the K-system is nonrecursive , as we shall see. The new purely synthesized K-system and associated G-algebra ft' differ from what was previously obtained only in the parts affected by the dependency specification, namely the carriers of ft, and the algebraic operations associated with productions 3, 4, and 6. According to the dependency specification, the carriers of X' are KJ-Q K^ = [Z ■* Q] x Z K£ = [Z - QL Ue now rederive the semantic equation associated with production 6; production. 3 and 4 are treated similarly. Since IX does not depend on s.L, then neither does o r L. i.e., O..L . Z, and thus the equations corresponding to (9) become s.L 1 ■ {10) The solution of (10) is • <0. V^' obta,ned Wlth ° Ut ^"^ to fixed-point methods, and thus we obtain the foil owing purely synthesized K-sy:e, a .B = Xs.O v a .B = Xs.2ts a .L = \s.(a v .B(s)) v v o..L = 1 1. B ■*" 2. B ■* 1 . 3. L ♦ B 4. h * L 2 B 5. N + L 6. N > L ] • L 2 o v .L, =Xs.(o v .t 2 (s+l) + o v .B(s)) V L 1 = V L 2 + 1 o v .N - V L(0) a v .l) " o^L^O) + o v .L 2 (-o r t 2 ) The operations of the algebra X' are 9] = Xs.O e£ = Xs.2ts e'(v) = 8^(v r £j, v 2 ) = e^(v,£') = v(o) e 6 (v r £ i' v 2» % t) = v i (0) + V 2 ( " £ P» where v, v-. , v~ e [Z -»■ Q] and £', ll, £-> s £ Z. Page 36 Page 37 6. Equivalence of K-Systems In this section we demonstrate the usefulness and elegance of the alge- braic formulation of K-systems by applying it to the problem of proving two K- systems equivalent, in the sense of Definition 3.2. Our method applies only to K-systems that have the same underlying grammar . Carrying out such equiva- lence proofs may require proving that one of the given K-systems possesses certain properties. To this end we formulate an induction principle for many-sorted algebras. All of the foregoing concepts are illustrated by an example. We first present our method for proving two K-systems with the same underlying grammar equivalent in the sense of Definition 3.2. The method is given and justified by the following theorem. 6.1 Theorem Let K and M be K-systems with the same underlying grammar G = V T , P, S>, and associated semantic algebras 7C and^U, respectively. We assume that K_ = M_, i.e., the carriers of sort S in algebras Jv and "L are identical. Then, K and M are equivalent (in the sense of Definition 3.2) if there exists a homomorphism e: ju -*• m such that e<- is the identity function on K s (i.e., e $ = 1 K ). Proof : Since T r is initial, there exist unique G-homomorphisms h: Tp ■*■ "}(/ and g: T r -> a ftl> Since the composition of G-homomorphisms is also a G-homomorphism [12], we have eoh = g, i.e., the diagram T G % — >in Page 38 commutes. Thus for all t e T f <., e s (h s (t)) = g s (t), which implies for all t e lr c» h s (t) = g s (t) (since e s = 1., ), establishing the equivalence of K and M. IS We now illustrate the use of Theorem 6.1 via an example. 6.2 Example Consider yet another K-system for the semantics of binary numbers, represented by Product! ons Semantic Rules 1. B -> v.B = £.B = 1 2. B ■> 1 v.B = 1 Jl.B = 1 3. L ■* B v.L = v.B 4. L l - L 2 B v.L 1 = 2 * v.L 2 + v.B JI.L-, = £.L 2 + 1 5. N •> L v.N = v.L 6. N-L r L 2 v.N = v.L 1 + v.L 2 * 2+(-£.L 2 ) This K-system is purely synthesized and has the same underlying grammar as those of Examples 4.5 and 5.1. Its E-algebra D consists of sorts (int., rail , carriers D. . = Z (the integers) and D , = Q (the rationals), and operations i n t ra I u > l s -» +, *, and l as in Examples 4.5 and 5.1. The attribute type strings for each nonterminal are F=U=N = t.,B_=L L = raj[ int., and N = ral . The synthesized attribute value domains (and the variable names ranging over these Page 39 domains) are D^ = Q x Z () [t = Q x z (, , ) D^-= Q (v.N) Let ^Tldenote the semantic G-algebra associated with the above K-system; the carriers of nt are M = M, - Q x z and M N = Q. The operations of Ttt. are T 1 = <0,1> Tp = <1 ,1> T 3 (V,£) = T 4 (v 1 ,£ 1 ,v 2 ,i» 2 ) = <2*v 1 + v 2 , l } + 1> T 5 (V,H) = V T 6 (v 1 ,il 1 ,v 2 ,£ 2 ) = v 1 + v 2 *2+(-£ 2 ) where v,v-,,v 2 e Q and fc.JL.JU e Z. We now prove the equivalence of the above K-system and the K-system of Example 5.1, whose semantic algebra is "J(J with carriers K', K/, K' and opera- tions 9-!,..., GA as defined in Example 5.1. Following Theorem 6.1, it is suf- ficient to define a mapping e: Jc' >P tyl> where e = {e.., e. , e R } and e., = 1~, and to show that e is a G-homomorphism. To this end, define e«: K\ ■*■ M», A = N,L,B as e N = ] Q e L : [Z ■* Q] * 1 -> Q x Z where e L (f,z) = e B : [Z v q] -> q x z where e g (f) = . To show that e is a G-homomorphism, the following analysis by cases must be performed. Page 40 (al) SHOW e B (ej) = t ] LHS = e B (Xs.O) = = <0, 1> RHS = <0, 1>. (a2) SHOW e B (6^) = x 2 LHS = e B (As.2ts) = = <1 , 1> RHS = <1, 1>. (a3) SHOW e L (e^(v)) = x 3 (e B (v)) LHS = e L (v, 1) = RHS = t 3 (v(0), 1) = . (a4) SHOW e L (e^(v r £], v 2 )) = T 4 (e L (v r Z\) , e B (v 2 )) LHS = e L (Xs.( Vl (s+l) + v 2 (s)>, SL\ + 1) = = RHS = t 4 ( Vi (0), i\ t v 2 (0), 1) = <2*v 1 (0) + v 2 (0), i\ + 1>. (a5) SHOW e N (e^(v,a')) = T 5 (e L (v,£')) LHS = e N (v(0)) = v(0) RHS = x 5 (v(0), V) = v(0). (a6) SHOW e N (e^(v ls l\, M^ l^)) = T 6 (e L (v r i\) , e L (v 2 , *,£)) LHS = e N ( Vl (0) + V 2 (-A 2 )) = v^O) + v 2 (-Aj) RHS = TgCv^O), l\, v 2 (0), V z ) = Vl (0) + V 2 (0)*Z*(-£J). Identities (al), (a2), (a3), and (a5) are clearly satisfied. To show the Isatisfaction of (a4) and (a6) it is sufficient to prove that (11) (Vz c Z)(v i (z) = v.(0) * 2+z), where v. e [Z -> Q], and i=l for (a4) and i = 2 for (a6). Page 41 6.3 Remarks Unfortunately, equation (11) is not satisfied for aVT_ V-, ,v~ e [Z -*■ Q]. However, we need be concerned in (11) only with those values of v, and v 2 that can occur within the definition of the K-system of Example 5.1. More particu- larly, the values of V-, in (a4) and v ? in (a6) can arise only as the result of mapping L-rooted parse trees t a T~ . into their semantic meanings via the unique homomorphism h': T« -> JC , namely via h/: T~ . -> K ' . Viewing (11) as a (total) predicate on [Z -> Q], we must prove that for all t e T« , > the function v = pr-|(h/(t)) satisfies (11), where pr-, extracts the first component of h^(t) (recall that K[ = [Z->Q] x Z). We now state and apply an induction principle which will prove useful in demonstrating such results. 6.4 Theorem (Principle of Structural Induction on Many-Sorted Algebras) Let E be an ^-sorted operator domain, let X be any E-algebra, and let h: T -► X be the unique E-homomorphism from T„ to X. Let P = (P.). c be an ^-indexed family of total predicates on X, i.e., P is a total predicate on X for all s c £ . If (0) (Vs e i )(Va e E E$s )(P s (a x )) and (1) (V c 4 + x A )(Va e Z WjS )(Vx e X W )( A P W (*) - P s (a x (x))) then (2) (Vs c & )(Vt e T^) (P s (h $ (t) ) ) , w where if w = s-i»«»S and x = e X , n>0, then n 1 n A PW (><) ^ P, (Xj A ... A P ( X ) P roof : The theorem follows directly from: (a) the predicates P can be viewed as a family of sets P £ X for all s c & ; (b) (0) and (1), if true, express that P can be regarded as a su balgebra of X; (c) h(T ) is the least subalgebra t We are interested in e being a homomorphism from h'(Tp) into??! rather than one from yj to Yfl . Page 42 of X (an easy consequence of the initial i ty of T ); therefore for all s e >& , h(T v ) £ P > the desired conclusion. 81 In particular, if for all s e J& , P is a total predicate on T satis- fying (0) and (1), then P (t) is true for all s c & and t e T v , since f h: T^ ■> Tj, is the identity homomorphism. We shall now apply Theorem 6.4 to complete the proof of Example 6.2. 6.5 Completion of Proof of Example 6.2 Define the following predicates on the G-algebra "]£ of Example 5.1. (12a) P B (v) = (Vz £ Z)(v(z) = v(0) * 2+z) (12b) P L (v,Jl) = (Vz e Z)(v(z) = v(0) * 2+z) (12c) P N (q) = true , where v e [Z i> Q], £ e Z, q e Q. Theorem 6.4 requires consideration of the following cases. (bl) P B (e]) = (Vz e Z)Us.O(z) = As. 0(0) * 2+z) = (Vz e Z)(0 = * 2+z) = true (b2) P B (e^) = (Vz e Z)(As.2+s(z) = Xs.2+s(0) * 2+z) = (Vz e Z)(2+z = (2+0) * (2+z)) = true (b3) Assume P Q (v) for v e [Z -> Q]. Then P L (63(v)) = P L (v,l) = (Vz c Z)(v(z) = v(0) * 2+z) = true, which follows directly from the hypothesis. (b4) Assume P L (v-j,£|) for e [Z ■+ Q] * Z and P B (v 2 ) for v £ e [Z -*• Q]. Then P L (64(v 1 ,aj,v 2 )) = P L (xs.(v 1 (s+l) + v 2 (s)), fcj + 1) = (Vz c Z)(v-,(z+l) + v 2 (z) - (v-,0) + v 2 (0)) * 2+z) = true , which follows readily from the hypotheses. Cases (bl) through (b4) and Theorem 6.4 thus establish that for all t The usual mathematical induction can be obtained from the above principle by taking J = {inth £ = {0}, E. . = {succ} and s,, _ = $ for all other e,s int,int w,s Page 43 t £ T„ , , P.(h'(t)), which shows that pr,(h.'(t)) satisfies (11) of Section 6.3. Thus the K-systems of Examples 5.1 and 6.2 are equivalent. In order to prove the equivalence of the K-systems of Examples 4.5 and 6.2, the algebraic carriers Z and Q of Example 6.2 must be extended to domains Z and Q , and the primitive semantic functions must be replaced by their strict extensions, so that the two K-systems are comparable. Then the mappings e = {e N ,e L ,e B } such that e N = \ V [z i ■* Q i ] x [z i * z i ] " Q i x z i where e L ( f 'J) = <^Co),j(o)> e B : [Z ± -> Q ± ] -* Q i x Z ± where e B (f) = can be defined. To show that e is a G-homomorphism reduces to proving that v-,0) = 2 * v-,(0) (13) ^(1) = ^(0) v 2 (Fix z (-£ 2 )) = v 2 (0) * 2+(-A 2 (0)) where the values of , e [Z -> Q ] x [Z •*■ Z ] can arise only as the result of mapping parse trees t e T~ , into their semantic meanings via h. : Tp . -> K. . Properties (13) can be verified via Theorem 6.4 with predicates P B on [Z ± -> Q ± ] and ? L on [Z ± ■> Q ± ] x [Z ± ■> Z ± ], where P B (v) = (Vz e Z)(v(z) - v(0) * 2+z) P L (v,£) ■ P B (v) a (Vz € Z)(Vy c Z)U(z) = £(y)). Details are left to the reader. Page 44 7. Conclusion Knuthian semantic systems, or K-systems, are a useful and intuitively appealing method for specifying syntax-directed semantics of context-free languages. We have formalized, within the framework of initial algebra semantics, K-systems containing both inherited and synthesized attributes, thus combining the intuitive appeal of K-systems with the power and elegance of algebraic methods. Since Knuth's initial presentation of K-systems was informal, we have left open the problem of proving it equivalent to our f algebraic formulation. Our formulation of K-systems enables proof of the properties of the semantics of context-free languages (such as correctness of translators and their implementation) to be conveniently carried out by induction on the (context-free) syntactic structure of the underlying grammar [4]. An important byproduct of our formulation is a method for transforming a K-system containing both inherited and synthesized attributes into an equiv- alent purely synthesized K-system. The new purely synthesized K-system has the same underlying grammar as the original, but its semantic domains are functions from the original inherited attribute semantic domains to synthesized attribute semantic domains. This transformation generally requires the solution of recursive sys- tems of equations. Such a solution is achieved via fixed-point methods with all their attendant mathematical apparatus, thus requiring the algebraic specification of the given K-system to be or extended to be continuous. For practical purposes we advocate the use of only those K-systems in which these systems of equations are nonrecursive, i.e., equations that can be solved by successive elimination of variables without recourse to fixed-point methods „ We feel that such nonrecursive K-systems are sufficient for the t In effect, we offered a "mathematical" definition in the sense that the algebraic semantics is independent of any algorithm for attribute computation. In contrast, Page 45 vast majority of practical applications. To foster the use of these more practical K-systems, we have suggested that the specification of a K-system include for each A e V\, a specification of the dependency of the synthesized attributes of A upon the inherited attributes of A. The K-system can then be checked for "consistency" (the K-system is nonrecursive) and "completeness" (the K-system actually contains all the specified attribute dependencies) with respect to this specification. We advocate the use of both Knuth's original approach and our algebraic formulation as a joint means of designing and defining K-systems for partic- ular applications. Our intuition is better aided by the visualization of inherited and synthesized attribute values being computed and communicated over attribute dependency graphs superimposed on derivation trees (an advantage originally cited by Knuth [14]), whereas the algebraic formulation renders our intuition precise and enables us to use associated powerful proof techniques. The usefulness of the algebraic formulation of K-systems was demon- strated via its application to proving the equivalence of K-systems having the same underlying grammar. It was found that such proofs may require verifying that a K-system possesses certain properties. Toward this latter end, a principle of structural induction on many-sorted algebras was formu- lated, justified, and applied. t Knuth's method (as well as those in [3], [7], [18]) is "operational," i.e., the definition is given by a particular algorithm. A possible solution to proving the two definitions equivalent is to explicitely write Knuth's defini- tion as an algorithm (program) and prove it consistent with our mathematical specification using, for instance, Floyd-Hoare method. Page 46 REFERENCES [1] Berry, D.M. "On the Design and Specification of the Programming Language OREGANO," Computer Science Department, University of California, Los Angeles, UCLA-ENG-7388, January 1974. [2] Birkhoff, G. and J.D. Lipson. "Heterogeneous Algebras," J. Combi- natorial Theory , 8: 115-113, 1970. [3] Bochmann, G.V. "Semantic Evaluation from Left to Right," Communi- cations of the ACM , 19(2): 55-62, February 1976. [4] Chirica, L.M. "Contributions to Compiler Correctness," Ph.D. dissertation, UCLA Computer Science Department, 1976. [5] Culic, K. "Attributed Grammars and Languages," Department d' Informatique, Universite de Montreal, Montreal, 1969. [6] Dreisbach, T.A. "A Declarative Semantic Definition of PL360," Computer Languages Group, Computer Science Department, University of California, Los Angeles, UCLA-ENG-7289, 1972. [7] Fang, I. "FOLDS - A Declarative Formal Language Definition System," Computer Science Department, Stanford University, Palo Alto, California, STAN-CS-329, December 1972. [8] Fischer, M.J. and A.R. Meyer. "Boolean Matrix Multiplication and Transitive Closure," Conference Record of the 12th IEEE Symposium on Switching and Automata Theory , East Lansing, Michigan, October 1971, IEEE, New York, 1971, pp. 129-131. [9] Gerhart, S. "Correctness Preserving Program Transformations," Proceedings of the Second ACM Symposium on Principles of Programming Languages , Palo Alto, California, January 1975, ACM, New York, 1975, pp. 54-56. Gordon, M. "Models of Pure LISP," Ph.D. dissertation, Computer Science, Edinburgh University, Edinburgh, Scotland, 1973. Goguen, J. A. and J.W. Thatcher. "Initial Algebra Semantics," Pro- ceedings of the 15th IEEE Symposium on Switching and Automata Theory , New Orleans, Louisiana, October 1974, IEEE, New York, 1974, pp. 63-77. Goguen, J. A., J.W. Thatcher, E.G. Wagner, and J.B. Wright. "Initial Algebra Semantics and Continuous Algebras," Journal of the ACM , 24(1): 68-95, January 1977. Jazayeri , M. , W.F. Ogden, W.C. Rounds. "The Intrinsically Exponential Complexity of the Circularity Problem for Attribute Grammars," Communications of the ACM , 18(12): 697-706, December 1975. [14] Knuth, D.E. "Semantics of Context-Free Languages," Math. S yst. Th . 2: 127-145, 1968. Page 47 [15] Knuth, D.E., "Semantics of Context-Free Languages: Correction," Math. Syst. Th. 5: 95-96,1971. [16] Knuth, D.E. "Examples of Formal Semantics," Lecture Notes in Math. No. 188 , Springer-Verlag, Berlin, 1971, pp. 212-235. [17] Koster, C.H.A. "Affix Grammars," ALGOL 68 Implementation , North- Holland, Amsterdam, 1971. [18] Kennedy, K. and S.K. Warren. "Automatic Generation of Efficient Evaluators for Attribute Grammars," Conference Record of the Third ACM Symposium on Principles of Programming L anguages , Atlanta, Georgia, January 1976, ACM, New York, 1976, pp. 32-49. [19] Lewis, P.M. and B.K. Rosen. "Recursively Defined Data Types," Pro- ceedings of ACM Symposium on Principles of Programming Languages , Boston, Massachusetts, October 1973, ACM, New York, 1973, pp. 125-138. [20] Lewis, P.M., D.J. Rosenkrantz, and R.E. Stearns. "Attributed Trans- lations," Journal of Computer and System Science , 9: 279-307, 1974. [21] Lewis, P.M., D.J. Rosenkrantz, and R.E. Stearns. Compiler Design Theory , Addi son-Wesley, Reading, Massachusetts, 1976. [22] Markowsky, G. "Chain-Complete Posets and Directed Sets with Appli- cations," IBM Thomas J. Watson Research Center, Yorkwown Heights, New York, Research Report RC5024, August 1974. [23] Martin, D.F. and S.A. Vere. "On Syntax-Directed Transduction and Tree Transducers," Proceedings of the Second Annual ACM Symposium on Theory of Computing , Northampton, Massachusetts, May 1970, ACM, New York, 1970, pp. 129-135. [24] Neel , D. and M. Amirchahy. "Semantic Attributes and Improvement of Generated Code," Proceedings of ACM National Conference , San Diego, California, November 1974, ACM, New York, 1974, pp. 1-10. [25] Petrick, S.R. "Semantic Interpretation in the REQUEST System," IBM Thomas J. Watson Research Center, Yorktown Heights, New York, Research Report RC4457, July 1973. [26] Scott, D. "Outline of a Mathematical Theory of Computation," Pro - ceedings of the Fourth Annual Princeton Conference on Informatio n Science and Systems , Princeton, New Jersey, 1970, Department of Electrical Engineering, Princeton University, Princeton, New Jersey, 1970, pp. 169-176. [27] Scott, D. "Continuous Lattices," Computing Laboratory, Oxford University, Oxford, Great Britain, Technical Monograph PRG-7, 1971. [28] Scott, U. "The Lattice of Flow Diagrams," S ymposium on Semantics of Algori thmic Langu ages, edited by E. Engler, Lecture Notes in Mathematics No. 188, Springer-Verlag, New York, 1971, pp. 311-366. Page 48 [29] Scott, D. "Mathematical Concepts in Programming Language Semantics," Proceedings of the AFIPS Spring Joint Computer Conference , Atlantic City, New Jersey, May 1972, AFIPS Press, Montvale, New Jersey, 1972, pp. 225-234. [30] Stearns, R.E. and P.M. Lewis. "Property Grammars and Table Machines," Information and Control , 14: 524-549, 1969. [31] Wilner, W.T. "Declarative Semantic Definition," Computer Science Department, Stanford University, Palo Alto, California, STAN-CS-233-71 , 1971. [32] Wilner, W.T. "Formal Semantic Definition Using Synthesized and Inherited Attributes," Proceedings of Courant Institute Computer Science Symposium , New York, September 1970, Prentice-Hall, Englewood Cliffs, New Jersey, 1972, Vol. 2, pp. 25-39. Page 49 APPENDIX How to Transform K-Systems to Satisfy Conditions (iii) and (iv) of Section 3 In this appendix we present a construction which transforms a K-system which does not satisfy conditions (iii) and (iv) of Section 3 into an equivaler one that does. Let x and y be, respectively, the inherited and synthesized attributes of the given K-system, and let w and z be, respectively, new (distinct from x and y) inherited and synthesized attributes with the same value domains as x and y. Let G = be the CFG underlying the given K-systerr and consider a typical (say the p-th) production A- a B 1 a ] ...B r a r , and the following four cases. Case 1 (A ■+ S, r > 0) The given semantic equations in this case are (Al.l) x.B k = F k (x.A, y.B r ..., y.B r , y.A, x.B^..., x.B r ) , l 0) The given semantic equations are (A1.2) x.B k = F .(y.B 1 , . . . , y.B r , y.S, x.Bj,..., x.B r ) , l a n B, •••B a is. not as simple as in Case 1. First, replace (Al ) and (A2) with (A3a.2) x.B R = F .(y.B^..., y.B r , w.S, z.B-, ,..;-, z.B r ) , l S w.S = y.S y.T = y.S Note that K-system fragment (A5.2) need be added only once to deal with all S-productions (Cases 2 and 4). Also note that the former distinguished symbol S now has inherited attributes w. Page 51 Case 3 (A f S, r = 0) The semantic eqaation associated with A -> a fi , A f S, is (A2 3) y.A = G (x.A, y.A). Let A 1 ^ v., u Vj u {T} be a new nonterminal distinct from any other new non- terminal introduced in the process of transforming the original K-system into one that satisfies conditions (iii) and (iv) of Section 3. Replace A + a Q and (A2.3) by (A6.3) A -> A' x.A' = x.A w.A' = y.A' y.A = y.A (A7.3) A' -> a Q y.A' = G (x.A' , w.A'). Case 4 (A = S, r = 0) This case is similar to Case 3, but with no attributes x.S and x.S'. The replacement K-system fragments are (A6 4) S -> S' w.S' = y.S' y.S = y.S' (A7.4) S' -> a Q y.S 1 = G p (w.S'). Of course fragment (A5.2) will "connect" (A6.4) and (A7„4) to the new distinguished symbol T. Noting that w and x are inherited and y and z are synthesized attri- butes, it is easily seen that all versions of equations (A3) through (A7) in Cases 1 - 4 satisfy conditions (iii) and (iv) of Section 3. By constructing local attribute dependency graphs of the original K-system and corresponding graphs of the new K-system, it can be readily established that the two systems are equivalent. BIBLIOGRAPHIC DATA SHEET 1. Report No. UIUCDCS-R-77-881 4. Title and Subtitle An Algebraic Definition of Knuthian Semantics 3. Recipient's Accession No. 5- Report Date June 1977 6. Laurian M. Chirica and David F. Martin 8. Performing Organization Rept. No. ). Performing Organization Name and Address Department of Computer Science University of Illinois Urbana, IL 61801 10. Project/Task/Work Unit No. 11. Contract /Grant No. NSF Grant MCS 03633 ERDA Contract E(04-3)-34 12. Sponsoring Organization Name and Address National Science Foundation Washington, DC Energy Research and Develop- ment Administration Washington, DC 13. Type of Report & PeriodPA 21|4 Covered 14. 15. Supplementary Notes 16. Abstracts This paper presents or attribute grammars) based on the initial al and Wright. Given a K- shown how to construct 3-derivation trees into ilgebra Tq of derivatio Jefinition of K- systems tion and the algebraic The usefulness of the a :ion to proving the equ shown that such proofs o this end, a principl an algebraic definition of Knuthian semantic systems (K-systems with both synthesized and inherited attributes. The approach is gebra semantics principle formulated by Goguen, Thatcher, Wagner system semantic definition for a context-free grammar G, it is a many-sorted algebra K, such that the semantic mapping from their "meaning" is the unique homomorphism from the initial n trees intoX.. The practical implications of the algebraic are discussed, and the combined use of Knuth's original formula- approach for the development of semantic definitions is advocated 1 gebra ic formulation of K-systems is demonstrated by its applica i valence of K-systems having the same underlying grammar. It is may require verifying that a K-system possesses certain properties e of structural induction on many-sorted algebras is formulated, '. Key Words and Document Analysis. 17a. Descriptors ttribute grammar omplete poset nitial algebra semantics east fixed- points any-sorted algebras tructural induction >• Identifiers/Open-Ended Terms justified, and applied. I ■ COSATI Field/Group ^Availability Statement FC |M NTIS-35 (10-70) 19. Security Class (This Report) UNCLASSIFIED 20. Security Class (This Page UNCLASSIFIED 21. No. of Pages 22. Price USCOMM-DC 40329-P7 1 nn ICTqr o 117Q