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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.IO;
namespace FirstUtil
{
class Lexer : IEnumerable<Token> //scanner
{
string program = String.Empty;
Compiler compiler;
Dictionary<char, int> dictionaryForIdents;
Dictionary<char, int> dictionaryForDigits;
bool afterIdent = false;
bool afterValue = false;
Token bufToken = null;
#region Таблицы переходов и т.д.
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_letters = {
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)*/
};
readonly byte[][] table_of_numbers = new byte[][] {
/*state 9 Exit*/ /*-+, 0,19, ., E, other*/
/*state 0*/ new byte[] { 8, 1, 4, 8, 8, 8 },
/*state 1*/ new byte[] { 9, 8, 8, 2, 8, 8 },
/*state 2*/ new byte[] { 8, 3, 3, 8, 8, 8 },
/*state 3*/ new byte[] { 9, 3, 3, 8, 5, 8 },
/*state 4*/ new byte[] { 9, 4, 4, 2, 5, 8 },
/*state 5*/ new byte[] { 6, 8, 7, 8, 8, 8 },
/*state 6*/ new byte[] { 8, 8, 7, 8, 8, 8 },
/*state 7*/ new byte[] { 9, 7, 7, 8, 8, 8 },
/*state 8*/ new byte[] { 8, 8, 8, 8, 8, 8 }, /*ERROR*/
};
readonly Lexem[] states_numbers = {
Lexem.START, /* state 0 = START */
Lexem.INT_CONST, /* state 1 = final 0 */
Lexem.ERROR, /* state 2 = Non-final 1. */
Lexem.FLOAT_CONST, /* state 3 = final 1.2 */
Lexem.INT_CONST, /* state 4 = final 1 */
Lexem.ERROR, /* state 5 = Non-final 1.2E */
Lexem.ERROR, /* state 6 = Non-final 1.2E- */
Lexem.FLOAT_CONST, /* state 7 = final 1.2E-3 */
Lexem.ERROR
};
#endregion
#region Сканеры
private Token scan(Position cursor)
{
Token result = null;
if (bufToken != null)
{
result = bufToken;
bufToken = null;
return result;
}
while (cursor.IsWhiteSpace) //WhiteSpaces
cursor++;
if (cursor.Current == -1)
return new SpecToken()
{
Tag = Lexem.END,
Coords = new Fragment() { Start = cursor }
};
if (cursor.IsLetter) //Idents and keys
result = FindIdent(cursor);
else if (cursor.IsDecimalDigit) //Numbers
result = FindNumber(cursor);
else if (cursor.Current == '\"') //Strings
result = FindStr(cursor);
else if (cursor.Current == '\'') //Chars
result = FindChar(cursor);
else //Other
result = FindOther(cursor);
return result ?? scan(cursor);
}
private Token FindIdent(Position cursor)
{
if (afterIdent || afterValue)
{
compiler.AddMessage(cursor, new Message()
{
IsError = true,
Text = "Unexpected identifier after identifier or value"
});
cursor++;
afterIdent = afterValue = false;
return null;
}
int state = 0;
Position start = cursor.Copy();
List<char> attr = new List<char>();
while (cursor.IsLetterOrDigit)
{
int mChar;
dictionaryForIdents.TryGetValue((char)cursor.Current, out mChar);
state = table_of_letters[state][mChar];
attr.Add((char)cursor.Current);
cursor++;
}
afterIdent = states_letters[state] == Lexem.IDENT;
afterValue = false;
if (afterIdent) return new IdentToken()
{
Code = compiler.AddName(new String(attr.ToArray())),
Coords = new Fragment() { Start = start, End = cursor }
};
else return new SpecToken()
{
Tag = states_letters[state],
Coords = new Fragment() { Start = start, End = cursor }
};
}
private Token FindNumber(Position cursor)
{
if (afterIdent || afterValue)
{
compiler.AddMessage(cursor, new Message()
{
IsError = true,
Text = "Unexpected identifier after identifier or value"
});
cursor++;
afterIdent = afterValue = false;
return null;
}
int state = 0;
Position start = cursor.Copy();
List<char> attr = new List<char>();
while (cursor.IsDecimalDigit
|| cursor.Current == '.'
|| cursor.Current == '+'
|| cursor.Current == '-'
|| cursor.Current == 'E'
|| cursor.Current == 'e')
{
if (state == 8) break;
int mChar;
dictionaryForDigits.TryGetValue((char)cursor.Current, out mChar);
if (table_of_numbers[state][mChar] == 9)
{ //Exit
break;
}
state = table_of_numbers[state][mChar];
attr.Add((char)cursor.Current);
cursor++;
}
afterValue = states_numbers[state] != Lexem.ERROR;
afterIdent = false;
string value = new String(attr.ToArray());
long iValue;
double fValue;
if (afterValue && long.TryParse(value,out iValue)) return new INumberToken()
{
Coords = new Fragment() { Start = start, End = cursor },
Value = iValue
};
else if (afterValue && double.TryParse(value,out fValue)) return new FNumberToken()
{
Coords = new Fragment() { Start = start, End = cursor },
Value = fValue
};
string errorText = afterValue ? "Constant is too large" : "Bad constant";
compiler.AddMessage(start, new Message()
{
IsError = true,
Text = errorText
});
return null;
}
private Token FindStr(Position cursor)
{
cursor++;
if (afterIdent || afterValue)
{
compiler.AddMessage(cursor, new Message()
{
IsError = true,
Text = "Unexpected value after identifier or value"
});
afterIdent = afterValue = false;
return null;
}
Position start = cursor.Copy();
List<char> attr = new List<char>();
bool correctStr = false;
while (cursor.Current != -1 && !cursor.IsNewLine)
{
if (cursor.Current == '\"')
{
correctStr = true;
break;
}
else if (cursor.Current == '\\')
{
char? escape = TryParseEscape(cursor);
if (escape == null) {
compiler.AddMessage(cursor, new Message()
{
IsError = true,
Text = "Bad escape \\" + (char)cursor.Current
});
attr.Add('\\');
attr.Add((char)cursor++.Current);
}
else
attr.Add((char)escape);
}
else
{
attr.Add((char)cursor.Current);
cursor++;
}
}
afterValue = correctStr;
afterIdent = false;
if (!correctStr)
{
compiler.AddMessage(cursor++, new Message()
{
IsError = true,
Text = "\" expected"
});
return null;
}
return new StringToken()
{
Value = new string(attr.ToArray()),
Coords = new Fragment() { Start = start, End = cursor++ }
};
}
private Token FindChar(Position cursor)
{
cursor++;
if (afterIdent || afterValue)
{
compiler.AddMessage(cursor, new Message()
{
IsError = true,
Text = "Unexpected value after identifier or value"
});
afterIdent = afterValue = false;
return null;
}
/*
* Первый символ или escape-последовательность
*/
char? codePoint = null;
if (cursor.Current == '\\')
{
codePoint = TryParseEscape(cursor);
}
else if (!cursor.IsNewLine)
{
codePoint = (char)cursor.Current;
cursor++;
}
/*
* Закрывающаяся кавычка
* Итог
*/
afterIdent = false;
afterValue = codePoint != null && cursor.Current == '\'';
if (afterValue) return new INumberToken()
{
Coords = new Fragment() { Start = cursor++ },
Value = (char)codePoint
};
string errorText = cursor.Current != '\'' ? "\' expected" : "Bad escape";
compiler.AddMessage(cursor++, new Message()
{
IsError = true,
Text = errorText
});
return null;
}
private char? TryParseEscape(Position cursor)
{
char? result = null;
if (cursor.Current != '\\')
{
return null;
}
switch ((++cursor).Current)
{
case '\'':
case '\"':
case '\\':
result = (char)cursor.Current;
break;
case 'a':
result = '\a';
break;
case 'b':
result = '\b';
break;
case 'f':
result = '\f';
break;
case 'n':
result = '\n';
break;
case 'r':
result = '\r';
break;
case 't':
result = '\t';
break;
case 'v':
result = '\v';
break;
default:
break;
}
cursor++;
return result;
}
private Token FindOther(Position cursor)
{
Lexem tag;
afterIdent = afterValue = false;
switch (cursor.Current)
{
case '>':
if ((++cursor).Current == '>')
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_RIGHT + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_RIGHT + ":\t" + cursor.ToString());
else if (cursor.Current == '=') Console.WriteLine(Lexem.LOGIC_NOT_S + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.LOGIC_L + ":\t" + cursor.ToString());
break;
case '<':
if ((++cursor).Current == '<')
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_LEFT + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_LEFT + ":\t" + cursor.ToString());
else if (cursor.Current == '=') Console.WriteLine(Lexem.LOGIC_NOT_L + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.LOGIC_S + ":\t" + cursor.ToString());
break;
case '+':
if ((++cursor).Current == '+')
if ((++cursor).Current == '+')
if (afterIdent)
{
Console.WriteLine(Lexem.INC + ":\t" + cursor++.ToString());
Console.WriteLine(Lexem.ASSIGN_ADD + ":\t" + cursor.ToString());
}
else
Console.WriteLine(Lexem.INC + ":\t" + cursor.ToString());
else Console.WriteLine(Lexem.INC + ":\t" + cursor.ToString());
else if (cursor.Current == '=') Console.WriteLine(Lexem.ASSIGN_ADD + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_ADD + ":\t" + cursor.ToString());
break;
case '-':
if ((++cursor).Current == '-')
if ((++cursor).Current == '-')
if (afterIdent)
{
Console.WriteLine(Lexem.OP_SUB + ":\t" + cursor++.ToString());
Console.WriteLine(Lexem.ASSIGN_SUB + ":\t" + cursor.ToString());
}
else
Console.WriteLine(Lexem.DEC + ":\t" + cursor.ToString());
else Console.WriteLine(Lexem.DEC + ":\t" + cursor.ToString());
else if (cursor.Current == '=') Console.WriteLine(Lexem.ASSIGN_SUB + ":\t" + (++cursor).ToString());
else if (cursor.Current == '>') Console.WriteLine(Lexem.PTR_LINK + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_ADD + ":\t" + cursor.ToString());
break;
case '*':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_MUL + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_MUL + ":\t" + cursor.ToString());
break;
case '/':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_DIV + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_DIV + ":\t" + cursor.ToString());
break;
case '%':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_PROCENT + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_PROCENT + ":\t" + cursor.ToString());
break;
case '&':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_AND + ":\t" + (++cursor).ToString());
else if (cursor.Current == '&') Console.WriteLine(Lexem.LOGIC_AND + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_AND + ":\t" + cursor.ToString());
break;
case '^':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_XOR + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_XOR + ":\t" + cursor.ToString());
break;
case '|':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.ASSIGN_OR + ":\t" + (++cursor).ToString());
else if (cursor.Current == '|') Console.WriteLine(Lexem.LOGIC_OR + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.OP_OR + ":\t" + cursor.ToString());
break;
case '!':
if (cursor.Current == '=') Console.WriteLine(Lexem.LOGIC_NOT_EQ + ":\t" + (++cursor).ToString());
else Console.WriteLine("ERROR!" + ":\t" + cursor.ToString());
break;
case '=':
if ((++cursor).Current == '=') Console.WriteLine(Lexem.LOGIC_EQ + ":\t" + (++cursor).ToString());
else Console.WriteLine(Lexem.ASSIGN + ":\t" + cursor.ToString());
break;
case ';':
Console.WriteLine(Lexem.SEMICOLON + ":\t" + (++cursor).ToString());
break;
case '[':
Console.WriteLine(Lexem.PTR_MASS_LEFT + ":\t" + (++cursor).ToString());
break;
case ']':
Console.WriteLine(Lexem.PTR_MASS_RIGHT + ":\t" + (++cursor).ToString());
break;
case '{':
Console.WriteLine(Lexem.BLOCK_LEFT + ":\t" + (++cursor).ToString());
break;
case '}':
Console.WriteLine(Lexem.BLOCK_RIGHT + ":\t" + (++cursor).ToString());
break;
case '(':
Console.WriteLine(Lexem.FUNC_ARG_LEFT + ":\t" + (++cursor).ToString());
break;
case ')':
Console.WriteLine(Lexem.FUNC_ARG_RIGHT + ":\t" + (++cursor).ToString());
break;
case ',':
Console.WriteLine(Lexem.COMMA + ":\t" + (++cursor).ToString());
break;
case '.':
Console.WriteLine(Lexem.POINT + ":\t" + (++cursor).ToString());
break;
case ':':
Console.WriteLine(Lexem.DOUBLE_POINT + ":\t" + (++cursor).ToString());
break;
default:
Console.WriteLine("ERROR!" + ":\t" + (++cursor).ToString());
break;
}
}
#endregion
public Lexer(string program, Compiler compiler)
{
this.compiler = compiler;
dictionaryForIdents = new Dictionary<char, int> { };
dictionaryForDigits = new Dictionary<char, int> { };
/*
* Заглавные буквы
*/
for (char i = 'A'; i <= 'Z'; i++)
dictionaryForIdents.Add(i, 20);
dictionaryForDigits.Add('E', 4);
/*
* Цифры
*/
dictionaryForIdents.Add('0', 20);
dictionaryForDigits.Add('0', 1);
for (char i = '1'; i <= '9'; i++)
{
dictionaryForIdents.Add(i, 20);
dictionaryForDigits.Add(i, 2);
}
/*
* маленькие буквы
*/
dictionaryForDigits.Add('e', 4);
int point = 'a';
for (char i = 'a'; i <= 'i'; i++)
dictionaryForIdents.Add(i, i - point);
dictionaryForIdents.Add('j', 20); point++;
dictionaryForIdents.Add('k', 'k' - point);
dictionaryForIdents.Add('l', 'l' - point);
dictionaryForIdents.Add('m', 20); point++;
dictionaryForIdents.Add('n', 'n' - point);
dictionaryForIdents.Add('o', 'o' - point);
dictionaryForIdents.Add('p', 20); point++;
dictionaryForIdents.Add('q', 20); point++;
for (char i = 'r'; i <= 'w'; i++)
dictionaryForIdents.Add(i, i - point);
dictionaryForIdents.Add('x', 20); point++;
dictionaryForIdents.Add('y', 'y' - point);
dictionaryForIdents.Add('z', 20); point++;
/*
* Спец символы
*/
dictionaryForIdents.Add('_', 20);
dictionaryForDigits.Add('.', 3);
dictionaryForDigits.Add('+', 0);
dictionaryForDigits.Add('-', 0);
this.program = program;
}
public override IEnumerable<Token> GetEnumerator() {
Position cursor = new Position(program);
Token t = new SpecToken()
{
Tag = Lexem.START,
Coords = new Fragment() { Start = cursor }
};
yield return t;
while (t.Tag != Lexem.END)
yield return t = scan(cursor);
}
public void Run()
{
Position cursor = new Position(program);
List<Token> tokens = new List<Token>();
tokens.Add(
new SpecToken()
{
Tag = Lexem.START,
Coords = new Fragment() { Start = cursor }
}
);
while (tokens.Last().Tag != Lexem.END)
tokens.Add(scan(cursor));
}
}
abstract class Token
{
public readonly Lexem Tag { get; set; }
public readonly Fragment Coords { get; set; }
}
class IdentToken : Token
{
public readonly int Code { get; set; }
public IdentToken() : base() { Tag = Lexem.IDENT; }
}
class INumberToken : Token
{
public readonly long Value { get; set; }
public INumberToken() : base() { Tag = Lexem.INT_CONST; }
}
class FNumberToken : Token
{
public readonly double Value { get; set; }
public FNumberToken() : base() { Tag = Lexem.FLOAT_CONST; }
}
class StringToken : Token
{
public readonly string Value { get; set; }
public StringToken() : base() { Tag = Lexem.STRING_LITERAL; }
}
class SpecToken : Token
{}
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,
/* + - / * % */
OP_ADD, OP_SUB, OP_DIV, OP_MUL, OP_PROCENT,
/* = +=... */
ASSIGN, ASSIGN_ADD, ASSIGN_SUB, ASSIGN_MUL, ASSIGN_DIV, ASSIGN_PROCENT,
/* &=... */
ASSIGN_AND, ASSIGN_OR, ASSIGN_XOR, ASSIGN_RIGHT, ASSIGN_LEFT,
/* ++, -- */
INC, DEC,
/* &, |, ^, >>, << */
OP_AND, OP_OR, OP_XOR, OP_RIGHT, OP_LEFT,
/* ==,!=,>=,<=,&&,||, <, >*/
LOGIC_EQ, LOGIC_NOT_EQ, LOGIC_NOT_S, LOGIC_NOT_L, LOGIC_AND, LOGIC_OR, LOGIC_S, LOGIC_L,
/* [], -> */
PTR_MASS_LEFT, PTR_MASS_RIGHT, PTR_LINK,
/*Utils { } ; , . : */
BLOCK_LEFT, BLOCK_RIGHT, SEMICOLON, COMMA, POINT, DOUBLE_POINT,
/* Sub Lexem */
START, ERROR, END
}
struct Message
{
public bool IsError;
public string Text;
}
struct Fragment
{
public Position Start, End = null;
public override string ToString()
{
return Start.ToString() + End == null ? "-" + 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 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);
}
}
public bool IsLetter
{
get
{
return Index != program.Length && (Char.IsLetter(program, Index) || Current == '_');
}
}
public bool IsLetterOrDigit
{
get
{
return Index != program.Length && (Char.IsLetterOrDigit(program, Index) || Current == '_');
}
}
public bool IsDecimalDigit
{
get
{
return Index != program.Length && program[Index] >= '0' && program[Index] <= '9';
}
}
public bool CanBeTogether
{
get
{
switch (Current)
{
case '(':
case ')':
case '+':
case '-':
case '/':
case '*':
case '%':
case '=':
case '&':
case '|':
case '^':
case '>':
case '<':
case '!':
case '[':
case '{':
case '}':
case ';':
case ',':
case '.':
return true;
default:
return false;
}
}
}
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); }
}
}