3 PASCAL 入門 - Welcome - Grad. Sch. of Math., Nagoya 第3章 PASCAL 入門 3.1 ここでの目的 ここではPASCALの文法を中心に解説をするが,単にPASCALを用いてプログラムが書けるようにな ることが目的なのではなく,PASCALの言語構造を理解し,アルゴリズムを正確に記述できるようにする

  • Published on
    09-Apr-2018

  • View
    222

  • Download
    9

Transcript

<ul><li><p>25</p><p>3 PASCAL </p><p>3.1 </p><p> PASCAL , PASCAL </p><p>, PASCAL , </p><p>.</p><p>, , </p><p>. .</p><p>3.2 PASCAL </p><p>PASCAL , . </p><p>, .</p><p> , .</p><p> .</p><p> test.p , .p .</p><p> .</p><p>,,.</p><p>3.3 PASCAL </p><p>.</p><p>1. (Mule ),.</p><p>2. , .</p><p>3. .</p><p>, Mule , hello.p , , </p><p>.</p><p>% pc hello.p -o hello</p><p> -o hello , hello . , -o</p><p>hello , a.out .</p><p> hello </p><p>% ./hello</p><p>. , ./ 1 .</p><p>Exercise 3.3.1 test.p 1 UNIX , .</p></li><li><p>26 </p><p>% pc test.p -o test</p><p>, ,</p><p>% test</p><p>, . </p><p>3.3.1 PASCAL </p><p> PASCAL SPARCompiler SUN MicroSystems </p><p>. , ANSI , </p><p>.</p><p>, PASCAL , SPARCompiler . ,</p><p>, , .</p><p>3.4 PASCAL </p><p>3.4.1 PASCAL Program </p><p>PASCAL , , ,. ,</p><p>, . , , </p><p>, .</p><p>1. </p><p>2. </p><p>3. </p><p>4. </p><p>5. </p><p>6. </p><p> ; , begin end . </p><p>, C , ; , .</p><p>3.4.2 Bachus-Naur formula</p><p>PASCAL , Bachus-Naur formula (BNF) .</p><p>BNF ,</p><p>::= | { }</p><p> BNF , PASCAL . {} </p><p>.</p><p> BNF </p><p>Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp</p></li><li><p> 27</p><p> ::= .</p><p> ::= program ( { , } ) ;</p><p> ::= </p><p> ::= { }</p><p> ::= | </p><p> ::=</p><p>.</p><p>3.4.3 PASCAL </p><p>PASCAL , , , , , , .</p><p>, . PASCAL , </p><p>2 . , PASCAL , , </p><p>.</p><p>, .</p><p> , , , , .</p><p>3.4.4 </p><p>,. </p><p>,. , { , } . </p><p>, { } , (* *) . </p><p>3 . , .</p><p>3.4.5 </p><p>, , , , , . PASCAL , </p><p>. , .</p><p>3.4.5.1 </p><p>, , PASCAL . PASCAL </p><p>.</p><p> ::=</p><p>+| - | * | / | = | &lt; | &gt; | [ | ] | . | , | := | ; | ; | ( | ) | | = | .. |^| </p><p> SPARCompiler , .</p><p>~ &amp; | ! # %</p><p>2 SPARCompiler , default . , .3 SPARCompiler , /* , */ , " . , </p><p>.</p><p>Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp</p></li><li><p>28 </p><p>, SPRACompiler .</p><p> ::=</p><p>+| - | * | / | = | &lt; | &gt; | [ | ] | . | , | := | ; | ; | ( | ) | | = | .. |^|~|&amp;| | | ! |#|%| </p><p>3.4.5.2 </p><p>, , .</p><p>and array begin case const div do downto elsefile for forward function goto if in label main mod nil notof or packed procedure program record repeat set then totype until var while with</p><p>, SPARCompiler , .</p><p>define extern external module otherwiseprivate public static univ</p><p>PASCAL , $, _ , ,</p><p>system , .</p><p> PASCAL .</p><p>abs arctan boolean char chr cos dispose eof eoln expfalse get input integer ln maxint new odd ord outputpage pred put read readln real reset rewrite roundsin sqr sqrt succ text true trunc write writeln</p><p>, SPARCompiler , </p><p>FALSE TRUE addr alfa argc argv arshft asl asrassert bell card clock close date discard double exitexpo firstof flush getenv getfile halt in_range index integer16integer32 intset land lastof length linelimit lnot longreal lorlshft lsl lsr max maxchar message min minchar minintnext null open pack random remove return rshft seedshortreal single sizeof stlimit string substr sysclock tab timetrim univ_ptr unpack varying wallclock xor</p><p>3.4.5.3 </p><p>PASCAL 4 . , .</p><p> ::= { }</p><p> ::= </p><p> ::= . </p><p>| . E E </p><p> ::= | </p><p> ::= | </p><p> ::= +| -</p><p>,</p><p>1 100 0.1 5E-3 87.35E+8</p><p>4 , .</p><p>Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp</p></li><li><p> 29</p><p>, 87.35E+8 , 87.35 10(+8) .</p><p>, SPARCompiler , 2 36 . , </p><p> ::= { }</p><p>BASE ::= </p><p> BASE ::= BASE # </p><p> ::= | BASE | </p><p>, 11 36 A Z .</p><p> [0,maxint] .</p><p>3.4.5.4 </p><p> . , </p><p>.</p><p> ::= </p><p>, .</p><p>3.4.5.5 </p><p>, .</p><p> ::= </p><p> ::= | | | | </p><p> ::= = </p><p>3.4.6 </p><p>PASCAL, . , </p><p>, . . , , . ,</p><p>.</p><p>Example 3.4.1 .</p><p>x15sin(x+y)x*yx+y-xx = 1.5</p><p>, , .</p><p>Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp</p></li><li><p>30 </p><p>3.4.7 </p><p>, PASCAL, . , </p><p> ; .</p><p>Example 3.4.2 .</p><p>x := y + z</p><p>, .</p><p>, begin end .</p><p>Example 3.4.3 .</p><p>begina := b ;c := d ;</p><p>end</p><p>3.5 </p><p>.</p><p>.</p><p>Example 3.5.1 </p><p>{Program 1Hello World. .</p><p>}program Hello_World (output) ;</p><p>beginwriteln(Hello World.)</p><p>end.</p><p>, . (, , .)</p><p>program Hello_World (output) ;</p><p>. , Hello_World . , </p><p>, , output </p><p>. , , input .</p><p>beginend.</p><p>. PASCAL , end . </p><p>. , . .</p><p>Id: P2.tex,v 1.2 2000/04/21 02:10:17 naito Exp</p></li><li><p> 31</p><p>writeln(Hello World.)</p><p> writeln , , , </p><p>5 . , . , ; </p><p>. begin end , ;</p><p>. , , .</p><p>C , . ,</p><p>PASCAL .</p><p>Exercise 3.5.1 Program 1 , .</p><p> () () ()</p><p>3.6 </p><p>Section 3.4.1 , PASCAL . ,</p><p>. BNF , </p><p>.</p><p> ::= .</p><p> ::= program ( { , } ) ;</p><p> ::= </p><p> ::= { }</p><p> ::= | </p><p> ::=</p><p>, , , , , .</p><p>3.6.1 </p><p>. </p><p>, PASCAL , </p><p>. , , input output </p><p>,.</p><p> readln, writeln , input, output </p><p>.</p><p>3.6.2 </p><p>, . </p><p>, 6 .5 . , </p><p>.6 ,, .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>32 </p><p>, PASCAL goto , PASCAL </p><p>, goto , .</p><p>.</p><p> ::= | label { , } ;</p><p> ::= </p><p>, [0, 9999] . , , </p><p>, . , 7 007 .</p><p>3.6.3 </p><p>.</p><p> ::= | const = ; { = ; }</p><p>. , .</p><p> ::= | | | | </p><p> ::= </p><p>, ,, , </p><p>, .</p><p>Example 3.6.1 .</p><p>const a = 32 ; b = 1.0 ; c = -2.0 ; d = a ; e = -b ; f = ----- ;</p><p>3.6.4 </p><p>PASCAL , , . PASCAL</p><p>. .</p><p> ::= type = ; { = ; }</p><p> ::= | | </p><p>. , Example , .</p><p>3.6.5 </p><p>PASCAL . , </p><p>, .</p><p> ::= var { , } : ; { { , } : ; }</p><p>Example 3.6.2 .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 33</p><p>var a, b : integer ;c, d : real ;e : Boolean ;f : char ;</p><p>, , . , </p><p>, .</p><p>, , , .</p><p>3.7 </p><p>, , . PASCAL </p><p>, , , , . ,</p><p>, , </p><p>, , .</p><p>, , , </p><p>. , ,,, </p><p>.</p><p>,, .</p><p>3.7.1 </p><p>PASCAL , . , PASCAL </p><p>, . , ,</p><p>var a,b : integer ;begina := 1 ;b := 1</p><p>end ;</p><p>.</p><p>, SPARCompiler , .</p><p>var a,b : integer := 1 ;</p><p>, . , , a, b </p><p>, , .</p><p>3.7.2 </p><p>PASCAL , .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>34 </p><p> ::= | </p><p> ::= | </p><p> ::= | </p><p> ::= </p><p> ::= </p><p> ::= ( { , } )</p><p> ::= .. </p><p>3.7.2.1 </p><p>, . , </p><p>, .</p><p>Example 3.7.1 .</p><p>type color = (white, red, blue, yellow, green, black) ;day = (mon, tue, wed, thr, fri, sat, sun) ;</p><p>Example 3.7.2 .</p><p>type workday = (mon, tue, wed, thr, fri, sat, sun) ;free = (sat, sun) ;</p><p>, sat , .</p><p>, 0 </p><p>. , .</p><p>, , .</p><p>succ .</p><p>pred .</p><p>SPARCompiler , firstof , .</p><p>3.7.2.2 </p><p> PASCAL , , .</p><p> . .</p><p> . .</p><p> true false .</p><p> . .</p><p>3.7.2.3 </p><p>, , </p><p>. . , </p><p>. .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 35</p><p>Example 3.7.3 .</p><p>var a : 1..10 ;b : -20..30 ;</p><p>.</p><p>Example 3.7.4 .</p><p>type days = (mon,tue,wed,thr,fri,sat,sun) ;workd = mon..fri ;letter = a..z ;</p><p>, days .</p><p>, </p><p>.</p><p>3.7.2.4 </p><p> integer , , </p><p>. , . .</p><p>3.7.2.5 </p><p> real , , </p><p>. .</p><p>3.7.2.6 </p><p> Boolean , </p><p>type Boolean = (false, true) ;</p><p>. , false true , , false &lt; true </p><p>. .</p><p>3.7.2.7 </p><p> char , , . </p><p>.</p><p>, , .</p><p> .</p><p> .</p><p> .</p><p>, 7 . , , 0 </p><p>. .</p><p>7 , , ASCII .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>36 </p><p> 0 9 , .</p><p> A Z ,, </p><p>.</p><p> a z ,, </p><p>.</p><p>, . </p><p>.</p><p>ord , .</p><p>chr chr(i) i .</p><p>. , , pred, succ</p><p>,</p><p>pred(c) = chr(ord(c)-1)succ(c) = chr(ord(c)+1)</p><p>.</p><p>3.7.2.8 </p><p>, T1 T2 .</p><p>(a) T1 T2 .</p><p>(b) T1 T2 , T2 T1 , T1 T2 </p><p>.</p><p>(c) T1 T2 , , .</p><p>(d) T1 T2 .</p><p>3.7.3 </p><p>. , , </p><p>.</p><p>3.7.3.1 </p><p>, . SPAR-</p><p>Compiler , ASCII , </p><p>.</p><p>SPARCompiler , minchar, maxchar, bell, tab . </p><p>minchar 0,</p><p>maxchar 255,</p><p>bell 7,</p><p>tab 9</p><p>.</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 37</p><p>3.7.3.2 </p><p>, . , , </p><p>, , 2n .</p><p>SPARCompiler , . , </p><p> -xl . </p><p>, SPARCompiler, integer16, integer32 ,</p><p>.</p><p>, maxint, minint maxint PASCAL </p><p>., integer , .</p><p>, PASCAL , C , </p><p>,.</p><p>3.7.3.2.1 , Section 1.4 </p><p>.</p><p>3.7.3.3 </p><p>, . , , </p><p>, 2n .</p><p>SPARCompiler , real . , </p><p> -xl . </p><p>, SPARCompiler , single, shortreal, double, longreal </p><p>, , , .</p><p>, . </p><p>, .</p><p>(0). 10^</p><p>, 0 , . , </p><p>.</p><p>3.7.3.3.1 , Section 1.4 </p><p>.</p><p>, , </p><p>. , .</p><p>3.7.3.4 </p><p>, .</p><p>3.8 </p><p>3.8.1 </p><p>.</p><p> , .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>38 </p><p> , .</p><p>,, .</p><p>3.8.2 </p><p>. , </p><p>, , PASCAL </p><p>, x = x .</p><p>3.8.3 </p><p>PASCAL , , , </p><p>.</p><p>, . , </p><p>, , .</p><p>.</p><p> .</p><p> .</p><p> .</p><p>.</p><p>, ,</p><p> .</p><p> .</p><p> .</p><p>.</p><p>3.9 </p><p>3.9.1 </p><p>, PASCAL . ,</p><p>Jensen-Wirth . , SPARCompiler . </p><p>, SPARCompiler .</p><p>3.9.2 </p><p> PASCAL , , , , , </p><p>, , .</p><p>, , .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 39</p><p>3.9.2.1 </p><p> := . , , </p><p>. , , . .</p><p> ::= := | := </p><p>3.9.2.1.1 , T2 , T1 </p><p>.</p><p>(a) T1 T2 , .</p><p>(b) T1 T2 .</p><p>(c) T1 T2 , T2 T1 .</p><p>(d) T1 T2 , T2 , T1 .</p><p>(e) T1 T2 .</p><p> T2 T1 , T1 T2 </p><p>.</p><p>, .</p><p>3.9.2.2 </p><p> not , , .</p><p> ::= not</p><p>3.9.2.3 </p><p>.</p><p> ::= * | / | div | mod | and</p><p>.</p><p>* , , </p><p>/ , </p><p>div </p><p>mod </p><p>and </p><p>, / div .</p><p>3.9.2.3.1 / , x/y , y 0 , , y 0 </p><p>, x/y , x y .</p><p> x div y , y 0 , , .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>40 </p><p>abs(i) - abs(j) &lt; abs((i div j) * j) abs(i)</p><p>, abs,,. , abs(i) &lt; abs(j)</p><p> i div j 0 .</p><p> i mod j , j . , i mod j </p><p>.</p><p> i mod j = j - (k*j) , k .</p><p> 0 i mod j &lt; j</p><p>, i 0, j &gt; 0 , (i div j) * i + i mod j = i .</p><p>3.9.2.4 </p><p>.</p><p> ::= +| - | or</p><p>.</p><p>+ , , </p><p>- , </p><p>or </p><p>, +, - , , - , + </p><p>.</p><p>3.9.2.5 </p><p>, , .</p><p> ::= = | | &lt; | &gt; | = | in</p><p>.</p><p>in T </p><p> T </p><p>=, , , , T </p><p> , </p><p>= , , T </p><p> , = , , , . , = </p><p>, .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 41</p><p>, p, q , p=q . , p</p></li><li><p>42 </p><p> ::= | | | nil</p><p> ::= | | | | ( ) | not </p><p> ::= [ ]</p><p> ::= { , }| </p><p> ::= | .. </p><p> ::= | </p><p> ::= | | </p><p> ::= | </p><p>, , . [] </p><p>, [x..y] , x y .</p><p>Example 3.9.1 ,</p><p>x15(x+y+z)not p[red,black,green][1..10]</p><p>. , </p><p>x*yp or q(x</p></li><li><p> 43</p><p> PASCAL , . , </p><p>, , . , </p><p>.</p><p> C , , , . </p><p>. PASCAL .</p><p>, C . , C ,</p><p>a &gt; b &amp;&amp; c &lt; d</p><p>, PASCAL , and &gt;, &lt; , .</p><p>3.9.4 </p><p>SPARCompiler , 8 .</p><p>~ bitwise not </p><p>&amp; bitwise and </p><p>| bitwise or </p><p>! bitwise or </p><p>, bitwise not , bitwise and , bitwise or </p><p>3.9.5 </p><p>SPARCompiler , 9 .</p><p>asl </p><p>asr </p><p>lsl </p><p>lsr </p><p>3.9.5.1 </p><p>, . , </p><p>, . 1 , </p><p> 1 . , , 1 ,</p><p> 2 , 1 , 2 .</p><p>, , , , </p><p>. , .</p><p>Example 3.9.3 , 1 .</p><p>8 .9 , SPARCompiler .</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p>44 </p><p>1111 1111 1111 1000 1111 1111 1111 1100</p><p>0000 0000 0000 1000 0000 0000 0000 0100</p><p>, 1 .</p><p>Example 3.9.4 , 1 .</p><p>1111 1111 1111 1000 1111 1111 1111 0000</p><p>0000 0000 0000 1000 0000 0000 0001 0000</p><p>, , .</p><p>Example 3.9.5 , 1 .</p><p>1111 1111 1111 1000 0111 1111 1111 1100</p><p>1000 0000 0000 1000 0100 0000 0000 0100</p><p>Example 3.9.6 , 1 .</p><p>1111 1111 1111 1000 1111 1111 1111 0000</p><p>0100 0000 0000 1000 1000 0000 0001 0000</p><p>3.10 </p><p>, . , writeln </p><p>.</p><p>Example 3.10.1 , .</p><p>program print_example (output) ;vara : char ;b,c : integer ;x : real ;</p><p>begina := a ;b := 1 ;c := 2 ;x := 1.0 ;writeln(a = ,a) ;writeln(b = ,b, c = ,c) ;writeln(b div c = ,b div c) ;writeln(b / c = ,b/c) ;writeln(x = ,x)</p><p>end.</p><p>writeln , SPARCompiler writeln . </p><p>.</p><p>Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp</p></li><li><p> 45</p><p>Exercise 3.10.1 .</p><p>Exercise 3.10.2 integer 1 .</p><p>Exercise 3.10.3 .</p><p>Exercise 3.10.4 .</p><p>, writeln . , </p><p>, , .</p><p>Exercise 3.10.5 AND, OR, NOT XOR .</p><p>3.11 </p><p>. .</p><p> ::= { : } | </p><p>3.11.1 </p><p>, , , , .</p><p> ::= | | | goto </p><p> ::=</p><p>. , .</p><p>3.11.1.1 goto </p><p>goto .</p><p>goto ::= goto </p><p>, goto PASCAL , , </p><p>.</p><p>3.11.2 </p><p>, .</p><p> ::= | | | with </p><p> ::= { ; }</p><p>, , . , goto </p><p>.</p><p>Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp</p></li><li><p>46 </p><p>3.11.2.1 </p><p>, .</p><p> ::= begin end</p><p>Example 3.11.1 .</p><p>begin z := x ; x := y end</p><p>3.11.2.2 </p><p>.</p><p> ::= if | case </p><p>3.11.2.3 if </p><p>if , .</p><p>if ::= if then { else }</p><p>else ::= else </p><p>, if true , if . false </p><p>, if , else .</p><p>else if else . ,</p><p>if e1 then if e2 then s1 else s2</p><p>if e1 thenbegin if e2 then s2 else s2 end</p><p>.</p><p>Example 3.11.2 if .</p><p>if x 1.5 then z := x + y else z := 1.5 ;</p><p>if x 1.5 then z:= x + y</p><p>if x 1.5 thenif y 2.0 thenz:= x + y</p><p>else z := yelse z := x</p><p>Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp</p></li><li><p> 47</p><p>3.11.2.4 case </p><p>case , , .</p><p>case ::= case of { ; } end</p><p> ::= : </p><p> ::= </p><p> ::= { , }</p><p> ::= </p><p>Example 3.11.3 case .</p><p>case x of1: y := x ;2: z := x ;3,4: w := x</p><p>end</p><p>, case ,. </p><p>.</p><p>3.11.3 </p><p>, , .</p><p> ::= repeat | while | for </p><p>3.11.3.1 repeat </p><p>repeat ::= repeat until </p><p>repeat , true . </p><p>, .</p><p>Example 3.11.4 , 1 10 .</p><p>program sum ;var i,j : integer ;</p><p>begini := 0 ; j := 1 ;repeati := i + j ;j := j+1 ;</p><p>until j = 11 ;end.</p><p>Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp</p></li><li><p>48 </p><p>3.11.3.2 while </p><p>while ::= while do </p><p>while </p><p>while b do body</p><p>beginif b thenrepeat bodyuntil not(b)</p><p>end</p><p>. , , true , , false </p><p>. ,.</p><p>Example 3.11.5 , 1 10 .</p><p>program sum ;var i,j : integer ;</p><p>begini := 0 ; j := 1 ;while j </p></li><li><p> 49</p><p> S .</p><p> S read readln .</p><p>,</p><p>for v := e1 to e2 do body</p><p>, v e1 , e2 , succ body</p><p>. , , .</p><p>begint1 := e1 ;t2 := e2 ;if t1 t2end ;</p><p>,</p><p>for v := e1 downto e2 do body</p><p>, v e1 , e2 , pred body</p><p>.</p><p>Example 3.11.6 , 1 10 .</p><p>program sum ;var i,j : integer ;</p><p>begini := 0 ;for j:=1 to 10 doi := i + j ;</p><p>end.</p><p>Example 3.11.7 , 1 10 .</p><p>program sum ;var i,j : integer ;</p><p>begini := 0 ;for j:=10 downto 1 doi := i + j ;</p><p>end.</p><p>, for , 1 , while , </p><p>repeat .</p><p>3.11.3.4 with </p><p>with , .</p><p>Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp</p></li><li><p>50 </p><p>3.11.4 </p><p>, .</p><p>.</p><p>3.11.4.1 </p><p>, readln .</p><p>readln(v)</p><p>, .</p><p>Example 3.11.8 , .</p><p>var n : integer ;beginreadln(n) ;writeln(n)</p><p>end.</p><p>3.11.4.2 </p><p>random SPARCompiler real . , </p><p>.</p><p>Example 3.11.9 .</p><p>var n : integer ;r, s : real ;</p><p>beginn := seed(wallclock) ;r := random(n) ;s := random(n) ;</p><p>end.</p><p>(SPARCompiler seed , , </p><p>. , random n , . ,</p><p> 0 1 .</p><p>Exercise 3.11.1 repeat , while , for , 2 N </p><p>.</p><p>Exercise 3.11.2 , C = (5/9)(F 32) . </p><p>, . , , , </p><p>, .</p><p>Exercise 3.11.3 integer , .</p><p>Exercise 3.11.4 0 1 , 0.5 </p><p>.</p><p>Exercise 3.11.5 , 1 + = 1 . </p><p>.</p><p>Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp</p></li><li><p> 51</p><p>Exercise 3.11.6 for </p><p>for v := e1 downto e2 do body</p><p> repeat .</p><p>3.12 </p><p>, , , , .</p><p> ::= | </p><p> ::= </p><p> ::= { packed } </p><p> ::= | | | </p><p> packed , . </p><p>, , </p><p>10 .</p><p>3.12.1 </p><p> (array) , . </p><p>.</p><p> ::= array [ { , } ] of </p><p> ::= </p><p> ::= </p><p>Example 3.12.1 , integer 0 9 digit </p><p>.</p><p>var digit : array [0..9] of integer ;</p><p>, 0..9 .</p><p>digit[0] digit[1] digit[2] digit[3] digit[4] digit[5] digit[6] digit[7] digit[8] digit[9]</p><p>, .</p><p>array [Boolean] of color</p><p>, color , .</p><p>, .</p><p> .</p><p> , </p><p>. , , .</p><p>, .10 SPARCompiler , packed .</p><p>Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp</p></li><li><p>52 </p><p>Example 3.12.2 </p><p>array [Boolean] of array [1..10] of array [size] of realarray [Boolean] of array [1..10,size] of realarray [Boolean,1..10] of array [size] of realarray [Boolean,1..10,size] of real</p><p>Example 3.12.3 </p><p>packed array [1..10,1..8] of Boolean ;packed array [1..10] of packed array [1..8] of Boolean ;</p><p>Example 3.12.4 </p><p>var str : array [0..1][0..9] of integer ;</p><p>str[i] , 10 .</p><p>str[0] [0] str[0] [1] str[0] [2] str[0] [3] str[0] [4] str[0] [5] str[0] [6] str[0] [7]</p><p>str[1] [0] str[1] [1] str[1] [2] str[1] [3] str[1] [4] str[1] [5] str[1] [6] str[1] [7]</p><p>str[0] [8] str[0] [9]</p><p>str[1] [8] str[1] [9]</p><p>. m, n ,</p><p>k = ord(n)ord(m)+1 , , k . , v = (v1, , vk)</p><p>, v v , v[i] vord(i)ord(m)+1 . , </p><p>.</p><p> ::= | </p><p> ::= [ { , } ]</p><p> ::= </p><p> ::= </p><p>, , .</p><p>, , </p><p>.</p><p>.</p><p>3.12.1.1 </p><p>, real , .</p><p>program sum_of_vector ;vara,b,c : array[1..3] of real ;i : integer ;</p><p>begina[1] := 1.0 ; a[2] := 1.0 ; a[3] := 2.0 ;b[1] := 2.0 ; b[2] := 0.0 ; b[3] := 3.0 ;for i:=1 to 3 doc[i] := a[i] + b[i] ;</p><p>end.</p><p>Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp</p></li><li><p> 53</p><p>3.12.2 </p><p>, .</p><p> 1 , 1 .</p><p> .</p><p>, , , </p><p>.</p><p>3.12.2.1 </p><p>, , .</p><p>program output_of_string (output) ;vars : packed array [1..30] of char ;i : integer ;</p><p>beginfor i := 1 to 30 dos[i] := chr(ord(A) + (i-1)) ;</p><p>writeln(s) ;end.</p><p>, 30 , SPARCompiler . ,</p><p>SPARCompiler , .</p><p>alfa 10 </p><p>string 80 </p><p>varying ,</p><p>varying[u] , u .</p><p>u 0 65535 .</p><p>, writeln , </p><p>.</p><p>varname1 : packed array [1..25] of char ;name2 : packed array [76..100] of char ;name3 : alfa ;name4 : string ;name5 : varying [25] of char ;name6 : varying [25] of char ;</p><p>beginname1 := a ;name2 := a ;name3 := a ;name4 := a ;name5 := a ;name6 := a ;writeln(name1, and , name2) ;writeln(name3, and , name4) ;writeln(name4, and , name6)</p><p>end.</p><p>Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp</p></li><li><p>54 </p><p>3.12.2.2 </p><p>SPRACompiler , , .</p><p>varint : array [1..10] of integer = [maxint,1,-375,5,20] ;ch : packed array [1..10] of char = ABCDEF ;int2 : array [1..*] of integer = [maxint,1,-375,5,20] ;ch2 : packed array [1..*] of char = ABCDEF ;</p><p>, * , , </p><p>.</p><p>varint : array [1..100] of integer = [50 of 1,50 of 2] ;</p><p>, 50 1 , 50 2 .</p><p>varint : array [1..100] of integer = [* of 1] ;int2 : array [1..10, 1..10] of integer = [[* of 1], [3 of 8], [10 of 88],] ;</p><p>, 1 , 8 , 88 .</p><p>, SPARCompiler , , </p><p>, . , </p><p>.</p><p>3.12.3 </p><p>, . , </p><p>.</p><p> ::= set of </p><p> ::= </p><p>, .</p><p>set of char ;set of (club, diamond

Recommended

View more >