-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.pas
More file actions
2873 lines (2785 loc) · 109 KB
/
Copy pathmain.pas
File metadata and controls
2873 lines (2785 loc) · 109 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
program PASCALS(INPUT,OUTPUT,PRD,PRR);
{* author:N.Wirth, E.T.H. CH-8092 Zurich,1.3.76 *}
{ modified by R.E.Berry
Department of computer studies
University of Lancaster
Variants of this program are used on
Data General Nova,Apple,and
Western Digital Microengine machines. }
{ further modified by M.Z.Jin
Department of Computer Science&Engineering BUAA,0ct.1989
}
const nkw = 27; {* no. of key words *}
alng = 10; {* no. of significant chars in identifiers *}
llng = 121; {* 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-talbe *}
amax = 30; {* size of array-table *}
c2max = 20; {* size of real constant table *}
csmax = 30; {* max no. of cases *}
cmax = 800; {* size of code *}
lmax = 7; {* maximum level *}
smax = 600; {* size of string-table *}
ermax = 58; {* max error no. *}
omax = 63; {* highest order code *}
xmax = 32767; {* 2**15-1 *}
nmax = 32767; {* 2**15-1 *}
lineleng = 132; {* output line length *}
linelimit = 200;
stacksize = 1450;
type symbol = ( intcon, realcon, charcon, stringcon,
notsy, plus, minus, times, idiv, rdiv, imod, andsy, orsy,
eql, neq, gtr, geq, lss, leq,
lparent, rparent, lbrack, rbrack, comma, semicolon, period,
colon, becomes, constsy, typesy, varsy, funcsy,
procsy, 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;
objecttyp = (konstant, vvariable, typel, prozedure, funktion );
types = (notyp, ints, reals, bools, chars, arrays, records );
symset = set of symbol; {*代表符号表的集合,在block中常用*}
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 ch: char; {* last character read from source program *}
rnum: real; {* real number from insymbol *}
inum: integer; {* integer from insymbol *}
sleng: integer; {* string length *}
cc: integer; {* character counter *}
lc: integer; {* program location counter *}
ll: integer; {* length of current line *}
errpos: integer;
t,a,b,sx,c1,c2:integer; {* indices to tables *}
iflag, oflag, skipflag, stackdump, prtables: boolean;
sy: symbol; {* last symbol read by insymbol *}
errs: set of 0..ermax;
id: alfa; {* identifier from insymbol *}
progname: alfa;
stantyps: typset;
constbegsys, typebegsys, blockbegsys, facbegsys, statbegsys: symset;
line: array[1..llng] of char;
key: array[1..nkw] of alfa;
ksy: array[1..nkw] of symbol;
sps: array[char]of symbol; {* special symbols *}
display: array[0..lmax] of integer;
tab: array[0..tmax] of {* indentifier lable *}
packed record
name: alfa;
link: index;
obj: objecttyp;
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;
psin,psout,prr,prd:text; {* default in pascal p *}
inf, outf, fprr: string;
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';
msg[16] := '= '; msg[17] := 'boolean ';
msg[18] := 'convar typ'; msg[19] := 'type ';
msg[20] := 'prog.param'; msg[21] := 'too big ';
msg[22] := '. '; msg[23] := 'type(case)';
msg[24] := 'character '; msg[25] := 'const id ';
msg[26] := 'index type'; msg[27] := 'indexbound';
msg[28] := 'no array '; msg[29] := 'type id ';
msg[30] := 'undef type'; msg[31] := 'no record ';
msg[32] := 'boole type'; msg[33] := 'arith type';
msg[34] := 'integer '; msg[35] := 'types ';
msg[36] := 'param type'; msg[37] := 'variab id ';
msg[38] := 'string '; msg[39] := 'no.of pars';
msg[40] := 'real numbr'; msg[41] := 'type ';
msg[42] := 'real type '; msg[43] := 'integer ';
msg[44] := 'var,const '; msg[45] := 'var,proc ';
msg[46] := 'types(:=) '; msg[47] := 'typ(case) ';
msg[48] := 'type '; msg[49] := 'store ovfl';
msg[50] := 'constant '; msg[51] := ':= ';
msg[52] := 'then '; msg[53] := 'until ';
msg[54] := 'do '; msg[55] := 'to downto ';
msg[56] := 'begin '; msg[57] := 'end ';
msg[58] := 'factor';
writeln(psout);
writeln(psout,'key words');
k := 0;
while errs <> [] do
begin
while not( k in errs )do k := k + 1;
writeln(psout, k, ' ', msg[k] );
errs := errs - [k]
end {* while errs *}
end {* errormsg *} ;
procedure endskip;
begin {* underline skipped part of input *}
while errpos < cc do
begin
write( psout, '-'); {*在错误的代码下用-来标识*}
errpos := errpos + 1
end;
skipflag := false
end {* endskip *};
procedure nextch; {* read next character; process line end *}
begin
if cc = ll
then begin
if eof( psin )
then begin
writeln( psout );
writeln( psout, 'program incomplete' );
errormsg;
exit;
end;
if errpos <> 0
then begin
if skipflag then endskip;
writeln( psout );
errpos := 0
end;
write( psout, lc: 5, ' ');
ll := 0;
cc := 0;
while not eoln( psin ) do
begin
ll := ll + 1;
read( psin, ch );
write( psout, ch );
line[ll] := ch
end;
ll := ll + 1;
readln( psin );
line[ll] := ' ';
writeln( psout );
end;
cc := cc + 1;
ch := line[cc];
end {* nextch *};
procedure error( n: integer );
begin
if errpos = 0
then write ( psout, '****' );
if cc > errpos
then begin
write( psout, ' ': cc-errpos, '^', n:2);
errpos := cc + 3;
errs := errs +[n]
end
end {* error *};
procedure fatal( n: integer );
var msg : array[1..7] of alfa;
begin
writeln( psout );
errormsg;
msg[1] := 'identifier'; msg[2] := 'procedures';
msg[3] := 'reals '; msg[4] := 'arrays ';
msg[5] := 'levels '; msg[6] := 'code ';
msg[7] := 'strings ';
writeln( psout, 'compiler table for ', msg[n], ' is too small');
exit; {*terminate compilation *}
end {* fatal *};
procedure insymbol; {*reads next symbol*}
label 1,2,3;
var i,j,k,e: integer;
procedure readscale; {*处理2e5即2*10^5这种科学计数法的e后面的数据*}
var s,sign: integer;
begin
nextch;
sign := 1; {*符号默认为正号,即不出现正号就代表是正号*}
s := 0;
if ch = '+'
then nextch
else if ch = '-'
then begin
nextch;
sign := -1 {*标记上为负数*}
end;
if not(( ch >= '0' )and (ch <= '9' ))
then error( 40 )
else repeat
s := 10*s + ord( ord(ch)-ord('0'));
nextch;
until not(( ch >= '0' ) and ( ch <= '9' ));
e := s*sign + e
end {* readscale *};
procedure adjustscale; {*计算出科学计数法的实际数据*}
var s : integer;
d, t : real;
begin
if k + e > emax {*e前后的数据长度和不能超过emax*}
then error(21)
else if k + e < emin {*如果比emin(-292)还要小,已经超出精度范围了,直接赋值为0*}
then rnum := 0
else begin {*快速幂求10^s,即不停地除2*}
s := abs(e);
t := 1.0;
d := 10.0;
repeat
while not odd(s) do
begin
s := s div 2;
d := sqr(d)
end;
s := s - 1;
t := d * t
until s = 0;
if e >= 0 then {*如果指数是正数,那么就直接相乘*}
rnum := rnum * t
else rnum := rnum / t {*否则就相除*}
end
end {* adjustscale *};
procedure options;
procedure switch( var b: boolean );
begin
b := ch = '+';
if not b
then if not( ch = '-' )
then begin {* print error message *}
while( ch <> '*' ) and ( ch <> ',' ) do
nextch;
end
else nextch
else nextch
end {* switch *};
begin {* options *}
repeat
nextch;
if ch <> '*'
then begin
if ch = 't'
then begin
nextch;
switch( prtables )
end
else if ch = 's'
then begin
nextch;
switch( stackdump )
end;
end
until ch <> ','
end {* options *};
begin {* insymbol *}
1: while( ch = ' ' ) or ( ch = chr(9) ) do {*遇到空格或者指标符就继续读下一个字符*}
nextch; {* space & htab *}
case ch of
'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z':
begin {* identifier of wordsymbol *}
k := 0;
id := ' ';
repeat {*一直读到不是数字或者小写字符为止*}
if k < alng {*规定了标识符的长度不能长于alng,即不能长于10*}
then begin
k := k + 1;
id[k] := ch
end;
nextch
until not((( ch >= 'a' ) and ( ch <= 'z' )) or (( ch >= '0') and (ch <= '9' )));
i := 1;
j := nkw; {* binary search *}
repeat {*下面这段是通过二分查找找到对应的那个key*}
k := ( i + j ) div 2;
if id <= key[k]
then j := k - 1;
if id >= key[k]
then i := k + 1;
until i > j;
if i - 1 > j
then sy := ksy[k]
else sy := ident
end;
'0','1','2','3','4','5','6','7','8','9': {*下面这段是找出数字并把它存到inum里面,长度为k*}
begin {* number *}
k := 0;
inum := 0;
sy := intcon;
repeat
inum := inum * 10 + ord(ch) - ord('0');
k := k + 1;
nextch
until not (( ch >= '0' ) and ( ch <= '9' ));
if( k > kmax ) or ( inum > nmax ) {*数字的长度不能15,数字的大小不能大于32767*}
then begin
error(21);
inum := 0;
k := 0
end;
if ch = '.' {*处理实数部分,即n.m这种*}
then begin
nextch;
if ch = '.'
then ch := ':' {*..用法,即array[1..10]这种写法*}
else begin
sy := realcon; {*注明这个数是实数*}
rnum := inum; {*先赋值为inum即整数部分*}
e := 0;
while ( ch >= '0' ) and ( ch <= '9' ) do
begin
e := e - 1;
rnum := 10.0 * rnum + (ord(ch) - ord('0'));
nextch
end;
if e = 0
then error(40);
if ch = 'e'
then readscale; {*处理底数是实数例如2.3e10这种科学计数法的指数*}
if e <> 0 then adjustscale {*求出这个科学计数法的具体数据*}
end
end
else if ch = 'e' {*处理底数是整数的科学计数法*}
then begin
sy := realcon;
rnum := inum;
e := 0;
readscale;
if e <> 0
then adjustscale
end;
end;
':': {*处理冒号*}
begin
nextch;
if ch = '=' {*代表是赋值号*}
then begin
sy := becomes; {*赋值号*}
nextch
end
else sy := colon {*单纯的冒号,例如定义*}
end;
'<':
begin
nextch;
if ch = '='
then begin
sy := leq; {*小于等于号*}
nextch
end
else
if ch = '>'
then begin
sy := neq;
nextch
end
else sy := lss
end;
'>':
begin
nextch;
if ch = '='
then begin
sy := geq; {*大于等于号*}
nextch
end
else sy := gtr
end;
'.':
begin
nextch;
if ch = '.'
then begin
sy := colon; {*..,例如array里面的范围*}
nextch
end
else sy := period {*单纯的点号*}
end;
'''':
begin
k := 0;
2: nextch;
if ch = ''''
then begin
nextch;
if ch <> ''''
then goto 3 {*代表是单个字符即不是字符串*}
end;
if sx + k = smax {*超过了字符串的长度限制*}
then fatal(7);
stab[sx+k] := ch; {*保存到字符串常量表里*}
k := k + 1;
if cc = 1
then begin {* end of line *}
k := 0;
end
else goto 2;
3: if k = 1
then begin
sy := charcon; {*单个字符*}
inum := ord( stab[sx] )
end
else if k = 0 {*单个引号,报错*}
then begin
error(38);
sy := charcon; {*容错,代表是单个字符*}
inum := 0
end
else begin
sy := stringcon; {*代表是字符串*}
inum := sx; {*字符串出现的起始位置*}
sleng := k; {*字符串的长度}
sx := sx + k {*所有的字符的个数*}
end
end;
'(':
begin
nextch;
if ch <> '*' {*不是(**}
then sy := lparent
else begin {* comment *}
nextch;
if ch = '$' {*($的写法,是编译的pascal文件的参数*}
then options;
repeat
while ch <> '*' do nextch; {*一直处理到**}
nextch
until ch = ')'; {*最后应该是个)*}
nextch;
goto 1 {*返回到1继续处理*}
end
end;
'{':
begin
nextch;
if ch = '$' {*处理程序的参数*}
then options;
while ch <> '}' do
nextch;
nextch;
goto 1
end;
'+', '-', '*', '/', ')', '=', ',', '[', ']', ';':
begin
sy := sps[ch];
nextch
end;
'$','"' ,'@', '?', '&', '^', '!':
begin
error(24);
nextch;
goto 1
end
end {* case *}
end {* insymbol *};
procedure enter(x0:alfa; x1:objecttyp; x2:types; x3:integer );
begin
t := t + 1; {* enter standard identifier *}
with tab[t] do {*把当前标识符t的信息录入到符号表里*}
begin
name := x0;
link := t - 1; {*指向前一个标识符*}
obj := x1; {*种类*}
typ := x2; {*类型*}
ref := 0;
normal := true; {*这个变量除了变参都是true*}
lev := 0;
adr := x3; {*在运行栈S中分配存储单元的相对地址*}
end
end; {* enter *}
procedure enterarray( tp: types; l,h: integer ); {*记录数组信息报atab*}
begin
if l > h {*下界显然要小于上界*}
then error(27);
if( abs(l) > xmax ) or ( abs(h) > xmax ) {*不允许超过大小限制*}
then begin
error(27);
l := 0;
h := 0;
end;
if a = amax
then fatal(4)
else begin
a := a + 1; {*表里元素的个数增加*}
with atab[a] do
begin
inxtyp := tp;
low := l; {*atab[a].low = l;*}
high := h
end
end
end {* enterarray *};
procedure enterblock; {*进入下一个分程序块*}
begin
if b = bmax {*只允许有至多20个分程序块*}
then fatal(2)
else begin
b := b + 1;
btab[b].last := 0; {*指向这个过程最后一个符号在符号表的位置*}
btab[b].lastpar := 0; {*指向这个过程的最后一个参数在符号表的位置*}
end
end {* enterblock *};
procedure enterreal( x: real ); {填实型变量}
begin
if c2 = c2max - 1
then fatal(3)
else begin
rconst[c2+1] := x;
c1 := 1;
while rconst[c1] <> x do {*实常量表中不允许出现重复的项,因此找前面有没有出现过x*}
c1 := c1 + 1;
if c1 > c2 {*注意在这里我们没有更新inc(c2), 因此直接通过c1来更新,c1最大是c2+1*}
then c2 := c1
end
end {* enterreal *};
procedure emit( fct: integer ); {*生成PCode方法,没有操作数*}
begin
if lc = cmax {*超出了生成代码的数量限制,即800*}
then fatal(6);
code[lc].f := fct; {*操作码等于fct*}
lc := lc + 1 {*代表生成的指令数增加一个*}
end {* emit *};
procedure emit1( fct, b: integer ); {*生成PCode方法,有一个操作数*}
begin
writeln('fct = ',fct,' b= ',b);
if lc = cmax {*超出了生成代码的数量限制,即800*}
then fatal(6);
with code[lc] do {*修改P代表表code的记录值*}
begin
f := fct;
y := b; {*???是说如果只有一个操作数,只填到y吗*}
end;
lc := lc + 1 {*代表生成的指令数增加一个*}
end {* emit1 *};
procedure emit2( fct, a, b: integer ); {*生成PCode方法,有2个操作数*}
begin
if lc = cmax then fatal(6); {*超出了生成代码的数量限制,即800*}
with code[lc] do {*修改P代表表code的记录值*}
begin
f := fct;
x := a; {*x操作数*}
y := b {*y操作数*}
end;
lc := lc + 1; {*代表生成的指令书增加一个*}
end {* emit2 *};
procedure printtables; {*打印Pcode表*}
var i: integer;
o: order;
mne: array[0..omax] of
packed array[1..5] of char;
begin {*下面是各种操作码对应的助记符,即动作*}
mne[0] := 'LDA '; mne[1] := 'LOD '; mne[2] := 'LDI ';
mne[3] := 'DIS '; mne[8] := 'FCT '; mne[9] := 'INT ';
mne[10] := 'JMP '; mne[11] := 'JPC '; mne[12] := 'SWT ';
mne[13] := 'CAS '; mne[14] := 'F1U '; mne[15] := 'F2U ';
mne[16] := 'F1D '; mne[17] := 'F2D '; mne[18] := 'MKS ';
mne[19] := 'CAL '; mne[20] := 'IDX '; mne[21] := 'IXX ';
mne[22] := 'LDB '; mne[23] := 'CPB '; mne[24] := 'LDC ';
mne[25] := 'LDR '; mne[26] := 'FLT '; mne[27] := 'RED ';
mne[28] := 'WRS '; mne[29] := 'WRW '; mne[30] := 'WRU ';
mne[31] := 'HLT '; mne[32] := 'EXP '; mne[33] := 'EXF ';
mne[34] := 'LDT '; mne[35] := 'NOT '; mne[36] := 'MUS ';
mne[37] := 'WRR '; mne[38] := 'STO '; mne[39] := 'EQR ';
mne[40] := 'NER '; mne[41] := 'LSR '; mne[42] := 'LER ';
mne[43] := 'GTR '; mne[44] := 'GER '; mne[45] := 'EQL ';
mne[46] := 'NEQ '; mne[47] := 'LSS '; mne[48] := 'LEQ ';
mne[49] := 'GRT '; mne[50] := 'GEQ '; mne[51] := 'ORR ';
mne[52] := 'ADD '; mne[53] := 'SUB '; mne[54] := 'ADR ';
mne[55] := 'SUR '; mne[56] := 'AND '; mne[57] := 'MUL ';
mne[58] := 'DIV '; mne[59] := 'MOD '; mne[60] := 'MUR ';
mne[61] := 'DIR '; mne[62] := 'RDL '; mne[63] := 'WRL ';
writeln(psout); {*写入到psout文件里去*}
writeln(psout);
writeln(psout);
writeln(psout,' identifiers link obj typ ref nrm lev adr');
writeln(psout);
for i := btab[1].last to t do {*不需要输出0..btab[1].last,因为btab[1].last也就是28也就是把所有Pascal自带类型都存进去的位置,这个后面就是我们自己定义的变量了*}
with tab[i] do
writeln( psout, i,' ', name, link:5, ord(obj):5, ord(typ):5,ref:5, ord(normal):5,lev:5,adr:5);
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'blocks last lpar psze vsze' );
writeln( psout );
for i := 1 to b do {*输出所有block的分程序表*}
with btab[i] do
writeln( psout, i:4, last:9, lastpar:5, psize:5, vsize:5 );
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'arrays xtyp etyp eref low high elsz size');
writeln( psout );
for i := 1 to a do {*输出所有的数组信息的向量表*}
with atab[i] do
writeln( psout, i:4, ord(inxtyp):9, ord(eltyp):5, elref:5, low:5, high:5, elsize:5, size:5);
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'code:');
writeln( psout );
for i := 0 to lc-1 do {*打印最终的所有Pcode*}
begin
write( psout, i:5 );
o := code[i];
write( psout, mne[o.f]:8, o.f:5 );
if o.f < 31 {*操作码大于等于31的都代表是无操作数的操作*}
then if o.f < 4 {*小于4的操作码代表是所有*}
then write( psout, o.x:5, o.y:5 )
else write( psout, o.y:10 )
else write( psout, ' ' );
writeln( psout, ',' )
end;
writeln( psout );
writeln( psout, 'Starting address is ', tab[btab[1].last].adr:5 ) {*输出Pcode程序的起始地址,相当于主函数begin下的第一句的起始地址,因为生成的PCode的顺序是按照程序的顺序的,即从上而下生成,而上面可能有很多函数这样。*}
{*之前有tab[prt].adr := lc;就是把这一函数或者过程(即block)的起始地址是哪一句Pcode,例如tab[28]=11就是第一个block也就是主程序的入口*}
end {* printtables *};
procedure block( fsys: symset; isfun: boolean; level: integer );
type conrec = record
case tp: types of
ints, chars, bools : ( i:integer );
reals :( r:real )
end;
var dx : integer ; {* data allocation index *}
prt: integer ; {* t-index of this procedure *}
prb: integer ; {* b-index of this procedure *}
x : integer ;
procedure skip( fsys:symset; n:integer); {*第n号错误,处理错误的函数,跳过错误的这些代码*}
begin
error(n);
skipflag := true;
while not ( sy in fsys ) do {*把所有不在fsys里的标识符全跳过去*}
insymbol;
if skipflag then endskip
end {* skip *};
procedure test( s1,s2: symset; n:integer ); {*检查当前符号sy是否合法*}
begin
if not( sy in s1 ) {*sy在s1中就是合法的*}
then skip( s1 + s2, n )
end {* test *};
procedure testsemicolon; {*检查分号是否合法*}
begin
if sy = semicolon {*如果是分号就合法*}
then insymbol
else begin
error(14);
if sy in [comma, colon] {*!!!如果是逗号或者句号,进行一定的容错处理*}
then insymbol
end;
test( [ident] + blockbegsys, fsys, 6 )
end {* testsemicolon *};
procedure enter( id: alfa; k:objecttyp ); {*将标识符id填入符号表*}
var j,l : integer;
begin
if t = tmax {*符号表已经填满了,即100个*}
then fatal(1)
else begin
tab[0].name := id; {*临时存在第0号符号表里,马上会用到*}
j := btab[display[level]].last; {*当前层次中最后一个标识符在符号表的位置*}
l := j;
while tab[j].name <> id do {*一直往前找,只要和id相等就停下来*}
j := tab[j].link;
if j <> 0 {*说明已经出现过了,报错*}
then error(1)
else begin {*加入到符号表里*}
t := t + 1; {*标识符数量增加一个*}
with tab[t] do {*填写这个标识符的记录信息*}
begin
name := id; {*id是传进来的参数*}
link := l; {*指向之前的最后一个标识符*}
obj := k; {*这个标识符的种类,代表常量、变量、类型、过程和函数*}
typ := notyp; {*具体的类型,例如整型、实型等等*}
ref := 0; {*最普通的标识符,ref为0*}
lev := level; {*标识符所在程序的层次*}
adr := 0;
normal := false {* initial value } {???*}
end;
btab[display[level]].last := t {*当前层的最后一个标识符的位置变了*}
end
end
end {* enter *};
function loc( id: alfa ):integer; {*查找id在符号表的位置在哪里*}
var i,j : integer; {* locate if in table *}
begin
i := level;
tab[0].name := id; {* sentinel *}
repeat
j := btab[display[i]].last; {*从这个块的最后一个变量开始*}
while tab[j].name <> id do
j := tab[j].link; {*不停地向前匹配是不是名字一样*}
i := i - 1; {保证可以查过所有的标识符,包括类型名(相当于都在level为0的里面)}
until ( i < 0 ) or ( j <> 0 );
if j = 0 {*没有找到这个标识符,说明定义的类型不存在,报错*}
then error(0);
loc := j
end {* loc *} ;
procedure entervariable; {*将变量填入符号表*}
begin
if sy = ident
then begin
enter( id, vvariable ); {*主要是放到了enter里面去执行*}
insymbol
end
else error(2)
end {* entervariable *};
procedure constant( fsys: symset; var c: conrec ); {*处理常量,变参c用于返回常量的信息*}
var x, sign : integer;
begin
c.tp := notyp; {*常量类型是无变量类型*}
c.i := 0;
test( constbegsys, fsys, 50 );
if sy in constbegsys {*首先出现的是const,继续分析*}
then begin
if sy = charcon {*对于字符类型的常量*}
then begin
c.tp := chars; {*类型是char*}
c.i := inum; {*存储它的ascii码值*}
insymbol
end
else begin
sign := 1; {*非char类型的常量*}
if sy in [plus, minus] {*有前导的正号或者负号*}
then begin
if sy = minus {*如果有-号就变号*}
then sign := -1;
insymbol
end;
if sy = ident {*接下来是标识符*}
then begin
x := loc(id); {*在符号表中找一下,例如const N=M, M是之前定义的常量. ???没有找到是没有处理吗*}
if x <> 0
then
if tab[x].obj <> konstant {*如果对应的不是常量类型,那么就报错*}
then error(25)
else begin
c.tp := tab[x].typ; {*读取这个常量的类型*}
if c.tp = reals
then c.r := sign*rconst[tab[x].adr] {*实型就从rconst里面去读*}
else c.i := sign*tab[x].adr {*整形就从符号表的adr里去读*}
end;
insymbol
end
else if sy = intcon {*读入到整数*}
then begin
c.tp := ints; {*整型*}
c.i := sign*inum;
insymbol
end
else if sy = realcon {*读入到实数*}
then begin
c.tp := reals; {*实型*}
c.r := sign*rnum;
insymbol
end
else skip(fsys,50) {*跳过无用符号*}
end;
test(fsys,[],6)
end
end {* constant *};
procedure typ( fsys: symset; var tp: types; var rf,sz:integer );
var eltp : types;
elrf, x : integer;
elsz, offset, t0, t1 : integer;
procedure arraytyp( var aref, arsz: integer ); {*处理数组类型*}
var eltp : types; {*数组保存的是什么类型的变量*}
low, high : conrec; {*下界和上界,例如array[1..100]的下界为1,上界为100*}
elrf, elsz: integer; {*数组元素的大小和改数组的大小*}
begin
constant( [colon, rbrack, rparent, ofsy] + fsys, low); {*求数组的下界*}
if low.tp = reals {*下界的类型是实型,报错*}
then begin
error(27);
low.tp := ints; {*容错,把它设为整形*}
low.i := 0 {*容错,下界为0*}
end;
if sy = colon {*下界之后跟着的是.. 类型是句号???这里的colon是..还是.*}
then insymbol
else error(13);
constant( [rbrack, comma, rparent, ofsy ] + fsys, high );
if high.tp <> low.tp {*上下界类型不一样,报错*}
then begin
error(27);
high.i := low.i {*容错,使得上界类型和下界是一样的*}
end;
enterarray( low.tp, low.i, high.i ); {*将数组类型的信息填入到符号表里*}
aref := a; {*找到在atab中的位置*}
if sy = comma {*遇到逗号了,即例如array[1..100,1..100]这样的二维数组*}
then begin
insymbol;
eltp := arrays;
arraytyp( elrf, elsz ) {*递归调用该过程处理数组的元素,即每一维都放到数组符号表里*}
end
else begin
if sy = rbrack {*遇到右中括号了*}
then insymbol
else begin
error(12);
if sy = rparent {*右括号就容错*}
then insymbol
end;
if sy = ofsy {*遇到了of*}
then insymbol
else error(8);
typ( fsys, eltp, elrf, elsz) {*处理后面的typ类型*}
end;
with atab[aref] do {*填这个数组的信息*}
begin
arsz := (high-low+1) * elsz; {*计算数组的大小*}
size := arsz; {*保存到atab[size]里*}
eltyp := eltp; {*保存数组的元素是什么类型*}
elref := elrf; {*保存数组从哪儿开始登陆的, 之前的typ过程的elrf是变参*}
elsize := elsz {*保存每个元素的大小*}
end
end {* arraytyp *};
begin {* typ } {处理类型的开始*}
tp := notyp; {*存储该符号是什么类型,默认是无类型变量*}
rf := 0; {*用来存储该符号在符号表的位置*}
sz := 0; {*用来存储该类型的大小*}
test( typebegsys, fsys, 10 ); {*检测是不是数组、记录或者标识符类型*}
if sy in typebegsys
then begin
if sy = ident {*如果是标识符类型*}
then begin
x := loc(id); {*找在符号表的位置*}
if x <> 0 {*如果找到了*}
then with tab[x] do {*将x的信息保存到变参里*}
if obj <> typel
then error(29)
else begin
tp := typ; {*相当于tp := tab[x].typ;*}
rf := ref; {*相当于rf := tab[x].ref;*}
sz := adr; {*相当于sz := tab[x].adr;*}
if tp = notyp {*如果是未定义的类型,则报错*}
then error(30)
end;
insymbol
end
else if sy = arraysy {*如果是数组类型的标识符*}
then begin
insymbol;
if sy = lbrack {*遇到左中括号,读入下一个标识符*}
then insymbol
else begin
error(11);
if sy = lparent {*容错,对于左小括号*}
then insymbol
end;
tp := arrays; {*tp是数组类型的变量*}
arraytyp(rf,sz) {*获得数组在数组符号表的位置和数组的大小*}
end
else begin {* records } {处理是记录类型的标识符} {例如item = record typ: types; ref: index; end;*}
insymbol;
enterblock;
tp := records;
rf := b; {*???rf指向当前过程在符号表的位置*}
if level = lmax {*当前嵌套已经到了lmax就不能再嵌套处理了*}
then fatal(5);
level := level + 1; {*继续嵌套*}
display[level] := b; {*设置当前层次的display区*}
offset := 0;
while not ( sy in fsys - [semicolon,comma,ident]+ [endsy] ) do
begin {* field section *}
if sy = ident
then begin
t0 := t; {*将t0赋值为当前符号的位置*}
entervariable; {*把变量填入符号表*}
while sy = comma do {*同种变量间是用逗号隔开的*}
begin
insymbol;
entervariable
end;
if sy = colon {*如果是冒号,这个变量都读完了,读类型*}
then insymbol
else error(5);
t1 := t;
typ( fsys + [semicolon, endsy, comma,ident], eltp, elrf, elsz ); {*递归调用typ处理记录类型的其它成员变量*}
while t0 < t1 do {*填写t0~t1中的信息*}
begin
t0 := t0 + 1;
with tab[t0] do
begin
typ := eltp; {*eltp是之前递归调用的typ的变参*}
ref := elrf; {*elrf..,*}
normal := true; {*给normal标记复制*}
adr := offset; {*相对于起始地址的位移*}
offset := offset + elsz {*下一个变量的唯一*}
end
end
end; {* sy = ident *}
if sy <> endsy {*遇到end则代表records的成员声明结束了*}
then begin
if sy = semicolon {*end后面必须跟分号*}
then insymbol
else begin
error(14);
if sy = comma {*容错,允许是逗号*}
then insymbol
end;
test( [ident,endsy, semicolon],fsys,6 )
end
end; {* field section *}
btab[rf].vsize := offset; {*???*}
sz := offset; {*储存了它占用空间的总数*}
btab[rf].psize := 0; {*records没有参数占用空间*}
insymbol;
level := level - 1 {*level结束后level减少1*}
end; {* record *}
test( fsys, [],6 )
end;
end {* typ *};
procedure parameterlist; {* formal parameter list } {处理参数列表*}
var tp : types;
valpar : boolean;
rf, sz, x, t0 : integer;
begin
insymbol;
tp := notyp; {*初始化 下同*}
rf := 0;
sz := 0;
test( [ident, varsy], fsys+[rparent], 7 );
while sy in [ident, varsy] do {*一直是标识符或者var就一直进行*}
begin
if sy <> varsy
then valpar := true {*值形参就填值*}