⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pascals.txt

📁 pascals编译程序源代码及说明
💻 TXT
📖 第 1 页 / 共 5 页
字号:
    0       var a: array ['a'..'z'] of complex;
    0           u: char;
    0       begin while x<y do x := x+1.O;
   10          null:=x=y
   12       end (*null*);
   IS
   IS    beginp := null(hl.c.re, h2c.im,z)
   25    end (*dummy*);
   27
   27 begin i := 85; j := 51;
   34    repeat
   34       if i > j then i := i-j else j := j-i
   46    until i = j;
   53    writeln(i)
   55 end.

Identifiers:                 link  obj  typ  ref  nrm  lev  adr
              29     ten     0     0    1    0    0    1    10
              30     plus    29    0    4    0    0    1    37
              31     row     30    2    5    1    0    1    10
              32     complex 31    2    6    3    0    1    2
              33     re      0     1    2    0    1    2    0
              34     im      33    1    2    0    1    2    1
              35     i       32    1    1    0    1    1    5
              36     j       35    1    1    0    1    1    6
              37     p       36    1    3    0    1    1    7
              38     z       37    1    6    3    1    1    8
              39     matrix  38    1    5    2    1    1    10
              40     pattern 39    1    5    3    1    1    80
              41     dummy   40    3    0    4    1    1    16
              42     i       0     1    1    0    0    2    5
              43     z       42    1    6    3    0    2    6
              44     u       43    1    5    1    1    2    7
              45     v       44    1    5    1    1    2    17
              46     hI      45    1    6    5    1    2    27
              47     h2      46    1    6    5    1    2    39
              48     c       0     1    6    3    1    3    0
              49     r       48    1    5    1    1    3    2
              50     null    47    4    3    6    1    2    0
              51     x       0     1    2    0    1    3    5
              52     Y       51    1    2    0    1    3    6
              53     z       52    1    6    3    1    3    7
              54     a       53    1    5    5    1    3    9
              55     u       54    1    4    0    1    3    61

Blocks:      last  1par  psze  vsze
         1   28     1    0        0
         2   41    28    5      105
         3   34     0    0        2
         4   50    43    7       51
         5   49     0    0       12
         6   55    53    9       62

Arrays:      xtyp  etyp  eref  low  high  elsz  size
         1    1     2    0       1  10      1   10
         2    1     5    1      -3   3     10   70
         3    1     5    4       1   5      5   25
         4    1     4    0       1   5      1   5
         5    4     6    3       1  26      2   52

Code:
 0       13    5,   13   6,   41    ,   11   10,   03    5,
 5       13    5,   25   1,   54    ,   38     ,   10    0,
10       03    0,   13   5,   13   6,   39     ,   38     ,
15       33     ,   01   7,   18  50,   02   27,   34     ,
20       02   39,    9   1,   34    ,   12    6,   22    2,
25       19    8,   38    ,   32    ,   01    5,   24    85,
30       38     ,   01   6,   24  51,   38     ,   11     5,
35       11    6,   49    ,   11  44,    0    5,   11     5,
40       11    6,   53    ,   38    ,   10   49,   01    6,
45       11    6,   11   5,   53    ,   38     ,   11    5,
50       11    6,   45    ,   11  34,    1   15,   29    1,
55       63     ,   31    ,
(eof)
      17
      51 steps

                       6. Machine-dependencies

Every program must be tailored to the facilities that are available in the language
and the computing system used. It is desirable to restrict these considerations to the
high-level language (in this case Pascal) and to ignore the actual hardware. In the
case of a program to be used thousands of times and every day by many people,
this is only possible and economically justifiable if the language successfully hides
the hardware without causing appreciable loss in efficiency. Although the Pascal 6000-
3.4 compiler satisfies this requirement to a high degree, we nevertheless had to
resort to facilities that are not available in standard Pascal in a few instances. These
facilities are particular to the Pascal implementation on the CDC computer and are
listed in detail below. Their use introduces what may be called First-order machine-
dependencies.

   The only such facility used is the segmented file. It allows one to recognize a
