-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.pas
467 lines (434 loc) · 8.53 KB
/
scanner.pas
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
unit scanner;
{PASCAL- COMPILER : Scanner
Copyright (c) 1984 Per Brinch Hansen
Free pascal adaptation (c) Jari Korhonen, 2023}
interface
uses
Classes, SysUtils;
procedure Scan(sourcefile: string);
implementation
uses main, util, defs;
const
MaxChar = 5000;
MaxKey = 631;
MaxInt = 32767;
NL = #10;
SP = ' ';
LeftBrace = #123;
RightBrace = #125;
LastStandardName = Write0;
type
CharSet = set of char;
SpellingTable = array [1..MaxChar] of char;
WordPointer = ^WordRecord;
WordRecord = record
NextWord: WordPointer;
IsName: boolean;
Index, Length, LastChar: integer;
end;
HashTable = array
[1..MaxKey] of WordPointer;
var
LineNo: integer;
ch: char;
AlphaNumeric, CapitalLetters, Digits, EndComment, Invisible, Letters,
Separators, SmallLetters: CharSet;
Spelling: SpellingTable;
Characters: integer;
Hash: HashTable;
Names: integer;
{ INPUT }
procedure NextChar;
begin
if UtilEOF() then
ch := ETX
else
begin
UtilReadChar(ch);
if ch in Invisible then
NextChar;
end;
end;
{ OUTPUT }
procedure Emit1(Symbol: SymbolType);
begin
Emit(Ord(Symbol));
//Writeln(symbolString1(Symbol));
end;
procedure Emit2(Symbol: SymbolType; Argument: integer);
begin
Emit(Ord(Symbol));
Emit(Argument);
//Writeln(symbolString1(Symbol),': ', Argument);
end;
{ WORD SYMBOLS AND NAMES }
function Key(Text: string; Length: integer): integer;
const
W = 32641 {32768 ~ 127};
N = MaxKey;
var
Sum, i: integer;
begin
Sum := 0;
i := 1;
while i <= Length do
begin
Sum := (Sum + Ord(Text[i])) mod W;
i := i + 1;
end;
Result := Sum mod N + 1;
end;
procedure Insert(IsName: boolean; Text: string; Length, Index, KeyNo: integer);
var
Pointer: WordPointer;
M, N: integer;
begin
{Insert the word in the spelling table}
Characters := Characters + Length;
TestLimit(Characters, MaxChar);
M := Length;
N := Characters - M;
while M > 0 do
begin
Spelling[M + N] := Text[M];
M := M - 1;
end;
{Insert the word in a word list}
New(Pointer);
Pointer^.NextWord := Hash[KeyNo];
Pointer^.IsName := IsName;
Pointer^.Index := Index;
Pointer^.Length := Length;
Pointer^.LastChar := Characters;
Hash[KeyNo] := Pointer;
end;
function Found(Text: string; Length: integer; Pointer: WordPointer): boolean;
var
Same: boolean;
M, N: integer;
begin
if Pointer^.Length <> Length then
Same := False
else
begin
Same := True;
M := Length;
N := Pointer^.LastChar - M;
while Same and (M > 0) do
begin
Same := Text[M] = Spelling[M + N];
M := M - 1;
end;
end;
Result := Same;
end;
procedure Define(IsName: boolean; Text: string; Length, Index: integer);
begin
Insert(IsName, Text, Length, Index, Key(Text, Length));
end;
procedure Search(Text: string; Length: integer; var IsName: boolean;
var Index: integer);
var
KeyNo: integer;
Pointer: WordPointer;
Done: boolean;
begin
KeyNo := Key(Text, Length);
Pointer := Hash[KeyNo];
Done := False;
while not Done do
begin
if Pointer = nil then
begin
IsName := True;
Names := Names + 1;
Index := Names;
Insert(True, Text, Length, Index, KeyNo);
Done := True;
end
else if Found(Text, Length, Pointer) then
begin
IsName := Pointer^.IsName;
Index := Pointer^.Index;
Done := True;
end
else
begin
Pointer := Pointer^.NextWord;
end;
end;
end;
function SubSet(First, Last: char): CharSet;
var
Value: CharSet;
ch: char;
begin
Value := [];
ch := First;
while
ch <= Last do
begin
Value := Value + [ch];
ch := chr(Ord(ch) + 1);
end;
Result := Value;
end;
procedure Initialize;
var
KeyNo: integer;
begin
InitPass(PASS1_SCAN);
Digits := SubSet('0', '9');
CapitalLetters := SubSet('A', 'Z');
SmallLetters := SubSet('a', 'z');
Letters := CapitalLetters + SmallLetters;
AlphaNumeric := Letters + Digits;
EndComment := [RightBrace, ETX];
Invisible := SubSet(#0, #31) + [#127] - [NL, ETX];
Separators := [SP, NL, LeftBrace];
KeyNo := 1;
while KeyNo <= MaxKey do
begin
Hash[KeyNo] := nil;
KeyNo := KeyNo + 1;
end;
Characters := 0;
{Insert the word symbols}
Define(False, 'and', 3, Ord(And1));
Define(False, 'array', 5, Ord(Array1));
Define(False, 'begin', 5, Ord(Begin1));
Define(False, 'const', 5, Ord(Const1));
Define(False, 'div', 3, Ord(Div1));
Define(False, 'do', 2, Ord(Do1));
Define(False, 'else', 4, Ord(Else1));
Define(False, 'end', 3, Ord(End1));
Define(False, 'if', 2, Ord(If1));
Define(False, 'mod', 3, Ord(Mod1));
Define(False, 'not', 3, Ord(Not1));
Define(False, 'of', 2, Ord(Of1));
Define(False, 'or', 2, Ord(Or1));
Define(False, 'procedure', 9, Ord(Procedure1));
Define(False, 'program', 7, Ord(Program1));
Define(False, 'record', 6, Ord(Record1));
Define(False, 'then', 4, Ord(Then1));
Define(False, 'type', 4, Ord(Type1));
Define(False, 'var', 3, Ord(Var1));
Define(False, 'while', 5, Ord(While1));
{Insert the standard names}
Define(True, 'integer', 7, Integer0);
Define(True, 'boolean', 7, Boolean0);
Define(True, 'false', 5, False0);
Define(True, 'true', 4, True0);
Define(True, 'read', 4, Read0);
Define(True, 'write', 5, Write0);
Names := LastStandardName;
end;
{ LEXICAL ANALYSIS }
procedure BeginLine(Number: integer);
begin
LineNo := Number;
NewLine(LineNo);
Emit2(NewLine1, LineNo);
end;
procedure EndLine;
begin
BeginLine(LineNo + 1);
end;
procedure Comment;
begin {ch = LeftBrace}
NextChar;
while not (ch in EndComment) do
begin
if ch = LeftBrace then
Comment
else
begin
if ch = NL then
EndLine;
NextChar;
end;
end;
if ch = RightBrace then
begin
NextChar;
end
else
begin
Error(Comment3);
end;
end;
procedure NextSymbol;
var
IsName: boolean;
Text: string;
Length, Index, Value, Digit: integer;
begin
Text := '';
IsName := False;
Index := 0;
while ch in Separators do
if ch = SP then
NextChar
else if ch = NL then
begin
EndLine;
NextChar;
end
else
begin
{ch = LeftBrace} Comment;
end;
if ch in Letters then
begin
Length := 0;
while ch in AlphaNumeric do
begin
if ch in CapitalLetters then
ch := chr(Ord(ch) + Ord('a') - Ord('A'));
Length := Length + 1;
Text := Text + ch;
NextChar;
end;
Search(Text, Length, IsName, Index);
if IsName then
Emit2(Name1, Index)
else
Emit(Index);
end
else if ch in Digits then
begin
Value := 0;
while ch in Digits do
begin
Digit := Ord(ch) - Ord('0');
if Value <= (MaxInt - Digit) div 10 then
begin
Value := 10 * Value + Digit;
NextChar;
end
else
begin
Error(Numeral3);
while ch in Digits do
NextChar;
end;
end;
Emit2(Numeral1, Value);
end
else if ch = '+' then
begin
Emit1(Plus1);
NextChar;
end
else if ch = '-' then
begin
Emit1(Minus1);
NextChar;
end
else if ch = '*' then
begin
Emit1(Asterisk1);
NextChar;
end
else if ch = '<' then
begin
NextChar;
if ch = '=' then
begin
Emit1(NotGreater1);
NextChar;
end
else if ch = '>' then
begin
Emit1(NotEqual1);
NextChar;
end
else
Emit1(Less1);
end
else if ch = '=' then
begin
Emit1(Equal1);
NextChar;
end
else if ch = '>' then
begin
NextChar;
if ch = '=' then
begin
Emit1(NotLess1);
NextChar;
end
else
Emit1(Greater1);
end
else if ch = ':' then
begin
NextChar;
if ch = '=' then
begin
Emit1(Becomes1);
NextChar;
end
else
Emit1(Colon1);
end
else if ch = '(' then
begin
Emit1(LeftParenthesis1);
NextChar;
end
else if ch = ')' then
begin
Emit1(RightParenthesis1);
NextChar;
end
else if ch = '[' then
begin
Emit1(LeftBracket1);
NextChar;
end
else if ch = ']' then
begin
Emit1(RightBracket1);
NextChar;
end
else if ch = ',' then
begin
Emit1(Comma1);
NextChar;
end
else if ch = '.' then
begin
NextChar;
if ch = '.' then
begin
Emit1(DoubleDot1);
NextChar;
end
else
Emit1(Period1);
end
else if ch = ';' then
begin
Emit1(Semicolon1);
NextChar;
end
else if ch <> ETX then
begin
Emit1(Unknown1);
NextChar;
end;
end;
procedure Scan(sourcefile: string);
begin
Initialize;
BeginLine(1);
UtilOpenSourceFile(sourcefile);
NextChar;
while ch <> ETX do
NextSymbol;
Emit1(EndText1);
//DbgOutputPass1();
end;
end.