Knuth-1968-SemanticsCFL

March 16, 2018 | Author: Zhen Lao | Category: Metalogic, Applied Mathematics, Syntax (Logic), Style (Fiction), Language Mechanics


Comments



Description

Semantics of Context-Free Languagesby DONALD E. KNUTH California Institute of Technology ABSTRACT "Meaning" may be assigned to a string in a context-free language by defining "attributes" of the symbols in a derivation tree for that string. The attributes can be defined by functions associated with each production in the grammar. This paper examines the implications of this process when some of the attributes are "synthesized", i.e., defined solely in terms of attributes of the descendants of the corresponding nonterminal symbol, while other attributes are "inherited", i.e., defined in terms of attributes of the ancestors of the nonterminal symbol. An algorithm is given which detects when such semantic rules could possibly lead to circular definition of some attributes. An example is given of a simple programming language defined with both inherited and synthesized attributes, and the method of definition is compared to other techniques for formal specification of semantics which have appeared in the literature. A simple technique for specifying the "meaning" of languages defined by context-free grammars is introduced in Section 1 of this paper, and its basic mathematical properties are investigated in Sections 2 and 3. An example which indicates how the technique can be applied to the formal definition of programming languages is described in Section 4, and finally, Section 5 contains a somewhat biased comparison of the present method to other known techniques for semantic definition. The discussion in this paper is oriented primarily towards programming languages, but the same methods appear to be relevant also in the study of natural languages. 1. Introduction. Let us st/ppose that we want to give a precise definition of binary notation for numbers. This can be done in many ways, and in this section we want to consider a manner of definition which can be generalized so that the meaning of other notations can be expressed in the same way. One such way to define binary notation is to base a definition on 127 MATHEMATICAL SYSTEMS THEORY, Vol. 2, No. 2 Published by Springer-Verlag New York Inc. L. F u r t h e r m o r e . the nonterminal symbols are B. . Each list of bits L has a "length" l(L) which is an integer. KNUTH the following context-free grammar: B ---> 0 B ---> l L--> B (1. This can be d o n e by assigning attributes to the nonterminal symbols. in general we could ascribe any desired n u m b e r of attributes to each nonterminal symbol.1) in a step-bystep m a n n e r corresponding to this structure.128 DONALD E. the g r a m m a r assigns a certain tree structure to each binary n u m b e r .2) L L L B B 0 N L B 1 /\ L /\ B 1 I 1 /\ I I B I 0 I 1 B I 1 I It is natural to define the m e a n i n g of binary notation (1.) This grammar says in effect that a binary n u m b e r is a sequence of one or more O's and l's. 0. (Note that each L has two attributes. as follows: Each bit B has a "value" v(B) which is an integer. optionally followed by a radix point and a n o t h e r sequence o f one or more O's and l's. and 1. L (Here the terminal symbols a r e .1) L --+LB N---~ L N---~ L . the m e a n i n g o f the notation as a whole is built up f r o m meanings of each part. list of bits. Each list of bits L has a "value" v(L) which is an integer. and N. standing respectively for bit.1) may now be a u g m e n t e d so that semantic rules are .) T h e g r a m m a r (1. and n u m b e r . the string 1101 • 01 receives the following structure: /\ /\ L (1. and a binary n u m b e r is i n t e n d e d to be any string of terminal symbols which can be obtained f r o m N by application of the above productions. Each n u m b e r N has a "value" v(N) which is a rational number. for example. T h e leading "1" in "1101 • 01" really denotes 8. In a sense it is just coincidence that the two symbols look the same.) o B(v=. k2 ) . although according to (1. I(L1) = I(L2) + 1 v(N) = v(L) v ( N ) = v(LO + v(L2)/2 uL2~ (In the fourth and sixth rules subscripts have been used to distinguish between occurrences of like nonterminals.4)it is ascribed the value 1.3) L 1 ---} L2B N'-* L N ' .* L1 • L2 v(L1) = 2v(L2) + v(B).(VT. 1 B(~ ! = 1) 1 1 Thus "1101 • 01" means 13. This manner of defining semantics for context-free languages is essentially well known. the integer zero.Semantics of Context-Free Languages given for each rule of the syntax: B-~ 129 0 v(B) = o B---~ 1 v(B)= 1 v(L) = v(B). The structure (1. l = 2) y d. The semantic rules are phrased in terms of notations which are assumed to be already understood.) Here the semantic rules define all of the attributes of a nonterminal in terms of the atrributes of its immediate descendants.2) may be augmented by showing the attributes at each level: N(v _ 13. l(L) = 1 L---~ B (1. But there is an important way to extend this method.25) L(v/= 3.25 (in decimal notation). Notice for example that the symbol "0" in the semantic rule "v(B) = 0" is to be interpreted quite differently from the symbol "0" in the production "B ~ 0". so ultimately values are defined for each attribute. since it has already been used by several authors. Perhaps therefore it would be better to define the semantics in such a way that . the former denotes a mathematical concept. Suppose for example that we want to define the semantics of binary notation in a different way corresponding more closely to the manner in which we usually think of the notation. and it is this extension which will be of primary interest to us. while the latter denotes a written character which has a certain elliptical shape.(y=. O) 1 i =0) . We could have the following attributes: Each B has a "value" v(B) which is a rational n u m b e r . v ( N ) and also the inherited attributes s(B) and s(L). T h e evaluated structure c o r r e s p o n d i n g to the string 1101 • 01 is . H e r e we are using both synthesized attributes (which are based on the attributes o f the descendants o f the nonterminal symbol) and inherited attributes (which are based on the attributes o f the ancestors). v ( N ) = v ( L . t h e n s(B) is defined to have this same value. l(L) = 1 s(B) = s(L~).5) L1 --* L2B v(B) = 0 v(B) = 2 * ~ v(L) = v(B). l(L~) = l(L~) + 1 Semantic rules s(L) = 0 s(LO = O.5) contains the synthesized attributes v(B). so the evaluation involves going in both directions. ) + v(L2). Each N has a "value" v ( N ) which is a rational number. Synthesized attributes are evaluated f r o m the b o t t o m u p in the tree structure. v(L1) = v(L~) + v(B). Each L has a "scale" s(L) which is an integer. Each L has a "value" v(L) which is a rational n u m b e r . Each B has a "scale" s(B) which is an integer. G r a m m a r (1. while in (1. s(L2) = s(L1) + 1.130 DONALD E. KNUTH positional characteristics play a role. Each L has a "length" l(L) which is an integer. while inherited attributes are evaluated f r o m the top down. s(L2) = --/(L2) ( H e r e the semantic rules are listed using the convention that the righth a n d side o f each equation is the definition o f the left-hand side. "s(B) = s(L)" says that s(L) is to be evaluated first.) T h e i m p o r t a n t feature o f g r a m m a r (1.3) all attributes were defined when the n o n t e r m i n a l a p p e a r e d on the left side. s(B) = s(L). N --~ L N ~ L 1 • L2 v ( N ) = v(L). v(L).5) is that some o f the attributes are defined for nonterminals which a p p e a r on the right side o f the corres p o n d i n g production. thus. l(L). T h e s e attributes can be defined as follows: Syntactic rules B---~O B--~I L--*B (1. s ~ 1) 1 LIV T 8.5) do not amount to a circular definition. l= 1.2 ) I L(v = 12. Suppose we have a context-free grammar f~ = (V. because the attributes are not evaluated in a single direction. It is not always obvious when semantic rules such as those in (1. S.25) ~ ~/ i 1 v = 12. which is not really relevant. N C V is the set of nonterminal symbols. an algorithm which tests for circularity appears later in this paper.I ) 0 1 Here it can be noted that the "length" attributes of the L's to the right of the radix point must be evaluated from the bottom up before the "scale" attributes can be evaluated (from the top down) and finally the "value" attributes (from the bottom up).6) iN(v= L(v = 13. s = . Grammar (1. s = . l = 3. l = 4. s. s= 2~) 0 B(v ~ 8.) Our interest in g r a m m a r (1. I= I. N.5) is probably not the "best possible" grammar for binary notation. s= 3~)B(v T 4. Let us now put the ideas of synthesized and inherited attributes into a more precise and more general setting. where V is the (finite) vocabulary of terminal and nonterminal symbols. Situations which involve a mixture of inherited and synthesized attributes are essentially the same as the cases which have been most difficult to handle in previous formulations of semantic rules. s = O) \ L(v = O. 6~). s = 2) B(v = O. which appears on the right-hand side of no production rule. but rather that it shows an interaction between inherited and synthesized attributes. 2. would be unnecessary. and ~ is the set of production rules. s = l~ B(v = 1. 1 = 2 . S • N is the "start symbol". B(vTO.3). Formal properties.25. but it does seem to correspond better to our intuition than grammar (1. then the "length" attribute. Although binary notation can be formulated using nothing but synthesized attributes. there are many languages for which such a restriction leads to a very awkward and unnatural definition of semantics. Semantic rules are added to fg in the following manner: To each . s = . s = 3) 1 \.= 0) li.Semantics of Context-Free Languages 131 (1.5) is not that it is an ideal definition of binary notation. The importance of inherited attributes is that they arise naturally in practice and that they are "dual" to synthesized attributes in a straightforward manner.1 ) B(v= \ . (A grammar which agrees more exactly with our conventional understanding of binary notation could be based on a different set of production rules which would assign another structure to the string of bits at the right of the radix point. KNUTH symbol X • V we associate a finite set `4(X) o f attributes. Xp0 • N.p .4 ~(Xpj) i f j > 0. (1. (1. N --~ L • L } ). A. Ao(L) = {v. 0 <~j ~< np. `4(X) is p a r t i t i o n e d into two disjoint sets. w h e r e n4 = 2. Xpnp into the value o f s o m e attribute o f Xpj. similarly we r e q u i r e Ao(X) to be e m p t y if X is a t e r m i n a l symbol. Xpn p (cf. Each attribute a in A(X) has a (possibly infinite) set o f possible values V~. a n d it is t h e mappingf40~(x. A typical p r o d u c t i o n rule is the f o u r t h p r o d u c t i o n X40 "-~ X41X42.5) is the g r a m m a r ~ = ({0. A typical semantic rule c o r r e s p o n d i n g to this p r o d u c t i o n isf40~. f o r s o m e t = t(p. v) = 1. X40 = X41 = L. For any derivation o f a terminal string t f r o m S by a sequence o f p r o d u c t i o n s . For e x a m p l e . Xpl. AI(B) = {s}. a n d Xpj • V for 1 ~<j ~< np.f r e e language. in the following way. which defines v(X4o) in t e r m s o f o t h e r attributes. 0. 1. or) is an attribute o f s o m e Xpki. in the latter case the n o d e has np i m m e d i a t e descendants. v) = 2. the start symbol S has no i n h e r i t e d attributes).2). L ---) B. for s o m e p. a n d let the p-th p r o d u c t i o n be (2. L. 1 <~ i <~ t. We r e q u i r e AI(S) to be e m p t y (i.(L) = {s}. I n o t h e r words.2)).5). I f o~ • Ao(X) t h e n X = Xp0 f o r s o m e p. L ~ LB. w h e r e in either case the tree in the n e i g h b o r h o o d o f this n o d e has the f o r m (2. X42 = B. in this casef40~ m a p s V~ x V~ into V~. k2(4.. or with a n o n t e r m i n a l symbol Xp0 corres p o n d i n g to an application o f the p-th p r o d u c t i o n . x V~2 x • • • x Vat into V~. . y) = x + y . {B. k1(4. (This is the rule "v(L1) = v(L2) + v(B)" o f (1. in t e r m s o f the r a t h e r c u m b e r s o m e notation o f the p r e c e d i n g parag r a p h we have t(4.j. v) = 2. v) = v. . • • • . w h e r e each o~ = ai(p.. 0. T h e attribute ~ is d e f i n e d to . 1 ~<j ~< np. construct the derivation tree in the usual way: T h e root o f this tree is S. f o r 0 ~< k~ ----k~(p. L. B ~ 1. 0.. = VI = {integers}. N } . (2. 0. v) = a2(4. N --) L. a n d each n o d e is labeled either with a t e r m i n a l symbol. a n d Ao(x) = A~(x) = ~ for x • {0. V. w h e r e np t> 0.2) Xpl Xp2 ° . a n d ct • Ao(Xpj) i f j = 0. Let ~ consist o f m productions. oq(4. Each such function is a m a p p i n g o f V~. each semantic rule m a p s values o f certain attributes o f Xp0. f r o m which o n e value will be selected (by m e a n s o f the semantic rules) for each a p p e a r a n c e o f X in a derivation tree. Ao(N) = { v ) .j. N. while if o~ • A 6 X ) t h e n X = Xp~ f o r s o m e j a n d p. T h e attribute value sets are Vv = {rational n u m b e r s } .}. o~) <~ np.j. ot •. 1.132 DONALDE. o0 >i O.1) Xpo ~ XpaXp2 • • • Xp. 0. l}. B. {B ~ O. N } .e. T h e semantic rules a r e functionsfpj~ d e f i n e d for all 1 ~< p ~< m. Now let X be the label o f a n o d e o f the tree a n d let c~ e A ( X ) b e an attribute o f X.) T h e semantic rules m a y be used to assign a " m e a n i n g " to strings o f the c o n t e x t . the synthesized attributes Ao(X) a n d the i n h e r i t e d attributes A~(X). A I ( N ) =~. T h e attributes are Ao(B) = {v}. in the corresponding semantic rule (2. 1).o) [s(X. 1). p. L). (1.2) we have s ( N ) = {(1. suppose we ascribe two inherited attributes l ("location") and t ("tree") to each symbol except S in a context-free g r a m m a r . (1.Semantics of Context-Free Languages have the value v at this node if. Xpo)[ Xpo ~ S } U jl~J= "[s(Xpj)l X p j ~ N } : Thus. (2. • " • .o) s(X.1. where l is a node location and X is a symbol of the g r a m m a r denoting the label of the node at location I. B).2. B).1. and then the defined attributes at the root of the tree constitute the "meaning" corresponding to the derivation tree (cf. (1. L). while 1 is the location . 0). • • • . vt at the respective nodes labeled Xpkl. (1.1. .2. . (1..1.1.1. Let us note that this m e t h o d of semantic definition is as powerful as any conceivable m e t h o d could be.S . (1.2. T h e semantic rules. 310]). .2. For example.1. • • • .1.o) = {(l(Xpo). It is natural to require that the semantic rules are formulated in such a way that all attributes can always be defined at all nodes. (1. np (2.1. (1. for example. Let us say the semantic rules are well defined if this condition holds.~: Val X • • • X Vat ---> Va 133 all of the attributes al. (1. (1. X ) . L).1. in the sense that the value of any attribute of any node of a derivation tree may d e p e n d in any desired way on the entire tree.1. B).4) t(Xpj) = Jt(X. L). Since there are in general infinitely many derivation trees.2.1.4) define the attribute t on all nodes (except the root) to be the set representing the entire derivation tree. if Xpo ~ S . • ) (3.1. and v = fpj. This clearly contains all the information of the entire derivation tree. Here 1 ranges over finite sequences of positive integers {a~ • a2 . This process of attribute definition is to be applied t h r o u g h o u t the tree until no more attribute values can be defined.1. for each production (2. (3. T h e semantic rules (2. B). in the tree (1.1. L).~(vl. at have previously been defined to have the respective values v l . Xpk t. if X~o -. 1)}. ak} which specify the location of tree nodes in a familiar index or "Dewey decimal" notation (see [8.1.2.1.1.1. 1). (3. ifXpo = S.2. An algorithm for testing this condition is presented in Section 3.1. and one synthesized attribute s Csubtree") t o each nonterminal symbol. (1.2. vt).1.1.1. t and s consist of sets of o r d e r e d pairs (l. it is important to be able to decide if a given g r a m m a r has well defined semantic rules or not. ' ' ' .1). are: l(Xpj) = II(Xpo) " j tj if Xpo ~ S .1.2. L). .1.1.3) fpj.1.1. (1.6)). (3. in any conceivable derivation tree. B).1.1. 0). (3. B). (3. then D(3-) is the directed graph .5). if 3. but allowed to have any symbol of V (not only the start symbol S) as the label of the root. since the synthesized attribute w defined by the rule np (2. We may assume that the grammar contains no "useless" productions. The ability to let the whole tree influence the attributes of each node of the tree often leads to rules of semantics which are much simpler and which correspond to the way in which we actually understand the meanings involved. a) as vertices. a2) if and only if the semantic rule for the value of attribute a2 depends directly on the value of attribute a~.5) w(Xpo) = {(0. Let 3. al) to (X2.and a is an attribute of the symbol which is the label of node X. we want to know when the semantic rules will always lead to definitions of all attributes at all nodes of all derivation trees. where X is a node of J . The arcs of D(~--) go from (XI. Now let us consider an algorithm which determines whether or not a collection of semantic rules. Testing for circularity. But this statement is very misleading. and therefore the method is inherently no more powerful than a method which uses no inherited attributes. For example. Similarly we can show that synthesized attributes alone are sufficient to define the meaning associated with any derivation tree. T h e n we can define a directed graph D(~--) corresponding to 3. is well defined. KNUTH of that node.by taking the ordered pairs (X. since such a function is f(t. that each production of ~ appears in the derivation of at least one terminal string. Any semantic rules definable by the method of this section can be considered to be a function of this attribute w.be any derivation tree obtainable in the grammar. It is therefore evident that any conceivable function of the derivation tree can be an attribute of any node.e. as described in the previous section. Xp0)} U U {(j" j=l a.134 DONALD E.x)l (a.X) ~w(Xvj).is the tree (1. having only terminal symbols as labels of its terminal nodes.XpjeN} evaluated at the root specifies the entire tree.. in other words. 3. l) for somef. i.2) and if the semantic rules are given by (1. since semantic rules which do not use inherited attributes are often considerably more complicated (and more difficult to understand and to manipulate) than semantic rules which allow both kinds of attributes. • • • . "Are the semantic rules well-defined?" reduces to the problem.3) /X. 258] ). "Do the directed graphs D(~--) contain any oriented cycles?" Each directed graph D(T) may be regarded as the superposition of smaller directed graphs Dp corresponding to each of the productions Xp0 --* Xp~ • • • Xpnp of the grammar. oO. the fact that the grammar contains no useless productions implies that there is an oriented cycle in some D ( J ' ) in which the root of Y.6).l(L)*s(L) Dn: .2) o4: "*v(B) #s(B) ov(L2) *'I(L2) ds(L2) "*v(B)-'°s(B) Ds:]Iv(N) • v(L) .) v(Lz)*ttL2~*s(L2) The directed graph (3. according to the definition. for I ~ j ~ np. a ~ A(Xpj). ot E AI(Xpj) if j > 0. the directed graph Dp has vertices (Xp~. . the vertices of D(3-) are the attribute values which must be determined. ~ .) s(L. this ~" is a derivation tree of the language for which it is impossible to evaluate all of the attributes. For if there are no oriented cycles. (1. or). 1 <~i <<-t(p.j. In the former case we will say ~-./. ~-" has the form (3. D(~--) has no arcs. there is a well-known procedure which assigns values to each attribute (see [8.is labeled with a nonterminal symbol. for 0 <~j <~rip.o\ Y--1 "'" 3-. D(~--) is obtained in this case from Dp. where ~--j is a derivation tree with Xp~ as the label of the root. 1 ~< p ~< m. oti) to (Xpj.is a derivation tree oftypep.has the label S.1) is obtained by "pasting together" various subgraphs having these forms. D(3".) It is clear that the semantic rules are well defined if and only if no directed graph D(Y') contains an oriented cycle. ° (N) *v(L1) . a).5) correspond to six directed graphs. In other words. (Cf.is a derivation tree of type 0. p. and arcs from (Xpki. namely DI: °v(B) °s(B) D2: "v(~B) "s(B) o3: (3.) . In general if 9. For example the six productions of grammar (1. D(J-1).j. And if there is an oriented cycle in some D ( J . and in the latter case we will say ~-. a ~ Ao(Xpj) i f j = 0. or). and the arcs specify the dependency relations which imply that certain attribute values must be computed before others. oti = ai(p.t(L. Dp reflects the dependencies of all the semantic rules associated with the p-th production.p) by identifying the vertices for attributes o f Xpj with the corresponding vertices for the attributes of the root of 3-j in D(~--j).p for some p. .has a terminal symbol as the label of the root. ki = ki(p. if the root of 9-. a) for 0 <~j <~np.j. In the notation of Section 2. 1 <~j <~ np.Semantics of Context-Free Languages 135 In other words. Therefore the problem. Now add f u r t h e r arcs to D'I. a') in the directed g r a p h (3. a n d with an arc f r o m a to a ' if and only if t h e r e is an o r i e n t e d path f r o m (Xp0. X¢o)0 = Xpj. there should be an arc f r o m a to a ' in D~.e.. For example. G2] is T h e following algorithm may now be used: " F o r 1 ~ p ~< m let D~ be the directed g r a p h with vertices A(Xp0)..2). t h e n let (3. = • D~ = • After the above algorithm terminates. or) to (Xp¢.• • D'5 = • D'6 = • a n d adds arcs until finally we have V°$ ~ 8 V l $ D'.ap)]. if and only if there is an arc from ot to or' in D~. since only finitely m a n y arcs are possible in all.5). In the case o f g r a m m a r (1. • • • . where if" is a derivation tree of type p with root X. Let p be the n u m b e r o f a p r o d u c t i o n .. or') in some D(ff-).) exists..~ • a n d if D 4 is the directed g r a p h a p p e a r i n g in (3. we can prove that there is an oriented path from (X. a) to (Xp0. For the construction does not add any arc f r o m a to a' unless such a D ( 3 .° V O l O S O v • v D'4 ~-.. Dm by the following p r o c e d u r e until no f u r t h e r arcs can be a d d e d : Choose an integer p. a) to (Xp0. with 1 ~< p ~< m. o n e f u r t h e r concept is useful. a') in D v./o D. = • • I s D~' = U" v D~ = -~.~. the algorithm could readily be e x t e n d e d so that it would in fact print out an . Grip] be the directed g r a p h obtained f r o m D v by a d d i n g an arc f r o m (Xpj. a n d for 1 ~<j ~< np let q(j) = 0 ifXp¢ is terminal." It is clear that this process must ultimately t e r m i n a t e with n o m o r e arcs a d d e d . this algorithm begins with V $ V 8 t D3 = '----oo D1 v l s t D2 = L . D. a ' ) w h e n e v e r t h e r e is an arc f r o m a to a ' in Gj. or) to (X.136 DONALD E. KNUTH In o r d e r to test w h e t h e r D(ff-) contains o r i e n t e d cycles. a n d for 1 <<. G2 = T. if we have vl s vs GI = e ~ • .5) D v[D~.4) D p [ G . the attributes o f Xpj. <<-np j suppose G~ is any directed g r a p h whose vertices are a subset of/I(Xpj). v 94- t __ o~. • • • . Let D~ be the e m p t y directed g r a p h having no vertices... • • • . i. o r choose an integer q(j) such that Xp~ is the left-hand side o f the q(j)-th production. t h e n D4[G1. T h e n if t h e r e is an orientdd path f r o m (Xpo. 5).for each arc in Di.1) . a') in this directed graph. (4.3) and D ( J ' ) is "pasted together" f r o m Dp. go to carry. suppose Y" is a derivation tree with root X. 4.5) when ~--~ is of the type q(~). hence there is an arc f r o m a to a ' in Dp. go to test}. T h e above algorithm now affords a solution to the problem posed in this section: t T H E O R E M . for which D(Y-) contains an oriented path f r o m (X.must be of the f o r m (3. zero. D(3-%).5) contains an oriented cycle. realign: move right one square. the remarks just m a d e prove that some D(3-) contains an oriented cycle. Now let us consider an example of how the above techniques of semantic definition can be applied to prog r a m m i n g languages. the machine can read or write characters f r o m a finite alphabet on the tape in the square which is currently being scanned. and therefore we may argue as above that any arcs o f the cycle which are within D (3"1). print "point". if ~-.p)]. D'~. q(1). for example. • • • . Proof If (3. D~. q(np) as specified in the above algorithm. • • • . any arcs of the ass u m e d path which appear in D(~--1). D(~"I). carry: m o v e left one square. such that D ( J ' ) contains an oriented cycle. and it can move the scanning position to the left or right. Semantic rules added to a grammar as described in Section 2 are well defined if and only if none of the directed graphs (3. By induction and the fact that no arcs r u n f r o m D(~Y'j) to D(J'~. if the tape symbol is "zero" then go to realign. where Y. • • • . For simplicity let us study a formal definition o f a little language that describes T u r i n g machine programs.. contains an oriented cycle. • • • . point.3). where Y'j is o f type q(j). Conversely. Conversely. D (9--%) may be replaced by arcs of (3. D(Sr%). for any admissible choice of p. print "one".must be of the f o r m (3. 3. the oriented cycle involves at least one arc of Dp. . • • • . By the minimality of 3-. assuming that the square just to the right of the n u m b e r is to be scanned at the beginning a n d end o f the program: tape alphabet is blank. then ~-. A simple programming language. one. a').is of type p: Since D(~J") contains at least one arc. • • • . D(3-1).Semantics of Context-Free Languages 137 appropriate derivation tree ~. test: if the tape symbol is "one" then {print "zero". 1 ~<j ~<np. T h e following program. and we have an oriented path f r o m (Xp0. adds unity to an integer expressed in binary notation a n d prints a radix point at the right of this n u m b e r .is a tree with the fewest possible nodes. • • • . D(3-%) may be replaced by appropriate arcs in Dp[D~t).) f o r j # j * . we can prove by induction on the n u m ber o f nodes of Y" that there is an arc f r o m a to a ' i n D . A T u r i n g machine (in the classical sense) processes an infinite tape which may be t h o u g h t of as divided into squares. or) to (X. and D ( J ' ) is "pasted together" f r o m Dp. and go into state q'. rather as an example of the features of the simple language considered in this section. although of course this is not necessary. as follows: At any moment of the computation there is a "current state" q ~ Q and an integer-valued "tape position" p. a3. the computation proceeds by replacing the value of ap by s'. (The computation might not terminate. i. initially q . If q = q=. (1) The semantic rule "include x in B" associated with a production will mean that x is to be a member of set B. The value of B will be the set of all x for which such a semantic rule has appeared corresponding to each appearance of the production in the derivation tree.e. a-3. ap) = (s'. let us call the language Turingol. a-2. and if 6(q. ao. +1 } × Q. a2.{q~o}) × X into X × {-1. (This rule may be regarded as an abbreviation for the semantic rule (4. • " " of elements of ~.. move k spaces to the right (meaning one space to the left if k = -1). a Turing machine program defines a computation on any "initial tape contents". k.q0 a n d p --. where B is an attribute of the start symbol S of the grammar. an "initial state" qo E Q. then by replacing p by p + k and q by q'. O. we wish to define the Turing machine program corresponding to any given Turingol program (and at the same time to define the syntax of Turingol). If 8(q. it will print symbol s'. Ifq ~ q=. s) = (s'. and a "transition function" 6 which maps (Q . For this purpose it is convenient to introduce a few abbreviation conventions. for program (4. KNUTH (It is hoped that the reader will find this programming language sufficiently self-explanatory that he understands it before any formal definition of the language is given. k.3) % B(Xpo) = U B(Xpj) U {x] "include x in B" is associated J=~ with the p-th production} . a "final state" q® ~ Q. a-l.138 DONALD E. the computation terminates. if the machine is in state q and scanning symbol s. q'). Any well-formed Turingol program defines a program for a T u r i n g machine. al.) Now that we have a precise definition of T u r i n g machine programs. The above program is not intended as an example of good programming. More formally.2) " " " .0.1) this happens if and only ifaj ="one" for allj < 0. q') we may say informally that.) Since every programming language must have a name. on any doubly infinite sequence (4. a set Z of "symbols". let us say a Turing machine program consists of a set Q of "states". F u r t h e r m o r e . . L (list of statements). as its value. If two rules occur defining f(x) for the same value of x... with the proviso that f ( x ) m a y only appear when f has been defined at x.. an abstract element which for each evaluation of "newsymbol" is different from the abstract element produced by other evaluations of newsymbol. by making use of the l attributes of (2.. (2). as in (1). y) i n f " . These rules clearly make B(S) the desired set. The effect of using these conventions is to reduce the number of attributes that are explicitly mentioned and to avoid unnecessarily long rules. Nonterminal symbols: P (program). and B(x) the empty set for each terminal symbol.g. (This type of rule is important.) (3) The function "newsymbol" appearing in any semantic rule will have. Terminal symbols: a b c d e f g h i j k lFm n o p q r s t u v w x y z . to ensure that there is agreement between the declaration and use of identifiers. Now it is a simple matter to present a formal definition of the syntax and semantics of Turingol. O (orientation).) We have observed that conventions (1). i f f is regarded as a set of ordered pairs. T h e function n e w s y m b o l serves as a convenient source of "raw material" for constructing sets. But they are of fairly wide utility. D (declaration).3) which completely specify this "define f(x) = y" convention are readily constructed and left to the reader. The rule may essentially be thought of as "include (x. { } tape alphabet is print go to if the symbol then m o v e left right one square . f may be used as a function in any other semantic rules. We may regard "well-formed or malformed" as an attribute of S. I (identifier).) (2) The semantic rule "definef(x) = y " associated with a production will mean that y is to be the value of the f u n c t i o n f evaluated at x. any derivation tree which calls for an undefined value o f f ( x ) is malformed. so they are not "primitives" for semantics. In the example below this convention implies that programs are malformed if the same identifier is used twice as a label or if a go to statement specifies an identifier which is not a statement label. for example. appropriate semantic rules analogous to (4. additional checks for malformedness are also included. A (alphabetic character). e.Semantics of Context-Free Languages 139 added to each production. . (3) can be replaced by other constructions of semantic rules which do not use these conventions.3) which has a different value at each node of a tree. w h e r e f is an attribute of the start symbol S of the grammar. (This convention can readily be expressed in terms of other semantic rules. S (statement). so they may be regarded as fundamental aspects of the techniques for semantic definition presented in this paper. since they correspond to concepts which are often needed. this is an error condition. with a set B added as a synthesized attribute of each nonterminal symbol. and any derivation tree which allows this condition to occur may be said to be malformed. : . 4). since the action of the statement is to change control to another place. . the state which is normally reached after the statement is executed.2). and also makes the Turing machine into a kind of "one-plus-one-address computer" in which each instruction specifies the location (state) of the next instruction. intoX x { . 0 . and shifting in each instruction. This example would be somewhat simpler if we had used a less standard definition of Turing machine instructions. not "dynamically" during a run of the program.140 DONALD E. In Table 1. follow (S) is the state which "follows" the statement.1 . In the case of a "go statement". printing. the program does not transfer to follow (S). Notice that two states correspond to each statement S: start (S) is the state corresponding to the first instruction of the statement (if any). label symbol follow d text start +1 String of letter's Element of Q Productions and semantics: See Table 1. follow (S) may be said to follow statement S "statically" or "textually". Similarly. both start (S) and follow (S) could be synthesized attributes. The definition we have used requires reading. although a less efficient program would be obtained for null statements (see Rule 4. but at the expense of additional instructions in the Turing machine program for statement lists (Rule 6. + 1 } XQ Function from strings of letters into elements of Q Function from strings of letters into elements of S Element of Q Purpose States of the program Symbols of the program Initial state Final state Transition function State table for statement labels Symbol table to tape symbols State immediately following statement or list of statements Direction Identifier State at the beginning of a statement or list of statements (an inherited attribute). it is possible to give similar semantic rules in which follow (S) is inherited. and it is an inherited attribute of S. however. KNUTH Start symbol: p Attributes: Name of attribute Q qo q= Type of value Set Set Element of Q Element of Q Function from (Q-q=) × ~i. follow (S) is a synthesized attribute. 1.1 S --> {L} L --->S 6.F r e e L a n g u a g e s Table 141 1. if t h e t a p e s y m b o l is "nmr/lyn" then p r i n t "]ayne" . (similarly for all letters) text (A) = z. d e f i n e 8 (start (S).Semantics o f C o n t e x t .2 LI-~ L2. Syntactic Rule a 1. marilyn tape a l p h a b e t is marilyn d e f i n e s y m b o l (text (I)) = newsymbol.. m text (I) = text (I) text (A).L.1 S --~ $1 --> i f t h e t a p e s y m b o l is "I" t h e n $2 Labeled statement 5.2 Go statement 4. 3.3 6. follow (S) = n e w s y m b o l . i n c l u d e start ($2) in Q.1 .. define 8 (start (SO.4 5. 0. d e f i n e symbol (text ( / ) ) = newsymbol. left o n e square start (Sz) = start ($1).s) = (s. i n c l u d e follow (.2 S --* m o v e 0 o n e square move left one square 4.4 D ---> t a p e a l p h a b e t is I text (A) = a. follow (L2) = n e w s y m b o l . follow (LO = follow (S). . p r i n t "'jayne ". start (L) = qo.1 A ~ a .26 A --*z 2. . z text (1) = text (A). start (L2) = start (LI). {print'Jayne". s) = (symbol (text (I)). follow (SO = follow ($2). . start ($2)) for s = symbol (text (I)). Example Semantic Rules Description Letters No. follow (S)) for all s e X. S Program 7 P-*D. start (S) = follow (Lz). d e f i n e label (text (I)) = boston: m o v e start (S~). d e f i n e 8 (start (S).s) = (s. start (L) = start (S). Null s t a t e m e n t Conditional statement 4.2 3. go to boston } follow (S) = follow (L). follow (S) = n e w s y m b o l .2.3 O "-' left O --* f i g h t S ~ go to I left fight go to boston i n c l u d e symbol (text (1)) in S. d(O) = . . O. O.S) in Q. follow (S) = n e w s y m b o l . I Print s t a t e m e n t 4. follow ($2)) for all s ~ X symbol (text (1)). q® = follow (L).1 4. . birgitta. follow (S) = start (S). birgitta p r i n t 'jayne" Move s t a t e m e n t 4. go to boston i n c l u d e follow (Lz) in Q. i n c l u d e follow (S) in Q. jayne. is marilyn. i n c l u d e follow (S) in Q. . . follow (S)) for a l l s e X. print"jayne". start ($2) = n e w s y m b o l .s)= (s.2. define 8 (start (S). s) = (s.1 2. define 8 (start (SO. start (S) = start (L). . i n c l u d e symbol (text (I)) in S.1 S --* p r i n t "I" tape a l p h a b e t is marilyn. . tape alphabet q0 = n e w s y m b o l .2 S 1 --> I : Sz Compound statement List o f statements 5. p r i n t "'jayne" follow (L) = follow (S). d(O). O. follow ($1) = follow ($2). i n c l u d e q0 i n Q.1 1 --->A Identifiers Declarations 1 --> 1. jayne. label (text (I)) for all s e X.2 D --->D. d(O) = +1. afterwards the program will continue with a new instruction. Discussion.start (S)) would be the number of instructions "compiled" for statement S. shows that the agreement between declaration and use of symbol s . But in practice. notably McClure [ 14]. In other words. "Block structure" is another common aspect of programming languages whose definition is greatly facilitated by the use of inherited attributes. at least those known to the author at the time of writing. leads to important simplifications. for example. the tape symbol on the currently scanned square will be replaced by the symbol denoted by I. The method of Section 2 shows how both inherited and synthesized attributes can be treated formally. The Definition 4. Then (follow (S) -. with an inherited "first (S)" and a synthesized "follow (S)" attribute. The idea of defining semantics by associating synthesized attributes with each nonterminal symbol. its "translation". 7]. inherited attributes are useful when part of the meaning of some construction is determined by the context in which that construction appears. whenever this statement is executed. lends itself readily also to computers or automata in which the (n + 1)st instruction normally is performed after the n-th. are de Bakker's definition of ALGOL60 by means of a growing Markovian .1 of a print statement. The definition of Turingol. which is defined (by other rules) to be the instruction following this statement. Originally each nonterminal symbol was given exactly one attribute. KNUTH The method of defining semantic rules in this example. and Section 3 shows that it is possible to rule out problems of circularity (which are potential sources of difficulty when both inherited and synthesized attributes are mixed). This means that. This definition of Turingol seems to approach the desirable goal of stating almost exactly the same things which would appear in an informal programmer's manual explaining the language. and associating corresponding semantic rules with each production. may be easily treated. this definition perhaps corresponds to the way we actually understand the language in our minds. and the association of labels to statements. the inclusion of inherited attributes as well as synthesized attributes. might be freely rendered in English as follows: "A statement may have the form print "I" where I is an identifier. as described in this paper. The principal contributions to formal semantic definition of programming languages. This idea was applied by Irons and later authors. in the design of "syntax-directed compilers" which translate programming languages into machine instructions. is due to Irons [6. As we have observed in Section 2.142 DONALD E." 5. synthesized attributes alone are (in principle) sufficient to define any function of a derivation tree. regardless of what symbol was being scanned. except that the description is completely formal and unambiguous. for example. In general. 5. at each stage of this machine's computations we are to execute the last applicable instruction. . by means of semantic rules applied as a program is parsed [ 16].L. Although the other authors cited above do not make use of such an intricately interwoven definition as de Bakker's. additional instructions are added to the list by the actions of the machine. Landin. does not influence the definition of print statements in a substantial way.out2.O. or to give formal proofs of its important properties.1. Yet even though Wirth and Weber tested their definition by means of extensive computer simulation.4. Landin's definition of ALGOL60 by means of the X-calculus [9. This localization and partitioning of the semantic rules tends to make the definition easier to understand and more concise. if false t h e n go to L else L. 10. for example. 11] (see also B6hm [2. consider the formal definition of Euler given by Wirth and Weber [16. this is a concise definition of a very sophisticated language.2. 4.3. and the IBM Vienna Laboratory's definition of PL/I [15] based on the work of McCarthy.4 ~-.Semantics of Context-Free Languages 143 algorithm [ 1] . furthermore. By contrast. McCarthy's definition of MicrO-ALGOLby means of recursive functions applied to the program and to "state vectors" [12] (see also McCarthy and Painter [13]). if false t h e n go to L else if A < 2 t h e n go to L else out 3. 4.4.3 could be deleted and we would obtain a valid definition of another language. For example. 3]).A ~-A+ 1. The following Euler program is syntactically and semantically well-formed. It is clearly very difficult for a reader to understand the workings of such a machine. and abstract machines defined by Elgot [4. L e n d ± . etc. the above definition of Turingol defines each construction of the language only in terms of its "immediate environment". minimizing to a large extent the interconnections between the definitions of different parts of the language. it is quite probable that their language contains some features which would surprise its authors. any of Rules 4. the relatively complex interdependence is still present. who defines a machine having approximately 800 instructions. so we cannot verify that instruction number 100 will be performed until we can prove to ourselves that the 700 subsequent instructions are inapplicable. and so it is certainly one of the most successful formal definitions ever devised. Wirth and Weber's definition of Euler. The most striking difference between the previous methods and the definition of Turingol in Table 1 is that the other definitions are processes which are defined on programs as a whole in a rather intricate manner.1. although the label L is never followed by a colon: ± b e g i n label L.. 4. analogous to Markov algorithms but somewhat more complicated. it may be said that a person must understand an entire compiler for the language before he can understand the definition of the language. out 1. 94-98]. pp. 5]. The definition of compound statements and go statements. This difficulty is most pronounced in the work of de Bakker. 5. n e w . but it still is semantically unambiguous since the attribute v(N) has the same value over all derivation trees. if we were to change production 5."Random-access. C. Comm. 160-179. R. 1966. [5] C. they are less likely to occur when the methods of Section 4 are employed. When a syntax is ambiguous. Amsterdam. B6HM. pp. [6 ] EDGART. .ACM 4 (1961). they need not even be tied down to specific forms of the syntax: All that the semantic rules depend on is the name of the nonterminal symbol on the left of a production and the names of the nonterminals on the right.3). pp. and the order in which the nonterminals appear on the right-hand side of any production. and (although the details have not of course been worked out) it also appears that ALGOL 60. the semantic rules give us one "meaning" for each derivation tree. ROBINSON. w.Formal definition ofprogramming languages. Math Cent. Caianiello). The CUCH as a formal and description language. 266-294. IFIP Working Conf. But of course the author cannot judge these things impartially. On the other hand. with an application to the definition of ALGOL 60. [2] C. 1966. FormalLanguage Description Languages for Computer Programming. "Machine species and their computation languages. Notice that semantic rules as given in this paper do not depend on any particular form of syntactic analysis.v(L2). stored program machines.. [4] C. in the sense that some strings of the language have more than one derivation tree. Tracts 16. 365-399. 1RONS.A syntax directed compiler for ALGOL 00. 1966. are immaterial as far as the semantic rules are concerned. Vienna (1964)."J. "Introduction to the OUCH. 2. T h e n the grammar becomes syntactically ambiguous. IFIP Working Conf. In fact.. l(Li) =/(L2) + 1 are added to grammar (1. Euler. C. 1967. the method of semantics considered here blends well with McCarthy's idea [12. and PL/I can be defined using the methods of Section 4 in a manner which has advantages over the definitions previously given. ELGOT. Proc. and more experience is needed before these claims can be substantiated. pp.2 of Turingol from S ~ I: S to S ~ S: I. DZ BAKKER. For example. 13] of "abstract syntax". Thus."Automata Theory (ed. Particular punctuation marks. an approach to programming languages. Vienna (1964). KNUTH The output of this program is 1. Proc. suppose the rules L1 ~ BL2 v(L1) = 2t(L2)v(B) q.144 DONALD E. by E. North Holland. MicrO-ALGOL. 3! Oversights such as this are not unexpected when an algorithmic definition of a language is constructed. Academic Press. REFERENCES [ 1] J. [3] CORRADOB6HM and WOLF GROSS. 35-65. the grammar would become syntactically and semantically ambiguous. 51-55. ACM 11 (1964). It appears to be reasonable to assert that none of the previous schemes for formal definition of semantics could produce a definition of Turingol that is as brief or as easy to comprehend as the definition given above." Formal Language Description Languages for Computer Programming. North Holland. ELGOTand A. 2. Mathematisch Centrum. J.878. 1968. 1-12. I. Vol. Towards more versatile mechanical translators. 1966. [ 15] PL/I Definition Group of the Vienna Laboratory. IFIP Working Conf.J. pp. ACM 8 (1965). Sympos."The mechanical evaluation of expressions. 20 (1965).olland. 6 (1964). Comm. Math. Math. 308-320. 1966.Formal Language Description Languages for Computer Programming. (1964). Sympos.Semantics of Context-Free Languages 145 [7] EDGART. McCLUm~. LANDIN. Math. 262-274.A formal description of ALGOL60. Soc. 15. Correctness of a compiler for arithmetic expressions. Conf. I. Amer..The Art of Computer Programming. 41-50. North H. Proc. A correspondence between ALGOL60 and Church's lambda notation. R. Providence. [9] P. [8] DONALDE.. Appl. Formal Language Description Languages for Computer Programming. Appl. [10] P. KNUTH. pp. IRONS. 11-23.A formal definition of a subset of ALGOL. Proc. [13] JOHN MCCARTHYand JAMESPAmTrR. ACM 9 (1966). Proc. 158-165. Math. [14] ROBERT M.. [16] NXKLAUSWroTH and HELMUTWEBER. North Holland. R. LANDIN. Proc.J. [11] P. ACM Nat. [12] JOHN MCCARTHY. LANDXN. Vienna.A syntax directed compiler. to appear.. Formal definition of PL/L IBM Technical Report TR 25. 89-99. 89-101.. Providence.and its formal definition.071 (1966)." Comp.. Addison-Wesley. 1963. IFIP Working Conf. (Received 15 November 1967) . Amer. Soc. J. 266-294.. Euler: A generalization of ALGOL. Vienna (1964). Comm. I. 1967.. pp. T M G . Vol. 17. Proc.
Copyright © 2024 DOKUMEN.SITE Inc.