substructure of the file called segment (in Pascal terminology) or 'logical record' (in
CDC terminology). The fact that the file input is to be treated like a segmented file
is indicated by a plus sign in the program parameter list (implemented through
Update 10 of Pascal 6000-3.4). A job is represented in the CDC operating system as
an input file consisting of three segments; control statements for the operating
system, program, and data. The recognition of this substructure is essential for
Pascal-S in order to skip backwards to list the input data segment, and it is desirable
in order to keep the rules for setting up a Pascal-S job deck identical to those for all
other jobs, particularly normal Pascal jobs. The reader is referred to Jensen and
Wirth [1], manual section 13.A.1, for an explanation of the procedure getseg and
the predicate eos.

   But even when strictly adhering to a machine-independent language such as
standard Pascal, a second kind of machine-dependent consideration creeps in if a
program is carefully planned. I shall call them second-order machine-dependencies.
They are due to the use of knowledge about limitations and characteristics of the
underlying system and the desire to use it optimally. They may cause another
implementation to reject the program (if its limitations are more severe) or merely
to process the program less efficiently. The first category concerns, for instance, the
range of available integers or-much more problematic in Pascal-the size of allowable
sets. The second category includes considerations of storage structure. In the
present system program, such considerations played an important role in achieving
high efficiency and economy, and are manifested in the choice of several constants
which are all defmed in the beginning of the program. The choice of these constants,
explained below, must be reconsidered if Pascal-S is reimplemented on a different
computer, be it by hand translation or by recompilation through an already
available Pascal compiler. (The reader not concerned with this problem may easily
skip the rest of this section.)

alng             defines the number of characters in the array type alfa. In a
                 word-oriented computer the choice of this value is critical, and
                 should be the number of characters packable into a word or a
                 small number of words.
ling             defines the maximum length of an input line delivered by the
                 operating system.
emax             is the maximum value of the decimal exponent of a real
                 number acceptable by the computer. emax = log(maxreal).
emin             is the minimum value of the decimal exponent. (Smaller numbers
                 are considered as identical to O. For most computers emin
                 = -emax.)
kmax             is the number of significant digits in a real number, i.e. kmax =
                 m log 2, if m is the number of bits of the mantissa of the
                 binary floating-point number.
ermax            is the number of error messages available. It is chosen such
                 that type e"s was acceptable to the available Pascal compiler.
omax,lmax,nmax   were chosen such that the packed record type order could be
                 represented in a single 60-bit word of the CDC computer.
xmax             is chosen such that the sub range type index occupies a reasonably
                 small part of a word to provide high storage economy in
                 packed tables, yet encompasses a sufficiently large subset of
                 the integers to cover all array-index values.
lineleng         is equal to the maximum number of characters permissable in
                 a line to be printed.

   Further comments about second-order machine-dependencies follow. The type
order allows for negative values of components f and x, although this is not needed.
The reason is that access to signed fields of packed records is more efficient in the
Pascal 6000-3.4 system, and because these accesses are very frequent in the interpreter.
The number of basic symbols much be such that the type symset is acceptable to
the available Pascal compiler. The array sps is a constant table used by the scanner.
Its index range must be such that it covers all characters which are neither letters
nor digits. For the sake of character set independence, its range is indicated as being
the entire character set.

   The set constants used in the scanner depend on the assertion that

ch in ['a'..'z'] = ch is a letter
ch in ['0'..'9'] = ch is a digit

   Most available character sets satisfy these equivalences. Several statements in the
scanner rely on them by computing the numeric value of a digit x as ord(x )-ord
('0'). This is the only r~quirement imposed on the ordering of character sets. The
present version of Pascal-S assumes the use of the restricted ASCII character set.
Unused characters cause an error indication when encountered.

   In the interpreter, the functions chr and ord are implemented as dummy procedures,
because we postulate for characters the ordering given by the collating
sequence of the given character set (thereby accepting an implicit machine-dependence).
The constants 0 and 63 are the ordinal numbers of the first and the last
character in the given set.

   Note that an element in the stack may represent a value of any of the four standard
types integer, real, boolean, or char. This implies that the same amount of
storage is allocated for values of these types. This results in uneconomical utilization
of store, particularly for Boolean and character values, but it simplifies both
compiler and interpreter considerably. With regard to real numbers, a floating-point
representation should be chosen that uses the same number of words or bytes as
integers, since a very high numerical precision is usually not required in the types of
problems for which Pascal-S is intended.

   A particular problem of practical importance is the regaining of control in the
case of a trap performed by the hardware or the underlying operating system. No
solution for this problem is indicated here, as it is inherently dependent on the
environment.

                       7. The compiler-interpreter program

program Pascals(input+,output);  (* 1.6.75 *)            
(*        N. Wirth, E.T.H
                    CH-8092 Zurich      *)
