-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLEXIC.PAS
271 lines (246 loc) · 6.48 KB
/
LEXIC.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
unit Lexic;
interface
const
KWmax = 100;
Setmax = 100;
type
TokenSet = set of char;
TokenType = ( Number, Identifier, Strn, KeyWord, Symbol, Operator, Unexisting );
LexData = record
StrId : char;
ComOp : char;
ComClos : char;
UnvSep : char;
end;
SetType = record
allowed : TokenSet; { Set of chars that may compose a token }
unallowed : TokenSet; { Chars that doesn't mean a token alone }
clase : TokenType; { Token's type }
end;
KWordObj = object
nKW : byte; { Number of key-words }
ary : pointer; { Pointer to the key-words array }
constructor Init ( nK : byte ; pKW : pointer );
function IsKWord ( s : string ) : byte; virtual; { 0 if s isn't a reserved word }
end;
AlphObj = object( KWordObj )
nSet : byte; { Number of sets }
SetArr : pointer; { Pointer to the sets' array }
StrId : char; { String identifier }
ComOp : char; { Comments identifier }
ComClos : char;
UnvSep : char;
constructor Init;
function GetSet ( ch : char ; var tSet : TokenSet ) : TokenType ; virtual;
end;
PLexObj = ^LexObj;
LexObj = object( AlphObj )
st : string;
tk : string;
ps : byte;
cf : TokenType;
skip : boolean;
constructor Init( kwP, stP : pointer ; kwN, stN : byte ; var data : LexData );
procedure SetStr( s : string ); { Sets string in st and formats it }
procedure FormatStr; { Formats st }
procedure SetToken; { Sets a token in tk }
procedure SetClasif; { Sets token's clasification in cf }
function GetStr( var s : string ) : boolean; { Gets a string to s. FALSE if there isn't any string. }
function Token : string; { Returns tk }
function Clasif : TokenType; { Returns cf }
function Poss : byte; { Returns ps }
function EOS : boolean; { TRUE if ps is in the End Of the String }
end;
implementation
uses
Util;
constructor KWordObj.Init( nK : byte ; pKW : pointer );
begin
nKW := nK;
ary := pKW;
end;
function KWordObj.IsKWord( s : string ) : byte;
type
KWaryType = array[ 1..KWmax ] of string;
var
i : byte;
KWary : ^KWaryType;
begin
KWary := ary;
i := 0;
repeat
inc( i )
until ( upper(s) = upper(KWary^[i]) ) or ( i = nKW );
if ( upper(s) = upper(KWary^[i]) )
then IsKWord := i
else IsKWord := 0;
end;
constructor AlphObj.Init;
begin
nSet := 0;
SetArr := nil;
StrId := ' ';
ComOp := ' ';
ComClos := ' ';
end;
function AlphObj.GetSet( ch : char ; var tSet : TokenSet ) : TokenType;
type
SetAryType = array[ 1..SetMax ] of SetType;
var
i : byte;
SetAry : ^SetAryType;
begin
SetAry := SetArr;
i := 0;
repeat
inc( i );
tSet := SetAry^[i].allowed;
until ( ch in (SetAry^[i].allowed - SetAry^[i].unallowed) ) or ( i = nSet );
if ch in (SetAry^[i].allowed - SetAry^[i].unallowed)
then GetSet := SetAry^[i].clase
else GetSet := Unexisting;
end;
constructor LexObj.Init( kwP, stP : pointer ; kwN, stN : byte ; var data : LexData );
begin
st := '';
tk := '';
ps := 1;
cf := Unexisting;
StrId := data.StrId;
ComOp := data.ComOp;
ComClos := data.ComClos;
UnvSep := data.UnvSep;
nKW := kwN;
ary := kwP;
nSet := stN;
SetArr := stP;
end;
procedure LexObj.FormatStr;
var
i : byte;
k : string;
inside : boolean;
comm : boolean;
begin
inside := FALSE;
comm := FALSE;
k := '';
i := 1;
while st[i] = UnvSep do
inc( i );
delete( st, 1, pred(i) );
for i := 1 to length( st ) do
begin
if st[i] = StrId
then inside := not inside;
if not inside
then
begin
if st[i] = ComOp
then comm := TRUE;
if not comm
then
if not( ( st[i] = UnvSep ) and ( (st[pred(i)] = UnvSep ) or (st[pred(i)] = ComClos) ) and ( pred(i) <> 0 ) )
then k := k + st[i];
if st[i] = ComClos
then comm := FALSE;
end
else k := k + st[i];
end;
st := k
end;
procedure LexObj.SetStr( s : string );
begin
st := s;
ps := 1;
FormatStr;
end;
procedure LexObj.SetToken;
var
aux : string;
tSet : TokenSet;
begin
tk := '';
if st[ps] = StrId
then
if GetStr( aux )
then
begin
tk := StrId + aux + StrId;
if st[ps] = UnvSep
then inc( ps );
end
else tk := ''
else
begin
cf := GetSet( st[ps], tSet );
while (cf = Unexisting) and not EOS do
begin
inc( ps );
cf := GetSet( st[ps], tSet );
end;
while ( st[ps] in tSet ) and ( ps <= length( st ) ) and ( st[ps] <> UnvSep ) do
begin
tk := tk + st[ps];
inc( ps );
end;
if st[ps] = UnvSep
then inc( ps );
end;
end;
function LexObj.GetStr( var s : string ) : boolean;
var
aux : string;
p : byte;
begin
if st[ps] = StrId
then
begin
aux := copy( st, ps, length( st ) - pred(ps) );
delete( aux, 1, 1 );
p := pos( StrId, aux );
if p <> 0
then
begin
s := copy( aux, 1, pred(p) );
ps := ps + succ(p);
GetStr := TRUE;
end
else GetStr := FALSE;
end;
end;
procedure LexObj.SetClasif;
var
i : byte;
auxSet : TokenSet;
auxStr : string;
begin
if tk = ''
then cf := Unexisting
else
if tk[1] = StrId
then
if byte( pos( StrId, copy( tk, 2, pred( length( tk ) ) ))) <> 0
then cf := Strn
else cf := Unexisting
else
if IsKWord( tk ) <> 0
then cf := KeyWord;
end;
function LexObj.Token : string;
begin
Token := tk
end;
function LexObj.Clasif : TokenType;
begin
Clasif := cf
end;
function LexObj.Poss : byte;
begin
Poss := ps
end;
function LexObj.EOS : boolean;
begin
EOS := ( ps > length( st ) );
end;
end.