20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.2. EXAMPLE<br />

NOTES ON DATA STRUCTURING 1,47<br />

A source text for an expressi<strong>on</strong> in a programming language is presented<br />

as a sequence of symbols defined:<br />

type symbol = (c<strong>on</strong>stant:(value: real), variable:(identifier:ident),<br />

op" operator, leftbracket, rightbracket);<br />

Write a program operating <strong>on</strong> an input variable<br />

source:sequence symbol,<br />

which reads from its beginning the l<strong>on</strong>gest possible legitimate expressi<strong>on</strong>,<br />

delivers the corresp<strong>on</strong>ding abstract expressi<strong>on</strong> as a result, and exits to the<br />

label error if this is impossible. The structure of the result and the syntax of<br />

the source are as specified earlier in this chapter.<br />

The structure of the program closely follows that of the desired result.<br />

There are three functi<strong>on</strong>s:<br />

compile expressi<strong>on</strong><br />

compile term (sign)<br />

compile primary<br />

each of which removes from the source the l<strong>on</strong>gest expressi<strong>on</strong> in its syntactic<br />

category, and delivers the corresp<strong>on</strong>ding abstract structure as a resulf. The<br />

main irregularity of the process is that the first term of an expressi<strong>on</strong> may be<br />

unsigned; this is why the sign is provided as a parameter for compile term,<br />

instead of being read from source by compile term itself. Each functi<strong>on</strong> has<br />

the side-effect of shortening the source sequence if successful, and jumping<br />

to error if not.<br />

functi<strong>on</strong> compile expressi<strong>on</strong>: expressi<strong>on</strong>;<br />

begin sign" operator;<br />

end;<br />

if source, first = plus v source, first = minus then sign from source<br />

compile expressi<strong>on</strong>:= [compile term (sign)];<br />

else sign: = plus;<br />

while source, first = plus v source.first = minus do<br />

begin sign from source;<br />

end<br />

functi<strong>on</strong> compile term (s: operator): term;<br />

compile expressi<strong>on</strong> :''[compile term (sign)]<br />

begin p" primary; sign" operator; fs" sequence factor;

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

Saved successfully!

Ooh no, something went wrong!