using System using System Collections Generic using System Globalizati

  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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CW4
{
class Lexer
{
readonly byte[][] table_of_letters = new byte[][] {
/*0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20*/
/*a, b, c, d, e, f, g, h, i, k, l, n, o, r, s, t, u, v, w, y, qpjzxm_A-Z0-9 */
/*state 0*/ new byte[] { 1, 2, 3, 4, 5, 6, 1, 1, 7, 1, 8, 1, 1, 9,10, 1,11,12,13, 1, 1 },
/*state 1*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 2*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,14, 1, 1, 1, 1, 1,18, 1 },
/*state 3*/ new byte[] {21, 1, 1, 1, 1, 1, 1,33, 1, 1, 1, 1,24, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 4*/ new byte[] { 1, 1, 1, 1,36, 1, 1, 1, 1, 1, 1, 1,42, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 5*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,46, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 6*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,51, 1,49, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 7*/ new byte[] { 1, 1, 1, 1, 1,55, 1, 1, 1, 1, 1,56, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 8*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,58, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state 9*/ new byte[] { 1, 1, 1, 1,61, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state10*/ new byte[] { 1, 1, 1, 1, 1, 1, 1,66,75, 1, 1, 1, 1, 1, 1, 1, 1, 1,70, 1, 1 },
/*state11*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,80, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state12*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,87, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state13*/ new byte[] { 1, 1, 1, 1, 1, 1, 1,90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state14*/ new byte[] { 1, 1, 1, 1,15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state15*/ new byte[] {16, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state16*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1,17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state17*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state18*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,19, 1, 1, 1, 1, 1 },
/*state19*/ new byte[] { 1, 1, 1, 1,20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state20*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state21*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,22, 1, 1, 1, 1, 1, 1 },
/*state22*/ new byte[] { 1, 1, 1, 1,23, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state23*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state24*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,25, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state25*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,31,26, 1, 1, 1, 1, 1 },
/*state26*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1,27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state27*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,28, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state28*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,29, 1, 1, 1, 1 },
/*state29*/ new byte[] { 1, 1, 1, 1,30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state30*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state31*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,32, 1, 1, 1, 1, 1 },
/*state32*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state33*/ new byte[] {34, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state34*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,35, 1, 1, 1, 1, 1, 1, 1 },
/*state35*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state36*/ new byte[] { 1, 1, 1, 1, 1,37, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state37*/ new byte[] {38, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state38*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,39, 1, 1, 1, 1 },
/*state39*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state40*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,41, 1, 1, 1, 1, 1 },
/*state41*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state42*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,93, 1, 1, 1, 1 },
/*state43*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,44, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state44*/ new byte[] { 1, 1, 1, 1,45, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state45*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state46*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,47, 1, 1, 1, 1, 1, 1 },
/*state47*/ new byte[] { 1, 1, 1, 1,48, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state48*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state49*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,50, 1, 1, 1, 1, 1, 1, 1 },
/*state50*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state51*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,52, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state52*/ new byte[] {53, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state53*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,54, 1, 1, 1, 1, 1 },
/*state54*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state55*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state56*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,57, 1, 1, 1, 1, 1 },
/*state57*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state58*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,59, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state59*/ new byte[] { 1, 1, 1, 1, 1, 1,60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state60*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state61*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,62, 1, 1, 1, 1, 1 },
/*state62*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,63, 1, 1, 1, 1 },
/*state63*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,64, 1, 1, 1, 1, 1, 1, 1 },
/*state64*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,65, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state65*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state66*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,67, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state67*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,68, 1, 1, 1, 1, 1, 1, 1 },
/*state68*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,69, 1, 1, 1, 1, 1 },
/*state69*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state70*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1,71, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state71*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,72, 1, 1, 1, 1, 1 },
/*state72*/ new byte[] { 1, 1,73, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state73*/ new byte[] { 1, 1, 1, 1, 1, 1, 1,74, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state74*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state75*/ new byte[] { 1, 1, 1, 1, 1, 1,76, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state76*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,77, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state77*/ new byte[] { 1, 1, 1, 1,78, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state78*/ new byte[] { 1, 1, 1,79, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state79*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state80*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,81, 1, 1, 1, 1, 1, 1 },
/*state81*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1,82, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state82*/ new byte[] { 1, 1, 1, 1, 1, 1,83, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state83*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,84, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state84*/ new byte[] { 1, 1, 1, 1,85, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state85*/ new byte[] { 1, 1, 1,86, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state86*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state87*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1,88, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state88*/ new byte[] { 1, 1, 1,89, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state89*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state90*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1,91, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state91*/ new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,92, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state92*/ new byte[] { 1, 1, 1, 1,93, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*state93*/ new byte[] { 1,43, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
/*a, b, c, d, e, f, g, h, i, k, l, n, o, r, s, t, u, v, w, y, qpjzxm_A-Z0-9 */
};
readonly Lexem[] states = {
Lexem.START, /* state 0 = START */
Lexem.IDENT, /* state 1 = DEFAULT IDENT */
Lexem.IDENT, /* state 2 = b|reak b|yte */
Lexem.IDENT, /* state 3 = c|ase c|ontinue c|har c|onst */
Lexem.IDENT, /* state 4 = d|ouble d|efault d|o */
Lexem.IDENT, /* state 5 = e|lse */
Lexem.IDENT, /* state 6 = f|or f|loat*/
Lexem.IDENT, /* state 7 = i|f i|nt */
Lexem.IDENT, /* state 8 = l|ong */
Lexem.IDENT, /* state 9 = r|eturn */
Lexem.IDENT, /* state 10 = s|witch s|hort s|igned */
Lexem.IDENT, /* state 11 = u|nsigned */
Lexem.IDENT, /* state 12 = v|oid */
Lexem.IDENT, /* state 13 = w|hile */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.BREAK, /* state 14,15,16,17 = break */
Lexem.IDENT,Lexem.IDENT,Lexem.BYTE, /* state 18,19,20 = byte */
Lexem.IDENT,Lexem.IDENT,Lexem.CASE, /* state 21,22,23 = case */
Lexem.IDENT,Lexem.IDENT, /* state 24,25 = con|st con|tinue */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.CONTINUE, /* state 26,27,28,29,30 = continue */
Lexem.IDENT,Lexem.CONST, /* state 31,32 = const */
Lexem.IDENT,Lexem.IDENT,Lexem.CHAR, /* state 33,34,35 = char */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.DEFAULT, /* state 36,37,38,39,40,41 = default */
Lexem.DO, /* state 42 = do do|ble */
Lexem.IDENT,Lexem.IDENT,Lexem.DOUBLE, /* state 43,44,45 = double */
Lexem.IDENT,Lexem.IDENT,Lexem.ELSE, /* state 46,47,48 = else */
Lexem.IDENT,Lexem.FOR, /* state 49,50 = for */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.FLOAT, /* state 51,52,53,54 = float */
Lexem.IF, /* state 55 = if */
Lexem.IDENT,Lexem.INT, /* state 56,57 = int */
Lexem.IDENT,Lexem.IDENT,Lexem.LONG, /* state 58,59,60 = long */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.FUNC_RETURN, /* state 61,62,63,64,65 = break */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.SHORT, /* state 66,67,68,69 = short */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.SWITCH, /* state 70,71,72,73,74 = switch */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.SIGNED, /* state 75,76,77,78,79 = signed */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.UNSIGNED, /* state 80,81,82,83,84,85,86 = unsigned */
Lexem.IDENT,Lexem.IDENT,Lexem.VOID, /* state 87,88,89 = long */
Lexem.IDENT,Lexem.IDENT,Lexem.IDENT,Lexem.WHILE, /* state 90,91,92,93 = while */
Lexem.IDENT /* state U for double(Forget it)*/
};
Dictionary<char, int> dictionary = new Dictionary<char, int>();
string program = String.Empty;
public Lexer(string program) {
for (char i = 'A'; i <= 'Z'; i++)
dictionary.Add(i, 20);
for (char i = '0'; i <= '9'; i++)
dictionary.Add(i, 20);
for (char i = 'a'; i <= 'i'; i++)
dictionary.Add(i, i - 'a');
dictionary.Add('j', 20);
dictionary.Add('k', 'k' - 'a' - 1);
dictionary.Add('l', 'l' - 'a' - 1);
dictionary.Add('m', 20);
dictionary.Add('n', 'n' - 'a' - 2);
dictionary.Add('o', 'o' - 'a' - 2);
dictionary.Add('p', 20);
dictionary.Add('q', 20);
for (char i = 'r'; i <= 'w'; i++)
dictionary.Add(i, i - 'a' - 4);
dictionary.Add('x', 20);
dictionary.Add('y', 19);
dictionary.Add('z', 20);
this.program = program;
}
public void Run()
{
Position cursor = new Position(program);
while (cursor.Current != -1)
{
while (cursor.IsWhiteSpace) cursor++;
int state = 0;
Position start = cursor.Copy();
List<char> attr = new List<char>();
while (!cursor.IsWhiteSpace && !cursor.IsNewLine)
{
int mChar;
dictionary.TryGetValue((char)cursor.Current,out mChar);
state = table_of_letters[state][mChar];
attr.Add((char)cursor.Current);
if (!cursor.IsWhiteSpace) cursor++;
}
Console.WriteLine(states[state] + "\t" + start.ToString() + '-' + cursor.ToString() + ":\t" + String.Join<char>("", attr));
cursor++;
}
Console.WriteLine("END");
Console.ReadLine();
}
}
enum Lexem
{
/* idents */ IDENT,
/* values */ INT_CONST, FLOAT_CONST, STRING_LITERAL,
/* types */ CHAR, DOUBLE, FLOAT, INT, LONG, SHORT, VOID, BYTE,
/* prefix */ CONST, SIGNED, UNSIGNED,
/* if */ IF, ELSE,
/* switch */ SWITCH, CASE, DEFAULT,
/* loop */ FOR, DO, WHILE, BREAK, CONTINUE,
/* ( ) return */ FUNC_ARG_LEFT, FUNC_ARG_RIGHT, FUNC_RETURN,
/* + - / * % */ ADD, SUB, DIV, MUL, PROCENT,
/* = +=... */ ASSIGN, ADD_ASSIGN, SUB_ASSIGN, MUL_ASSIGN, DIV_ASSIGN, PROCENT_ASSIGN,
/* &=... */ AND_ASSIGN, OR_ASSIGN, XOR_ASSIGN, RIGHT_ASSIGN, LEFT_ASSIGN,
/* ++, -- */ INC, DEC,
/* &, |, ^, >>, << */ AND_OP, OR_OP, XOR_OP, RIGHT_OP, LEFT_OP,
/* ==,!=,>=,<=,&&,||*/ EQ_LOGIC, NOT_EQ_LOGIC, NOT_S_LOGIC, NOT_L_LOGIC, AND_LOGIC, OR_LOGIC,
/* [], -> */ PRT_MASS_LEFT, PRT_MASS_RIGHT, PTR_LINK,
/*Utils { } ; , .*/ BLOCK_LEFT, BLOCK_RIGHT, SEMICOLON, COMMA, POINT,
/* Sub Lexem */ START, ERROR
}
struct Message {
readonly bool IsError;
readonly string Text;
}
struct Fragment
{
readonly Position Start, End;
public override string ToString()
{ return Start.ToString() + "-" + End.ToString(); }
}
class Position : IComparable<Position>
{
string program = String.Empty;
public int Index { get; private set; }
public int Line { get; private set; }
public int Pos { get; private set; }
public int Current
{
get
{
return (Index == program.Length) ? -1
: Char.ConvertToUtf32(program, Index);
}
}
public UnicodeCategory Uc
{
get
{
if (Index == program.Length) return UnicodeCategory.OtherNotAssigned;
else return Char.GetUnicodeCategory(program, Index);
}
}
public bool IsNewLine
{
get
{
if (Index == program.Length) return true;
if (program[Index] == '\r' && Index + 1 < program.Length)
return program[Index + 1] == '\n';
return program[Index] == '\n';
}
}
public bool IsWhiteSpace
{
get
{
return Index != program.Length && Char.IsWhiteSpace(program, Index);
}
}
private Position() { }
public Position(string program)
{
this.program = program;
Line = Pos = 1;
Index = 0;
}
public Position Copy() {
Position result = new Position()
{
Line = this.Line,
Pos = this.Pos,
Index = this.Index,
program = this.program
};
return result;
}
public override string ToString()
{ return "(" + Line + "," + Pos + ")"; }
public static Position operator ++(Position p) {
if (p.Index < p.program.Length)
{
if (p.IsNewLine)
{
if (p.program[p.Index] == '\r') p.Index++;
p.Line++;
p.Pos = 1;
}
else
{
if (Char.IsHighSurrogate(p.program[p.Index])) p.Index++;
p.Pos++;
}
p.Index++;
}
return p;
}
public int CompareTo(Position other)
{ return Index.CompareTo(other.Index); }
}
}