label 99;
const nkw   =   27;      (* no. of key words *)
      alng  =   10;      (* no. of significant chars in identifiers *)
      llng  =  120;      (* input line length *)
      emax  =  322;      (* max exponent of real numbers *)
      emin  = -292;      (* min exponent *)
      kmax  =   15;      (* max no. of significant digits *)
      tmax  =  100;      (* size of table *)
      bmax  =   20;      (* size of block-table *)
      amax  =   30;      (* size of array-table *)
      c2max =   20;      (* size of real constant table *)
      csmax =   30;      (* max no. of cases *)
      cmax  =  850;      (* size of code *)
      lmax  =    7;      (* maximum level *)
      smax  =  600;      (* size of string table *)
      ermax =   58;      (* max error no. *)
      omax  =   63;      (* highest order code *)
      xmax  = 131071;    (* 2**17 - 1 *) 
      nmax  = 281474976710655; (* 2**48 - 1 *) 
      lineleng = 136;    (* output line length *)
      linelimit = 200;
      stacksize = 1500;

type  symbol = (intcon, realcon, charcon, stringt,
                notsy, plus, minus, times, idiv, rdiv, imod, andsy, orsy,
                egl, neg, gtr, geg, lss, leg,
                lparent, rparent, lbrack, rbrack, comma, semicolon, period,
                colon, becomes, constsy, typesy, varsy, functionsy,
                proceduresy, arraysy, recordsy, programsy, ident,
                beginsy, ifsy, casesy, repeatsy, whilesy, forsy,
                endsy, elsesy, untilsy, ofsy, dosy, tosy, downtosy, thensy);

       index = -xmax .. +xmax;
       alfa = packed array [1..alng] of char;
       object = (konstant, variable, typel, prozedure, funktion);
       types = (notyp, ints, reals, bools, chars, arrays, records);
       symset = set of symbol;
       typset = set of types;
       item = record
                 typ: types; ref: index;
              end;
       order = packed record
                  f: -omax..+omax;
                  x: -lmax..+lmax;
                  y: -nmax..+nmax;
               end;

var    sy: symbol;         (* last symbol read by insymbol *)
       id: alfa;           (* identifier from insymbol *)
       inum: integer;      (* integer from insymbol *)
       rnum: real;         (* real number from insymbol *)
       sleng: integer;     (* string length *)
       ch: char;           (* last character read from source program *)
       line: array [1..llng] of char;
       cc: integer;        (* character counter *)
       lc: integer;        (* program location counter *)
       ll: integer;        (* length of current line *)
       errs: set of 0..ermax;
       errpos: integer;
       progname: alfa;
       iflag, oflag: boolean;
       constbegsys, typebegsys, blockbegsys, facbegsys, statbegsys: symset;
       key: array [1..nkw] of alfa;
       ksy: array [1..nkw] of symbol;
       sps: array [char] of symbol; (* special symbols *)

       t, a, b, sx, c1, c2: integer; (* indicies to tables *)
       stantyps: typset;
       display: array [0..lmax] of integer;

       tab: array [0..tmax] of     (* identifier table *)
               packed record
                  name: alfa; link: index;
                  obj: object; typ: types;
                  ref: index; normal: boolean;
                  lev: 0..lmax; adr: integer;
               end;
       atab: array [1..amax] of    (* array-table *)
                packed record
                   inxtyp, eltyp: types;
                   elref, low, high, elsize, size: index;
                end;
       btab: array [1..bmax] of    (* block table *)
                packed record
                   last, lastpar, psize, vsize: index
                end;
       stab: packed array [0..smax] of char; (* string table *)
       rconst: array [1..c2max] of real;
       code: array [0..cmax] of order;

procedure errormsg;
   var k: integer;
       msg: array [0..ermax] of alfa;

begin
   msg[ 0] := 'undef id  '; msg[ 1] := 'multi def ';
   msg[ 2] := 'identifier'; msg[ 3] := 'program   ';
   msg[ 4] := ')         '; msg[ 5] := ':         ';
   msg[ 6] := 'syntax    '; msg[ 7] := 'ident, var';
   msg[ 8] := 'of        '; msg[ 9] := '(         ';
   msg[10] := 'id, array '; msg[11] := '[         ';
   msg[12] := ']         '; msg[13] := '..        ';
   msg[14] := ';         '; msg[15] := 'func. type';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -