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

  • Published on
    09-Apr-2018

  • View
    222

  • Download
    9

Embed Size (px)

Transcript

  • 25

    3 PASCAL

    3.1

    PASCAL , PASCAL

    , PASCAL ,

    .

    , ,

    . .

    3.2 PASCAL

    PASCAL , .

    , .

    , .

    .

    test.p , .p .

    .

    ,,.

    3.3 PASCAL

    .

    1. (Mule ),.

    2. , .

    3. .

    , Mule , hello.p , ,

    .

    % pc hello.p -o hello

    -o hello , hello . , -o

    hello , a.out .

    hello

    % ./hello

    . , ./ 1 .

    Exercise 3.3.1 test.p 1 UNIX , .

  • 26

    % pc test.p -o test

    , ,

    % test

    , .

    3.3.1 PASCAL

    PASCAL SPARCompiler SUN MicroSystems

    . , ANSI ,

    .

    , PASCAL , SPARCompiler . ,

    , , .

    3.4 PASCAL

    3.4.1 PASCAL Program

    PASCAL , , ,. ,

    , . , ,

    , .

    1.

    2.

    3.

    4.

    5.

    6.

    ; , begin end .

    , C , ; , .

    3.4.2 Bachus-Naur formula

    PASCAL , Bachus-Naur formula (BNF) .

    BNF ,

    ::= | { }

    BNF , PASCAL . {}

    .

    BNF

    Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp

  • 27

    ::= .

    ::= program ( { , } ) ;

    ::=

    ::= { }

    ::= |

    ::=

    .

    3.4.3 PASCAL

    PASCAL , , , , , , .

    , . PASCAL ,

    2 . , PASCAL , ,

    .

    , .

    , , , , .

    3.4.4

    ,.

    ,. , { , } .

    , { } , (* *) .

    3 . , .

    3.4.5

    , , , , , . PASCAL ,

    . , .

    3.4.5.1

    , , PASCAL . PASCAL

    .

    ::=

    +| - | * | / | = | < | > | [ | ] | . | , | := | ; | ; | ( | ) | | = | .. |^|

    SPARCompiler , .

    ~ & | ! # %

    2 SPARCompiler , default . , .3 SPARCompiler , /* , */ , " . ,

    .

    Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp

  • 28

    , SPRACompiler .

    ::=

    +| - | * | / | = | < | > | [ | ] | . | , | := | ; | ; | ( | ) | | = | .. |^|~|&| | | ! |#|%|

    3.4.5.2

    , , .

    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

    , SPARCompiler , .

    define extern external module otherwiseprivate public static univ

    PASCAL , $, _ , ,

    system , .

    PASCAL .

    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

    , SPARCompiler ,

    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

    3.4.5.3

    PASCAL 4 . , .

    ::= { }

    ::=

    ::= .

    | . E E

    ::= |

    ::= |

    ::= +| -

    ,

    1 100 0.1 5E-3 87.35E+8

    4 , .

    Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp

  • 29

    , 87.35E+8 , 87.35 10(+8) .

    , SPARCompiler , 2 36 . ,

    ::= { }

    BASE ::=

    BASE ::= BASE #

    ::= | BASE |

    , 11 36 A Z .

    [0,maxint] .

    3.4.5.4

    . ,

    .

    ::=

    , .

    3.4.5.5

    , .

    ::=

    ::= | | | |

    ::= =

    3.4.6

    PASCAL, . ,

    , . . , , . ,

    .

    Example 3.4.1 .

    x15sin(x+y)x*yx+y-xx = 1.5

    , , .

    Id: P1.tex,v 1.3 2000/04/21 02:09:32 naito Exp

  • 30

    3.4.7

    , PASCAL, . ,

    ; .

    Example 3.4.2 .

    x := y + z

    , .

    , begin end .

    Example 3.4.3 .

    begina := b ;c := d ;

    end

    3.5

    .

    .

    Example 3.5.1

    {Program 1Hello World. .

    }program Hello_World (output) ;

    beginwriteln(Hello World.)

    end.

    , . (, , .)

    program Hello_World (output) ;

    . , Hello_World . ,

    , , output

    . , , input .

    beginend.

    . PASCAL , end .

    . , . .

    Id: P2.tex,v 1.2 2000/04/21 02:10:17 naito Exp

  • 31

    writeln(Hello World.)

    writeln , , ,

    5 . , . , ;

    . begin end , ;

    . , , .

    C , . ,

    PASCAL .

    Exercise 3.5.1 Program 1 , .

    () () ()

    3.6

    Section 3.4.1 , PASCAL . ,

    . BNF ,

    .

    ::= .

    ::= program ( { , } ) ;

    ::=

    ::= { }

    ::= |

    ::=

    , , , , , .

    3.6.1

    .

    , PASCAL ,

    . , , input output

    ,.

    readln, writeln , input, output

    .

    3.6.2

    , .

    , 6 .5 . ,

    .6 ,, .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 32

    , PASCAL goto , PASCAL

    , goto , .

    .

    ::= | label { , } ;

    ::=

    , [0, 9999] . , ,

    , . , 7 007 .

    3.6.3

    .

    ::= | const = ; { = ; }

    . , .

    ::= | | | |

    ::=

    , ,, ,

    , .

    Example 3.6.1 .

    const a = 32 ; b = 1.0 ; c = -2.0 ; d = a ; e = -b ; f = ----- ;

    3.6.4

    PASCAL , , . PASCAL

    . .

    ::= type = ; { = ; }

    ::= | |

    . , Example , .

    3.6.5

    PASCAL . ,

    , .

    ::= var { , } : ; { { , } : ; }

    Example 3.6.2 .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 33

    var a, b : integer ;c, d : real ;e : Boolean ;f : char ;

    , , . ,

    , .

    , , , .

    3.7

    , , . PASCAL

    , , , , . ,

    , ,

    , , .

    , , ,

    . , ,,,

    .

    ,, .

    3.7.1

    PASCAL , . , PASCAL

    , . , ,

    var a,b : integer ;begina := 1 ;b := 1

    end ;

    .

    , SPARCompiler , .

    var a,b : integer := 1 ;

    , . , , a, b

    , , .

    3.7.2

    PASCAL , .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 34

    ::= |

    ::= |

    ::= |

    ::=

    ::=

    ::= ( { , } )

    ::= ..

    3.7.2.1

    , . ,

    , .

    Example 3.7.1 .

    type color = (white, red, blue, yellow, green, black) ;day = (mon, tue, wed, thr, fri, sat, sun) ;

    Example 3.7.2 .

    type workday = (mon, tue, wed, thr, fri, sat, sun) ;free = (sat, sun) ;

    , sat , .

    , 0

    . , .

    , , .

    succ .

    pred .

    SPARCompiler , firstof , .

    3.7.2.2

    PASCAL , , .

    . .

    . .

    true false .

    . .

    3.7.2.3

    , ,

    . . ,

    . .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 35

    Example 3.7.3 .

    var a : 1..10 ;b : -20..30 ;

    .

    Example 3.7.4 .

    type days = (mon,tue,wed,thr,fri,sat,sun) ;workd = mon..fri ;letter = a..z ;

    , days .

    ,

    .

    3.7.2.4

    integer , ,

    . , . .

    3.7.2.5

    real , ,

    . .

    3.7.2.6

    Boolean ,

    type Boolean = (false, true) ;

    . , false true , , false < true

    . .

    3.7.2.7

    char , , .

    .

    , , .

    .

    .

    .

    , 7 . , , 0

    . .

    7 , , ASCII .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 36

    0 9 , .

    A Z ,,

    .

    a z ,,

    .

    , .

    .

    ord , .

    chr chr(i) i .

    . , , pred, succ

    ,

    pred(c) = chr(ord(c)-1)succ(c) = chr(ord(c)+1)

    .

    3.7.2.8

    , T1 T2 .

    (a) T1 T2 .

    (b) T1 T2 , T2 T1 , T1 T2

    .

    (c) T1 T2 , , .

    (d) T1 T2 .

    3.7.3

    . , ,

    .

    3.7.3.1

    , . SPAR-

    Compiler , ASCII ,

    .

    SPARCompiler , minchar, maxchar, bell, tab .

    minchar 0,

    maxchar 255,

    bell 7,

    tab 9

    .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 37

    3.7.3.2

    , . , ,

    , , 2n .

    SPARCompiler , . ,

    -xl .

    , SPARCompiler, integer16, integer32 ,

    .

    , maxint, minint maxint PASCAL

    ., integer , .

    , PASCAL , C ,

    ,.

    3.7.3.2.1 , Section 1.4

    .

    3.7.3.3

    , . , ,

    , 2n .

    SPARCompiler , real . ,

    -xl .

    , SPARCompiler , single, shortreal, double, longreal

    , , , .

    , .

    , .

    (0). 10^

    , 0 , . ,

    .

    3.7.3.3.1 , Section 1.4

    .

    , ,

    . , .

    3.7.3.4

    , .

    3.8

    3.8.1

    .

    , .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 38

    , .

    ,, .

    3.8.2

    . ,

    , , PASCAL

    , x = x .

    3.8.3

    PASCAL , , ,

    .

    , . ,

    , , .

    .

    .

    .

    .

    .

    , ,

    .

    .

    .

    .

    3.9

    3.9.1

    , PASCAL . ,

    Jensen-Wirth . , SPARCompiler .

    , SPARCompiler .

    3.9.2

    PASCAL , , , , ,

    , , .

    , , .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 39

    3.9.2.1

    := . , ,

    . , , . .

    ::= := | :=

    3.9.2.1.1 , T2 , T1

    .

    (a) T1 T2 , .

    (b) T1 T2 .

    (c) T1 T2 , T2 T1 .

    (d) T1 T2 , T2 , T1 .

    (e) T1 T2 .

    T2 T1 , T1 T2

    .

    , .

    3.9.2.2

    not , , .

    ::= not

    3.9.2.3

    .

    ::= * | / | div | mod | and

    .

    * , ,

    / ,

    div

    mod

    and

    , / div .

    3.9.2.3.1 / , x/y , y 0 , , y 0

    , x/y , x y .

    x div y , y 0 , , .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 40

    abs(i) - abs(j) < abs((i div j) * j) abs(i)

    , abs,,. , abs(i) < abs(j)

    i div j 0 .

    i mod j , j . , i mod j

    .

    i mod j = j - (k*j) , k .

    0 i mod j < j

    , i 0, j > 0 , (i div j) * i + i mod j = i .

    3.9.2.4

    .

    ::= +| - | or

    .

    + , ,

    - ,

    or

    , +, - , , - , +

    .

    3.9.2.5

    , , .

    ::= = | | < | > | = | in

    .

    in T

    T

    =, , , , T

    ,

    = , , T

    , = , , , . , =

    , .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 41

    , p, q , p=q . , p

  • 42

    ::= | | | nil

    ::= | | | | ( ) | not

    ::= [ ]

    ::= { , }|

    ::= | ..

    ::= |

    ::= | |

    ::= |

    , , . []

    , [x..y] , x y .

    Example 3.9.1 ,

    x15(x+y+z)not p[red,black,green][1..10]

    . ,

    x*yp or q(x

  • 43

    PASCAL , . ,

    , , . ,

    .

    C , , , .

    . PASCAL .

    , C . , C ,

    a > b && c < d

    , PASCAL , and >, < , .

    3.9.4

    SPARCompiler , 8 .

    ~ bitwise not

    & bitwise and

    | bitwise or

    ! bitwise or

    , bitwise not , bitwise and , bitwise or

    3.9.5

    SPARCompiler , 9 .

    asl

    asr

    lsl

    lsr

    3.9.5.1

    , . ,

    , . 1 ,

    1 . , , 1 ,

    2 , 1 , 2 .

    , , , ,

    . , .

    Example 3.9.3 , 1 .

    8 .9 , SPARCompiler .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 44

    1111 1111 1111 1000 1111 1111 1111 1100

    0000 0000 0000 1000 0000 0000 0000 0100

    , 1 .

    Example 3.9.4 , 1 .

    1111 1111 1111 1000 1111 1111 1111 0000

    0000 0000 0000 1000 0000 0000 0001 0000

    , , .

    Example 3.9.5 , 1 .

    1111 1111 1111 1000 0111 1111 1111 1100

    1000 0000 0000 1000 0100 0000 0000 0100

    Example 3.9.6 , 1 .

    1111 1111 1111 1000 1111 1111 1111 0000

    0100 0000 0000 1000 1000 0000 0001 0000

    3.10

    , . , writeln

    .

    Example 3.10.1 , .

    program print_example (output) ;vara : char ;b,c : integer ;x : real ;

    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)

    end.

    writeln , SPARCompiler writeln .

    .

    Id: P3.tex,v 1.3 2000/04/21 02:12:24 naito Exp

  • 45

    Exercise 3.10.1 .

    Exercise 3.10.2 integer 1 .

    Exercise 3.10.3 .

    Exercise 3.10.4 .

    , writeln . ,

    , , .

    Exercise 3.10.5 AND, OR, NOT XOR .

    3.11

    . .

    ::= { : } |

    3.11.1

    , , , , .

    ::= | | | goto

    ::=

    . , .

    3.11.1.1 goto

    goto .

    goto ::= goto

    , goto PASCAL , ,

    .

    3.11.2

    , .

    ::= | | | with

    ::= { ; }

    , , . , goto

    .

    Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp

  • 46

    3.11.2.1

    , .

    ::= begin end

    Example 3.11.1 .

    begin z := x ; x := y end

    3.11.2.2

    .

    ::= if | case

    3.11.2.3 if

    if , .

    if ::= if then { else }

    else ::= else

    , if true , if . false

    , if , else .

    else if else . ,

    if e1 then if e2 then s1 else s2

    if e1 thenbegin if e2 then s2 else s2 end

    .

    Example 3.11.2 if .

    if x 1.5 then z := x + y else z := 1.5 ;

    if x 1.5 then z:= x + y

    if x 1.5 thenif y 2.0 thenz:= x + y

    else z := yelse z := x

    Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp

  • 47

    3.11.2.4 case

    case , , .

    case ::= case of { ; } end

    ::= :

    ::=

    ::= { , }

    ::=

    Example 3.11.3 case .

    case x of1: y := x ;2: z := x ;3,4: w := x

    end

    , case ,.

    .

    3.11.3

    , , .

    ::= repeat | while | for

    3.11.3.1 repeat

    repeat ::= repeat until

    repeat , true .

    , .

    Example 3.11.4 , 1 10 .

    program sum ;var i,j : integer ;

    begini := 0 ; j := 1 ;repeati := i + j ;j := j+1 ;

    until j = 11 ;end.

    Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp

  • 48

    3.11.3.2 while

    while ::= while do

    while

    while b do body

    beginif b thenrepeat bodyuntil not(b)

    end

    . , , true , , false

    . ,.

    Example 3.11.5 , 1 10 .

    program sum ;var i,j : integer ;

    begini := 0 ; j := 1 ;while j

  • 49

    S .

    S read readln .

    ,

    for v := e1 to e2 do body

    , v e1 , e2 , succ body

    . , , .

    begint1 := e1 ;t2 := e2 ;if t1 t2end ;

    ,

    for v := e1 downto e2 do body

    , v e1 , e2 , pred body

    .

    Example 3.11.6 , 1 10 .

    program sum ;var i,j : integer ;

    begini := 0 ;for j:=1 to 10 doi := i + j ;

    end.

    Example 3.11.7 , 1 10 .

    program sum ;var i,j : integer ;

    begini := 0 ;for j:=10 downto 1 doi := i + j ;

    end.

    , for , 1 , while ,

    repeat .

    3.11.3.4 with

    with , .

    Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp

  • 50

    3.11.4

    , .

    .

    3.11.4.1

    , readln .

    readln(v)

    , .

    Example 3.11.8 , .

    var n : integer ;beginreadln(n) ;writeln(n)

    end.

    3.11.4.2

    random SPARCompiler real . ,

    .

    Example 3.11.9 .

    var n : integer ;r, s : real ;

    beginn := seed(wallclock) ;r := random(n) ;s := random(n) ;

    end.

    (SPARCompiler seed , ,

    . , random n , . ,

    0 1 .

    Exercise 3.11.1 repeat , while , for , 2 N

    .

    Exercise 3.11.2 , C = (5/9)(F 32) .

    , . , , ,

    , .

    Exercise 3.11.3 integer , .

    Exercise 3.11.4 0 1 , 0.5

    .

    Exercise 3.11.5 , 1 + = 1 .

    .

    Id: P4.tex,v 1.2 2000/04/21 02:13:09 naito Exp

  • 51

    Exercise 3.11.6 for

    for v := e1 downto e2 do body

    repeat .

    3.12

    , , , , .

    ::= |

    ::=

    ::= { packed }

    ::= | | |

    packed , .

    , ,

    10 .

    3.12.1

    (array) , .

    .

    ::= array [ { , } ] of

    ::=

    ::=

    Example 3.12.1 , integer 0 9 digit

    .

    var digit : array [0..9] of integer ;

    , 0..9 .

    digit[0] digit[1] digit[2] digit[3] digit[4] digit[5] digit[6] digit[7] digit[8] digit[9]

    , .

    array [Boolean] of color

    , color , .

    , .

    .

    ,

    . , , .

    , .10 SPARCompiler , packed .

    Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp

  • 52

    Example 3.12.2

    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

    Example 3.12.3

    packed array [1..10,1..8] of Boolean ;packed array [1..10] of packed array [1..8] of Boolean ;

    Example 3.12.4

    var str : array [0..1][0..9] of integer ;

    str[i] , 10 .

    str[0] [0] str[0] [1] str[0] [2] str[0] [3] str[0] [4] str[0] [5] str[0] [6] str[0] [7]

    str[1] [0] str[1] [1] str[1] [2] str[1] [3] str[1] [4] str[1] [5] str[1] [6] str[1] [7]

    str[0] [8] str[0] [9]

    str[1] [8] str[1] [9]

    . m, n ,

    k = ord(n)ord(m)+1 , , k . , v = (v1, , vk)

    , v v , v[i] vord(i)ord(m)+1 . ,

    .

    ::= |

    ::= [ { , } ]

    ::=

    ::=

    , , .

    , ,

    .

    .

    3.12.1.1

    , real , .

    program sum_of_vector ;vara,b,c : array[1..3] of real ;i : integer ;

    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] ;

    end.

    Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp

  • 53

    3.12.2

    , .

    1 , 1 .

    .

    , , ,

    .

    3.12.2.1

    , , .

    program output_of_string (output) ;vars : packed array [1..30] of char ;i : integer ;

    beginfor i := 1 to 30 dos[i] := chr(ord(A) + (i-1)) ;

    writeln(s) ;end.

    , 30 , SPARCompiler . ,

    SPARCompiler , .

    alfa 10

    string 80

    varying ,

    varying[u] , u .

    u 0 65535 .

    , writeln ,

    .

    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 ;

    beginname1 := a ;name2 := a ;name3 := a ;name4 := a ;name5 := a ;name6 := a ;writeln(name1, and , name2) ;writeln(name3, and , name4) ;writeln(name4, and , name6)

    end.

    Id: P5.tex,v 1.2 2000/04/21 02:14:33 naito Exp

  • 54

    3.12.2.2

    SPRACompiler , , .

    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 ;

    , * , ,

    .

    varint : array [1..100] of integer = [50 of 1,50 of 2] ;

    , 50 1 , 50 2 .

    varint : array [1..100] of integer = [* of 1] ;int2 : array [1..10, 1..10] of integer = [[* of 1], [3 of 8], [10 of 88],] ;

    , 1 , 8 , 88 .

    , SPARCompiler , ,

    , . ,

    .

    3.12.3

    , . ,

    .

    ::= set of

    ::=

    , .

    set of char ;set of (club, diamond

Recommended

View more >