II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University II. Notes on Data Structuring * - Cornell University

cs.cornell.edu
from cs.cornell.edu More from this publisher
20.03.2013 Views

146 c.A.R. HOARE structure. In many circumstances, a bitstream representation is some ten times more compact than the tree representation. Tree I,I. 1 tag Bitstream I I IoI A I IoI B IOINILong>IIong>IoI B I01 A I (a) (b) FIG. 8. Representation of ((A. (B.NIL)). (B. A)) , NIL The choice between tree and linear representation is usually obvious. If the structure is being processed by the program, usually by means of recursive procedures, the needs of ready access to any component of the structure dictate a tree representation. In addition, some of the space lost may be regained by sharing common branches among several trees; such commonality of branches is a feature of the processing of symbolic expressions. However, if the structure has to be output and subsequently re-input, the linear structure is vastly preferable. Not only does the reduction in volume reduce transfer time, but the linearisation avoids a number of tricky problems of representing pointers in backing store. In many cases, a structure which passes through several phases of processing and input-output will be translated between the two representations at each phase; and this is standard practice in a multipass translator for a high-level programming language. It is important to note that the sharing of the recursive sub-structure is nothing but a means of saving time and storage, and has no effect on the running of the program. This means that the sharing must be avoided whenever there is any danger that the shared sub-structure might be selectively updated as part of one of its owners. In principle, all values are entirely disjoint from all other values, and there is no way in which the programmer could either know or care how far his structures are shared. Furthermore, there is no way whatsoever in which a pointer can be made to point back to a structure of which it is a component; since this would mean that the structure was identical to one of its'own components. Only an infinite struc- ture can have this property; and infinite structures do not satisfy the axiom of exclusion on which the important principle of induction for recursive structures is based.

9.2. EXAMPLE NOTES ON DATA STRUCTURING 1,47 A source text for an expression in a programming language is presented as a sequence of symbols defined: type symbol = (constant:(value: real), variable:(identifier:ident), op" operator, leftbracket, rightbracket); Write a program operating on an input variable source:sequence symbol, which reads from its beginning the longest possible legitimate expression, delivers the corresponding abstract expression as a result, and exits to the label error if this is impossible. The structure of the result and the syntax of the source are as specified earlier in this chapter. The structure of the program closely follows that of the desired result. There are three functions: compile expression compile term (sign) compile primary each of which removes from the source the longest expression in its syntactic category, and delivers the corresponding abstract structure as a resulf. The main irregularity of the process is that the first term of an expression may be unsigned; this is why the sign is provided as a parameter for compile term, instead of being read from source by compile term itself. Each function has the side-effect of shortening the source sequence if successful, and jumping to error if not. function compile expression: expression; begin sign" operator; end; if source, first = plus v source, first = minus then sign from source compile expression:= [compile term (sign)]; else sign: = plus; while source, first = plus v source.first = minus do begin sign from source; end function compile term (s: operator): term; compile expression :''[compile term (sign)] begin p" primary; sign" operator; fs" sequence factor;

146 c.A.R. HOARE<br />

structure. In many circumstances, a bitstream representati<strong>on</strong> is some ten<br />

times more compact than the tree representati<strong>on</strong>.<br />

Tree<br />

I,I. 1<br />

tag<br />

Bitstream I I IoI A I IoI B IOINIL<str<strong>on</strong>g>II</str<strong>on</strong>g>IoI B I01 A I<br />

(a)<br />

(b)<br />

FIG. 8. Representati<strong>on</strong> of ((A. (B.NIL)). (B. A))<br />

, NIL<br />

The choice between tree and linear representati<strong>on</strong> is usually obvious. If<br />

the structure is being processed by the program, usually by means of recursive<br />

procedures, the needs of ready access to any comp<strong>on</strong>ent of the structure<br />

dictate a tree representati<strong>on</strong>. In additi<strong>on</strong>, some of the space lost may be<br />

regained by sharing comm<strong>on</strong> branches am<strong>on</strong>g several trees; such comm<strong>on</strong>ality<br />

of branches is a feature of the processing of symbolic expressi<strong>on</strong>s. However,<br />

if the structure has to be output and subsequently re-input, the linear structure<br />

is vastly preferable. Not <strong>on</strong>ly does the reducti<strong>on</strong> in volume reduce transfer<br />

time, but the linearisati<strong>on</strong> avoids a number of tricky problems of representing<br />

pointers in backing store. In many cases, a structure which passes through<br />

several phases of processing and input-output will be translated between<br />

the two representati<strong>on</strong>s at each phase; and this is standard practice in a<br />

multipass translator for a high-level programming language.<br />

It is important to note that the sharing of the recursive sub-structure is<br />

nothing but a means of saving time and storage, and has no effect <strong>on</strong> the<br />

running of the program. This means that the sharing must be avoided<br />

whenever there is any danger that the shared sub-structure might be selectively<br />

updated as part of <strong>on</strong>e of its owners. In principle, all values are entirely<br />

disjoint from all other values, and there is no way in which the programmer<br />

could either know or care how far his structures are shared. Furthermore,<br />

there is no way whatsoever in which a pointer can be made to point back<br />

to a structure of which it is a comp<strong>on</strong>ent; since this would mean that the<br />

structure was identical to <strong>on</strong>e of its'own comp<strong>on</strong>ents. Only an infinite struc-<br />

ture can have this property; and infinite structures do not satisfy the axiom<br />

of exclusi<strong>on</strong> <strong>on</strong> which the important principle of inducti<strong>on</strong> for recursive<br />

structures is based.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!