Nov 9, 1990 - Maarten Fokkinga, Johan Jeuring, Lambert Meertens, Erik Meijer ... aparse tree with attribute values and then delivers the synthesised attribute ...
A Translation from Attribute Grammars to CatamorpMsms Maarten Fokkinga, Johan Jeuring, Lambert Meertens, Erik Meijer November 9, 1990 Let A G be an attribute grammar, with underlying context free grammar G and attribute evaluation rules A . The function that decorates —according to A parse - a tree with attribute values and then delivers the synthesised attribute value of the root node, is denoted 141 . We translate G into a functor F such that any parse tree for G is an element of the initial F -algebra. The attribute evaluation rules A are translated to a function 0 such that e l oD is, in a precise sense, equivalent to
IA].
1 T h e translation We begin by fixing some terminology and notations. Let AG be an attribute grammar. We define G = t h e underlying context free grammar of AG. X = t h e type of the inherited attributes (explained in (2, 3)). Y = t h e type of the synthesized attributes (explained in (2, 3)). T ——t h e set of parse trees for grammar G (explained in OD. A — t h e attribute evaluation rules of AG (explained in (5)). IA1 = t h e function that, given t E T and z E X , { decorates tree t according to rules A when the inherited attribute of the root node of t is set to z , and } yields as result the synthesized attribute value E Y of the root node of t; thus (Al : TxX Y . (Explained in (6).) In this note we construct a catamorphism for AG that is equivalent (equal) to IA1 . (Neither IA1 nor the catamorphism is intended to yield a parse tree when given an actual string. However, one can extend any AG to AG' in such a way that ( A l (t, z) yields the fully decorated parse tree; see Example 2.) Plan W e shall proceed as follows. • First we show that we may assume that A G has a simple form, so that the actual
translation can be formulated without too many indices and the like.
20
• The context free grammar G determines a functor • Lemma T is a subset of the carrier of the initial F -algebra. • The attribute evaluation rules A determine a function : (X -4 Y)F ( X • Theorem t A l l ( t , z ) =
t
Y ) •
z
We assume that we are working in the category Set, or in a Set -like category, like CPO. Simplification of the attribute grammar F o r notational simplicity we make the following three assumptions, without loss of generality. (1) Any terminal a is produced only by rules of the form As a where a in the right hand side has no attributes. This can be achieved by the addition of auxiliary nonterminal symbols, say one for each terminal symbol a (2) Any nonterminal has precisely one inherited and one synthesized attribute. This can be achieved for any AG by tupling the inherited attributes of each nonterminikl, and also the synthesized ones. Notice that a tuple may be the empty tuple E 1. All inherited attributes have one type X say, and all synthesized attributed have one type Y say. This can be achieved thanks to the following technique. Suppose we have t3rpes AO • • I AM 1 IBOI • • • Bn_i and f is a function of type A Then i Bwei can define fl : A o ft+ A= A y ••.v in + • • • for i li — i n j m — for = n inj e — _ !A —— i• t•!h e unique fct A -- 1 for any A — i + 1 A f -Function f i 4 the actual attribute types are X0, • • • XIPS-1 and Yo) • • • ) 1 i s p B X = uX O + 1 e rq i AG' •v -a- 1with 0 n h =eG nand IA']wt • inje l et Gs and o •n ti •Yna j + c n t a k e o • 1 4 4 1 A t •t a Construction Consider an arbitrary production rule of G, say w e1 the •-o r 1 h - of 3r functor „e e *-1 f• •1 - t t —)/uo • • • Vn-1 • td o t e -i „1 e - n X 1 s Here ph b 1 nis just a (unique) label of the rule, and to be very precise we should have subscripted all thee c r rule r with y p Lie t P be the set of production rule labels of G; in the -y 1 - u in the t entries a t g n 1 h ta r n e id b n f o u t X d l l e o Y o w t= 21 i n y Y g p o s ( 3 )
sequel we let P be the domain over which p ranges. Rule p determines a functor F and1all rules together determine a functor F and an F -algebra, as follows. p —1 i f n = land v — times) o li xs ...t xie (r nm i n ao tlh;e r w i s e . The product is 1 if n = 0a F = r d ne c a l l .( ,7ʻ " ___.e,AA ------- F -algebra ( 1 ) .1 for alip in • inj "' 1 p : : in follows from preceding lines vp ,F p i:n ip p n n (i is the identity functor, and 1 is the constant functor. Mono-functor F ,i ) z(FxG) = 33F X Z G for ay Similarly, z(Ep :: Fp) = Ep z f X G— i s d e fiany n type e dand function b type p ftand o rfunction a nzp .)y h • e (4) Lemma There0 exists an embedding from the parse trees of G into ( 7 / 11 a n As we haveT Proof not yet given a definition of (the algebra of) parse trees, we do it here. , i 1n ) . 1 A parse tree t for production rule p consists of an indication " node i immediate constituents to, 4 _ 1 such that, for all i , t p n rule that j" iaits a vinin p its a left e notation " n o d e n dhas (r pshand oe sside. st Let i rbusleuse y the Thus p f0 i o"node a ) r a arguments s (pt lr o ofdoperation u c node t i o n Now derme function e : (7 p t o ," o hi sa v e e(node 1.a, 4td_i s f y s m , . .a 7 p , v p a e 1 pre-inverse, since not every i n g t o , . . . , t 1 (t n o d e c o n d i t i i)each t o " n ti fhas rits t happens in Example 2.) vi in root. This n 1 o pn . o the sequel we identify *node, with lis _ t, In , ) - - - • a p lh t (a 7 r p Attribute evaluation Consider again production rule p , now provided with the at0E )a 7 tribute mks: tn 1i evaluation a 1 t_ 1' , s a t i s fi e s l n e P c: eu s sv a r i l y h 1 v o po • • ta h e )or ) p • V slightly more suggestive (the A is explained below) e r c o n n d i t i s = i n a t- 1i o (i np n a i t o nwh 5 ) tp h a where ( y abbreviates f (y o )( e , to The ' Az ' and ' Ayi ' are binding occurrences, their scope is the entire rule and o fP boccurrences g s systematic ,tt: oy, o y renaming is allowed. This rule says, for a tree t = i n ) tp every node has been assigned two values (called attributes), that ,y n • • • o -(u ,t g n _ p (i the second attribute of t = A z , y) 1 ) 1 e o le the first attribute of t A n t ,)T i, n w h i — h gi i c x v a n ",l ( z , y ) e a -„. b b r e v f i o r r. 22 a -f t i o n a l s t1 ( h a l e ) FP
—
where z is the first attribute of t and y to such that i ibes aithe t hfunction e s (6) e[ Acl ( to, zn) dL e t ti be tree t in which every node has been assigned two a t t r i b u values t e (called attributes) in such a way that the first attribute o f t root of ti equals z and all subtrees satisfy the above of the i condition (for the appropriate p ). Then yield as result the N o w second attribute of the root of ts [We assumeAthat AG isl such that a IA1 exists; in the proof of Theorem (8) we shall further i s for tAl T h e specification implies narrow the choice d e fi n e (7) 1 A 1 z ) = f ( z , y) where i - [Al ( t d i The equation suggests to compute the second attribute of any (sub)tree by "attribute , s ( z , y ) )within . evaluation" that (sub)tree; hence this attribute is called synthesized. Similarly, the equation suggests that the first attribute of any (sub)tree is to be determined by the context, i.e., by attribute evaluation in the enclosing tree or by the environment in case the (sub)tree is the entire parse tree; hence this attribute is called inherited. As argued in assumptions (2, 3), the typing within rule p (5) is: z : X , y r l , : X xY" , and g i :Construction of the catamorphism Attribute grammar rule p (5) determines a function X O p x , = ( ) t z :: f(z,y) where i y a = th(gi(z, OD possibly n - 0 a n Y : - 4 Y r X -Or d " (vp / / p ) a X l : ( X Y ) F -t( X - O r ) l : T t ( X ---,Y). r Notice that the where-clause defines y by recursion; this corresponds to the potential u l circularity in the attribute evaluation when IA] z ) is computed as suggested by grammar e rule p (5). s t z t(8) Theorem 1A1(t,z) The o equality holds for all t E r assumption tg n o t (2)othat n any l ynode in a parse tree has precisely two attributes; these have not been fe onamed, r and have tbeen referred to as "the first" and "the second" attribute of the node, in the attribute evaluation rule (6). Also, we have assumed in (3) that all functions E t fT and gi. accept all kinds of values (though they may return a result in the summand h 1 for wrong inputs). T h i sThe theorem could have been formulated as IA1* ( 0 ) , where * e denotes the currying operation. ir s Proof p oBy induction s s oni the structure of C. Suppose t = i n d possibly n e- 0 so that inp : I -+ ( X -4 Y ) ; this covers the so-called base case.) We p b l e calculate (t t h a n o ,s 4 _ k eo 1 tr )o ( N o t i c e m 23 to h a t u i rn
(0 tz ____ c a s e assumption on t 0 4 (in„(t = eo v a l u a t i o n rule for catamorphisms , p.OM . . tot • • • t CO t22-1) z O , t = u n f o l d definition of Op n f(x,y) where i :: yi = (0) ti ( g _ (*) = i ni1d u c t i o n hypothesis (f(z,y) ) ) x) , y ) where i :: yi = 1 4 4 1 ( t = aizt t r i b u t e evaluation equation (7) ,tAl g (inp(tot • • -2 4-1 )2 z ) i — c a s e assumption on t (z,y))
1A1 (t, 2) .
In step (*) of the calculation it turns out that the circularity in the attribute evaluation and the mutual recursion in the definition of #p should be resolved in the same way. For example, if in the definition of 44, the y equations i a r e i ::d yie= fiOi(gi(z,y)), n e d then so must specification (6) of IM1 be understood. 0 t o b e t e 2 E xha m p les l e a s t fi Linearxparseetrees d Consider the following attribute grammar AG: p o i n t p : u ( A 2 3 , f y ) —0 titqz, Ay). s q : u ( A z , hz)—0 . o f tThis is a veryhsimple example e since there is no circularity at all. The parse trees are linear. Attribute evaluation of a tree t of depth n gives u n • h • g l z as synthesized attribute value of the root node when its inherited attribute value is set to z . I n other words, 1A1 (t , 2) — ( r • h • 912. Our construction of the previous section gives F'
:
1
1N+ 1
M O = ( A z :: fy where y — #(11(2,0)) s o (It 0,0 = ( A z :: hz) 8 0 i 0 4 , = == C hg 4 voOil- 8+ 0 0 f= ( ) g 0 - ' 1 ) .(F1 40 : N --, (X —• Y) 1 (OP — ( g o-t f) • • • • • (g o-, f) iPh = fn • h • gn
% where, in the 1 last line, t g e
is assumed to have depth n , i.e., t — (in, • • • • • in, • i n
)().
24
Binary parse trees T h e following attribute grammar works on binary (parse) trees with numbers at the tips. The attribute evaluation yields (as synthesized attribute value) a tree of the same shape as the input parse tree t , having all tip numbers equal to the minimum tip value in t . This function has been discussed by a number of people, e.g., Bird [11, Kuiper and Swierstra [3, 4], Fokkinga [21. We use * as join-operation of trees, [.) as tip-former, 1 as minimum-operation, and we let a, t vary over trees and k, m,n over numbers. The type of trees with numbers at the tips is denoted N * . Let us first present the attribute grammar in the conventional form, i.e., not yet simplified. p:
U(t) — d7: v()k, 4 v min, a-H-t) v ( k , )m, As) v(k, An, )it). for all numbers i v()sk,i, Ti : ( m , [le]) --0 i. A the (synthesized) first attribute value of v is specified to be equal In rule p we see that to the (inherited) m second attribute value, and in rules q and r we see (by induction) that , value of v is specified to be the minimum of the input parse tree. So, the second attribute eventually, in thei third attribute value of v the required tree is delivered. (Kuiper and Swierstra [3] needtten lines for this grammar.) Nonterminal u) has no inherited attribute; it may considered to have a "nullary" attribute of type 1 .. Rather than taking X = 1 + IsT for all inherited attributes, we give u a dummy inherited attribute of type N ; this avoids the introduction of many injections
and inspections. Also, we give u an extra synthesized attribute, and then tuple the two synthesized attributes everywhere giving values of type Y = DT xNog; in order to avoid many projections we use "parameter matching" at the binding lambdas. Thus we get the following attribute grammar that satisfies the assumptions (1, 2, 3): p•
u(Ak, (m, ID v ( m , Mm, ID. q: v(Alc,(min, sift)) ---t v(le, A(m,$)) v(k,A(n,t)). r v(Alc,(i, RD) — i 4 is The construction of the catamorphism gives now the following.
for all i
:
II-I-1-(Ei::1) T the carrier of the initial F -algebra 1 OAS) (Ak :: (m,t) where (n, t) = On) 0.16 (Ak :: (min, a-H-t) where (m, a) — Ok, ( f t , t) = skk) for all i On() Pik :: (i, RD) 6 0 O 4) (DI p .--,, INx1N3OF --+ (Ik1—p IsbctI*) = CO T' v —4 (N —, isixN*) O Notice that, as you can see from the equation for F , T g also trees in which a node has just one immediate constituent; each 1 nparse o t trees o is), n lbut y v these c o trees n tcan a not i result n s from parsing by the underlying context-free grammar. See also ( the b discussion i n just a following r y Theorem (8). v t r e e s i ( a s : 25 : 4 4 F
M IE E M 1
A MO S
• IMME ,
Wm • IM
,
E • I I
P
•
• • •
References [1] R.S. Bird. Using circular programs to eliminate multiple traversals of data. Acta Informatica, 21:239-250, 1984. [2] ACM. Fokkinga. Tupling of catamorphisms yields a catamorphism. May 1990. CIVI, Amsterdam. [3] M.F. Kuiper and S.D. Swierstra. Using attribute grammars to derive efficient functional programs. I n Computing Science in The Netherlands, pages 39-52, Stichting Informatica Onderzoek in Nederland, SION, GINVI, Amsterdam, November 1987. 141 M.F. Kuiper. Parallel Attribute Evaluation. PhD thesis, Utrecht University, 1989.
I
26