-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPARSER.PAS
415 lines (361 loc) · 9.75 KB
/
PARSER.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
{********************************************************}
{* *}
{* LEX & YACC tools generic syntax analyzer unit *}
{* *}
{* File name : PARSER.PAS *}
{* *}
{* Programming: Russlan Kafri - 1999 *}
{* *}
{********************************************************}
unit Parser;
interface
uses Generics;
type TParseAction = (actShift, actReduce, actAccept, actError);
TActionRec = record
case Action: TParseAction
of actError: ( ErrorCode: Byte);
actShift: ( NextState: Byte);
actReduce: ( Rule: Byte);
end;
PStatesStackItem = ^TStatesStackItem;
TStatesStackItem = record
State: Byte;
Next: PStatesStackItem;
end;
PStatesStack = ^TStatesStack;
TStatesStack = object( TGeneric)
private
States: PStatesStackItem;
public
constructor Create;
destructor Destroy; virtual;
procedure Push( AState: Word);
function Pop: Word;
function Top: Word;
procedure Remove( ACount: Word);
procedure Clear;
procedure Display;
end;
PAttribStackItem = ^TAttribStackItem;
TAttribStackItem = record
Item: PGeneric;
Next: PAttribStackItem;
end;
PAttribStack = ^TAttribStack;
TAttribStack = object( TGeneric)
private
Items: PAttribStackItem;
public
constructor Create;
destructor Destroy; virtual;
procedure Push( AItem: PGeneric);
function Pop: PGeneric;
procedure Remove( ACount: Word);
procedure Clear;
end;
PParser = ^TParser;
TParser = object( TErrGeneric)
private
StatesStack: TStatesStack;
AttribStack: TAttribStack;
Lexeme: Word;
LexemeAttr: PGeneric;
Stop: Boolean;
procedure Reduce( ARule: Byte);
procedure Shift( AState: Byte);
procedure Accept;
public
constructor Create( AOwner: PErrGeneric);
destructor Destroy; virtual;
function ParsLoop: Boolean;
procedure ExecuteSemantic( ARule: Byte); virtual;
procedure NextLexeme; virtual;
function GetGoto( ATopState: Byte; ANonterminal: Byte): Byte; virtual;
procedure GetAction( ATopState: Byte; ATerminal: Word; var AAction: TActionRec); virtual;
function GetNonterminal( ARule: Byte): Byte; virtual;
function GetRuleSize( ARule: Byte): Byte; virtual;
procedure Error( AErrorCode: Byte); virtual;
procedure SetLexeme( ALexeme: Word; AAttrs: PGeneric);
function GetLexemeAttrs: PGeneric;
procedure StopParsing;
function PopAttribute: PGeneric;
procedure PushAttribute( AAttributes: PGeneric);
end;
implementation
{******************************************************************}
{* *}
{******************************************************************}
constructor TStatesStack.Create;
begin
inherited Create;
States := nil;
end;
destructor TStatesStack.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TStatesStack.Push( AState: Word);
var NewItem: PStatesStackItem;
begin
New( NewItem);
NewItem^.State := AState;
NewItem^.Next := States;
States := NewItem;
end;
function TStatesStack.Pop: Word;
var PopedItem: PStatesStackItem;
begin
if (States <> nil)
then begin
PopedItem := States;
States := PopedItem^.Next;
Pop := PopedItem^.State;
Dispose( PopedItem);
end
else Halt;
end;
function TStatesStack.Top: Word;
begin
if (States <> nil)
then Top := States^.State
else Halt;
end;
procedure TStatesStack.Remove( ACount: Word);
var I: Word;
ScanItem: PStatesStackItem;
begin
if (ACount > 0)
then begin
for I := 1 to ACount
do begin
ScanItem := States;
if (ScanItem <> nil)
then begin
States := ScanItem^.Next;
Dispose( ScanItem);
end
else Halt;
end;
end;
end;
procedure TStatesStack.Clear;
var ScanItem: PStatesStackItem;
begin
ScanItem := States;
while (ScanItem <> nil)
do begin
States := ScanItem^.Next;
Dispose( ScanItem);
ScanItem := States;
end;
end;
procedure TStatesStack.Display;
var ScanItem: PStatesStackItem;
begin
Write('Stack >');
ScanItem := States;
while (ScanItem <> nil)
do begin
Write( ScanItem^.State,' ');
ScanItem := ScanItem^.Next;
end;
end;
{******************************************************************}
{* *}
{******************************************************************}
constructor TAttribStack.Create;
begin
inherited Create;
Items := nil
end;
destructor TAttribStack.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TAttribStack.Push( AItem: PGeneric);
var NewItem: PAttribStackItem;
begin
New( NewItem);
NewItem^.Item := AItem;
NewItem^.Next := Items;
Items := NewItem;
end;
function TAttribStack.Pop: PGeneric;
var PopedItem: PAttribStackItem;
begin
if (Items <> nil)
then begin
PopedItem := Items;
Items := PopedItem^.Next;
Pop := PopedItem^.Item;
Dispose( PopedItem);
end
else Halt;
end;
procedure TAttribStack.Remove( ACount: Word);
var I: Word;
ScanItem: PAttribStackItem;
begin
if (ACount > 0)
then begin
for I := 1 to ACount
do begin
ScanItem := Items;
if (ScanItem <> nil)
then begin
Items := ScanItem^.Next;
if (ScanItem^.Item <> nil)
then ScanItem^.Item^.Free;
Dispose( ScanItem);
end
else Halt;
end;
end;
end;
procedure TAttribStack.Clear;
var ScanItem: PAttribStackItem;
begin
ScanItem := Items;
while (ScanItem <> nil)
do begin
Items := ScanItem^.Next;
if (ScanItem^.Item <> nil)
then ScanItem^.Item^.Free;
Dispose( ScanItem);
ScanItem := Items;
end;
end;
{******************************************************************}
{* *}
{******************************************************************}
constructor TParser.Create( AOwner: PErrGeneric);
begin
inherited Create( AOwner);
StatesStack.Create;
AttribStack.Create;
Stop := False;
Lexeme := 0;
LexemeAttr := nil;
end;
destructor TParser.Destroy;
begin
StatesStack.Destroy;
AttribStack.Destroy;
if ( LexemeAttr <> nil)
then LexemeAttr^.Free;
inherited Destroy;
end;
function TParser.ParsLoop: Boolean;
var ActionRec: TActionRec;
begin
ParsLoop := False;
Stop := False;
StatesStack.Clear;
AttribStack.Clear;
StatesStack.Push(1);
NextLexeme;
while not Stop
do begin
GetAction( StatesStack.Top, Lexeme, ActionRec);
case ActionRec.Action
of actShift: begin
Shift( ActionRec.NextState);
NextLexeme;
end;
actReduce: begin
Reduce( ActionRec.Rule);
end;
actAccept: begin
Accept;
ParsLoop := True;
end;
actError: begin
Error( ActionRec.ErrorCode);
end;
else
Break;
end;
end;
end;
procedure TParser.ExecuteSemantic( ARule: Byte);
begin
end;
procedure TParser.NextLexeme;
begin
end;
function TParser.GetGoto( ATopState: Byte; ANonterminal: Byte): Byte;
begin
GetGoto := 0;
Stop := True;
end;
procedure TParser.GetAction( ATopState: Byte; ATerminal: Word; var AAction: TActionRec);
begin
AAction.Action := actError;
AAction.ErrorCode := 0;
Stop := True;
end;
function TParser.GetNonterminal( ARule: Byte): Byte;
begin
Stop := True;
GetNonterminal := 0;
end;
function TParser.GetRuleSize( ARule: Byte): Byte;
begin
GetRuleSize := 0;
end;
procedure TParser.Error( AErrorCode: Byte);
begin
Stop := True;
end;
procedure TParser.SetLexeme( ALexeme: Word; AAttrs: PGeneric);
begin
if (LexemeAttr <> nil)
then LexemeAttr^.Free;
LexemeAttr := AAttrs;
Lexeme := ALexeme;
end;
function TParser.GetLexemeAttrs: PGeneric;
begin
GetLexemeAttrs := LexemeAttr;
end;
procedure TParser.StopParsing;
begin
Stop := True;
end;
function TParser.PopAttribute: PGeneric;
begin
PopAttribute := AttribStack.Pop;
end;
procedure TParser.PushAttribute( AAttributes: Pgeneric);
begin
AttribStack.Push( AAttributes);
end;
procedure TParser.Reduce( ARule: Byte);
var NonTerminal: Word;
RuleSize: Word;
GotoState: Word;
begin
ExecuteSemantic( ARule);
NonTerminal := GetNonterminal( ARule);
RuleSize := GetRuleSize( ARule);
StatesStack.Remove( RuleSize);
GotoState := GetGoto( StatesStack.Top, NonTerminal);
StatesStack.Push( GotoState);
end;
procedure TParser.Shift( AState: Byte);
begin
StatesStack.Push( AState);
if ( LexemeAttr <> nil)
then begin
AttribStack.Push( LexemeAttr);
LexemeAttr := nil;
end;
end;
procedure TParser.Accept;
begin
StatesStack.Clear;
Stop := True;
end;
end.