-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.c
1024 lines (984 loc) · 33.8 KB
/
scanner.c
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#include "scanner.h"
extern bool jirkaDouble;
bool dbl_end = false; // Variable for readDouble
Token *getNextToken(FILE *f) {
char *str = NULL;
char *original = NULL;
ReservedWord reserved;
SymbolType symbol;
int lineNum, lineCol;
int c = Get_Token(f, &original, &str, &reserved, &symbol, &lineNum, &lineCol);
Token *t = NULL;
char *endptr = NULL;
t = malloc_c(sizeof(Token));
t->lineNum = lineNum;
t->lineChar = lineCol;
t->original = original;
t->next = NULL;
if (dbl_end == true){
t->lineNum = -1;
}
#ifdef DEBUG
printf("TOKEN: %s\n", t->original);
#endif
switch(c) {
case RESERVED_WORD:
t->type = RESERVED;
t->val.reserved = reserved;
break;
case IDEN:
t->type = ID_SIMPLE;
t->val.id = str;
break;
case IDEN_COMPOUND:
t->type = ID_COMPOUND;
t->val.id = str;
break;
case NUMBER:
t->type = LIT_INTEGER;
t->val.intVal = strtol(str,NULL,10);
break;
case EXT_BASE:
str+=2;
t->type = LIT_INTEGER;
t->val.intVal = strtol(str, &endptr, 2);
break;
case EXT_HEX:
t->type = LIT_DOUBLE;
t->val.doubleVal = hexToDbl(t->original);
break;
case EXT_OCT:
t->type = LIT_INTEGER;
t->val.intVal = strtol(str, &endptr, 8);
break;
case FLOAT:
t->type = LIT_DOUBLE;
t->val.doubleVal = strtod(str,&endptr);
if (*endptr != '\0'){
fprintf(stderr, "Invalid float read %s\n", t->original);
ERROR(ERR_LEXER);
}
break;
case STRING:
t->type = LIT_STRING;
t->val.stringVal = str;
break;
case AUT_SYMBOL:
t->type = SYMBOL;
t->val.symbol = symbol;
break;
case END_OF_FILE:
free_c(t->original);
free_c(t);
return NULL;
default:
fprintf(stderr, "Invalid string occurred %s\n", t->original);
if (jirkaDouble) {
ERROR(ERR_RUNTIME_INT_PARSE);
} else {
ERROR(ERR_LEXER);
}
}
return t;
}
Token *createToken(TokenType type, void *value, char *original) {
Token *t = malloc_c(sizeof(Token));
t->lineNum = t->lineChar = 0;
t->original = original;
t->next = NULL;
t->type = type;
t->val.id = value;
return t;
}
//sluzi pre Parser,spojovy zoznam Tokenov
Token *chainTokens(Token *first, ...) {
va_list args;
va_start(args, first);
Token *current = first, *next;
while ((next = va_arg(args, Token *)) != NULL) {
current->next = next;
current = next;
}
current->next = NULL;
va_end(args);
return first;
}
void freeToken(Token *t) {
if (t == NULL) {
return;
}
if (t->original != NULL) {
free_c(t->original);
}
switch (t->type) {
case ID_SIMPLE:
case ID_COMPOUND:
if (t->original != t->val.id)
free_c(t->val.id);
break;
case LIT_STRING:
if (t->original != t->val.stringVal)
free_c(t->val.stringVal);
break;
case RESERVED:
case SYMBOL:
case LIT_INTEGER:
case LIT_DOUBLE:
break;
}
free_c(t);
}
void printToken(Token *t) {
if (t == NULL) {
printf("NULL\n");
return;
}
switch (t->type) {
case ID_SIMPLE:
case ID_COMPOUND:
printf("ID:%s\n", t->val.id);
return;
case LIT_INTEGER:
printf("I:%d\n", t->val.intVal);
return;
case LIT_STRING:
printf("S:\"%s\"\n", t->val.stringVal);
return;
case LIT_DOUBLE:
printf("D:%g\n", t->val.doubleVal);
return;
case RESERVED:
printf("R:");
switch (t->val.reserved) {
case RES_BOOLEAN: printf("boolean\n"); return;
case RES_BREAK: printf("break\n"); return;
case RES_CLASS: printf("class\n"); return;
case RES_CONTINUE:printf("continue\n"); return;
case RES_DO: printf("do\n"); return;
case RES_DOUBLE: printf("double\n"); return;
case RES_ELSE: printf("else\n"); return;
case RES_FALSE: printf("false\n"); return;
case RES_FOR: printf("for\n"); return;
case RES_IF: printf("if\n"); return;
case RES_INT: printf("int\n"); return;
case RES_RETURN: printf("return\n"); return;
case RES_STRING: printf("String\n"); return;
case RES_STATIC: printf("static\n"); return;
case RES_TRUE: printf("true\n"); return;
case RES_VOID: printf("void\n"); return;
case RES_WHILE: printf("while\n"); return;
}
case SYMBOL:
printf("Y:");
switch (t->val.symbol) {
case SYM_BRACE_OPEN: printf("{\n"); return;
case SYM_BRACE_CLOSE: printf("}\n"); return;
case SYM_BRACKET_OPEN: printf("[\n"); return;
case SYM_BRACKET_CLOSE: printf("]\n"); return;
case SYM_PAREN_OPEN: printf("(\n"); return;
case SYM_PAREN_CLOSE: printf(")\n"); return;
case SYM_PLUS: printf("+\n"); return;
case SYM_MINUS: printf("-\n"); return;
case SYM_UNARY_PLUS: printf("++\n"); return;
case SYM_UNARY_MINUS: printf("--\n"); return;
case SYM_STAR: printf("*\n"); return;
case SYM_SLASH: printf("/\n"); return;
case SYM_SEMI: printf(";\n"); return;
case SYM_COMMA: printf(",\n"); return;
case SYM_LESS: printf("<\n"); return;
case SYM_GREATER: printf(">\n"); return;
case SYM_LESS_EQUAL: printf("<=\n"); return;
case SYM_GREATER_EQUAL: printf(">=\n"); return;
case SYM_EQUALS: printf("==\n"); return;
case SYM_NOT_EQUALS: printf("!=\n"); return;
case SYM_ASSIGN: printf("=\n"); return;
case SYM_NOT: printf("!\n"); return;
case SYM_AND: printf("&&\n"); return;
case SYM_OR: printf("||\n"); return;
}
}
}
//kontrola klucovych slov
AUTSTATES control_res_key_word(char *str, ReservedWord *reserved)
{
switch (str[0]) {
case 'b':
if (strcmp(str, "boolean") == 0) {
*reserved = RES_BOOLEAN;
return RESERVED_WORD;
}
if (strcmp(str, "break") == 0) {
*reserved = RES_BREAK;
return RESERVED_WORD;
}
break;
case 'c':
if (strcmp(str, "class") == 0) {
*reserved = RES_CLASS;
return RESERVED_WORD;
}
if (strcmp(str, "continue") == 0) {
*reserved = RES_CONTINUE;
return RESERVED_WORD;
}
break;
case 'd':
if (strcmp(str, "do") == 0) {
*reserved = RES_DO;
return RESERVED_WORD;
}
if (strcmp(str, "double") == 0) {
*reserved = RES_DOUBLE;
return RESERVED_WORD;
}
break;
case 'e':
if (strcmp(str, "else") == 0) {
*reserved = RES_ELSE;
return RESERVED_WORD;
}
break;
case 'f':
if (strcmp(str, "false") == 0) {
*reserved = RES_FALSE;
return RESERVED_WORD; }
if (strcmp(str, "for") == 0) {
*reserved = RES_FOR;
return RESERVED_WORD;
}
break;
case 'i':
if (strcmp(str, "if") == 0) {
*reserved = RES_IF;
return RESERVED_WORD; }
if (strcmp(str, "int") == 0) {
*reserved = RES_INT;
return RESERVED_WORD;
}
break;
case 'r':
if (strcmp(str, "return") == 0) {
*reserved = RES_RETURN;
return RESERVED_WORD;
}
break;
case 'S':
if (strcmp(str, "String") == 0) {
*reserved = RES_STRING;
return RESERVED_WORD;
}
break;
case 's':
if (strcmp(str, "static") == 0) {
*reserved = RES_STATIC;
return RESERVED_WORD;
}
break;
case 't':
if (strcmp(str, "true") == 0) {
*reserved = RES_TRUE;
return RESERVED_WORD;
}
break;
case 'v':
if (strcmp(str, "void") == 0) {
*reserved = RES_VOID;
return RESERVED_WORD;
}
break;
case 'w':
if (strcmp(str, "while") == 0) {
*reserved = RES_WHILE;
return RESERVED_WORD;
}
break;
}
return IDEN;
}
//funkcia pre string
void string_end(char **string, char c, int *stringLength, int *stringAlloc) {
if (*string == NULL) {
*string = malloc_c(*stringAlloc * sizeof(char));
}
if ((*stringLength) + 1 >= (*stringAlloc)) {
*stringAlloc <<= 1;
*string = realloc_c(*string, *stringAlloc * sizeof(char));
}
(*string)[(*stringLength)++] = c;
(*string)[*stringLength] = '\0';
}
#define GET_CHAR(c, input, state, line, lineChar) \
do { \
c = fgetc(input); \
if (c == -1) { \
return END_OF_FILE; \
} \
if (c == '\n') { \
lineNum++; \
lineChar = 0; \
if (input == stdin) { \
dbl_end = true; \
} \
} else { \
lineChar++; \
} \
} while (0);
AUTSTATES Get_Token(FILE *input, char **orig, char **string, ReservedWord *reserved, SymbolType *symbol, int *lineNumToken, int *lineColToken) {
static int state = NEUTRAL_STATE;
static int lineNum = 0;
static int lineCol = 0;
static int c = '\0';
int num;
int stringLength = 0;
int stringAlloc = ALLOC_BLOCK;
int origLength = 0;
int origAlloc = ALLOC_BLOCK;
while (true) {
switch (state) {
case END_OF_FILE:
return END_OF_FILE;
case NEUTRAL_STATE : // nacitame znak
GET_CHAR(c, input, state, line, lineCol);
//zacaitocny stav
case Start_state :
*lineNumToken = lineNum;
*lineColToken = lineCol;
*reserved = 0; // musime si vynulovat reserved ked nacitame znova
if (isspace(c)) {
state = NEUTRAL_STATE;
continue;
}
string_end(orig, c, &origLength, &origAlloc);
//Identifikatori
if ((c>='A' && c<='Z') || (c>='a' && c<='z') || c == '_' || c == '$')
state = AUT_IDEN;
//rozsirenie BASE
//kvoli oktalovemu , string musi byt nulovy kedze 0 je prva
else if ((c == '0') && *string== NULL)
state = AUT_BIN;
//pokracujeme na stav cisla
else if ((isdigit(c)))
state = AUT_NUM;
//rozsirenia unary + a -
else if (c == '+') {
state = AUT_UNARY_PL;
}
else if (c == '-'){
state = AUT_UNARY_MN;
}
/*po nacitani znaku sa hned dostavame k symbolom
a vynechavame dalsie stavy*/
else if (c == '*') {
state = NEUTRAL_STATE;
*symbol = SYM_STAR;
return AUT_SYMBOL;
}
else if(c == '{') {
state = NEUTRAL_STATE;
*symbol = SYM_BRACE_OPEN;
return AUT_SYMBOL;
}
else if(c == '}') {
state = NEUTRAL_STATE;
*symbol = SYM_BRACE_CLOSE;
return AUT_SYMBOL;
}
else if(c == '(') {
state = NEUTRAL_STATE;
*symbol = SYM_PAREN_OPEN;
return AUT_SYMBOL;
}
else if(c == ')') {
state = NEUTRAL_STATE;
*symbol = SYM_PAREN_CLOSE;
return AUT_SYMBOL;
}
else if(c == '[') {
state = NEUTRAL_STATE;
*symbol = SYM_BRACKET_OPEN;
return AUT_SYMBOL;
}
else if(c == ']') {
state = NEUTRAL_STATE;
*symbol = SYM_BRACKET_CLOSE;
return AUT_SYMBOL;
}
else if(c == ',') {
state = NEUTRAL_STATE;
*symbol = SYM_COMMA;
return AUT_SYMBOL;
}
else if (c == '/') // pokracujeme dalej kvoli moznosti komentaru
state = AUT_DIV;
else if (c == '<')
state = AUT_LESS; // pokracujeme kvoli moznosti mensie rovne
else if (c == '>')
state = AUT_GREAT; // moznost vacsie rovne
else if (c == '=')
state = AUT_EQUALS; // moznost rovnsti
else if (c == '!')
state = AUT_NOT_EQUALS; // moznost nerovnosti
else if (c == ';') {
state = NEUTRAL_STATE; //Neviem ci tu mam zahrnovat aj dvojbodku, bodkociarku, bodku atd... toto som tu dal ale neviem ci to treba
*symbol = SYM_SEMI;
return AUT_SYMBOL;
}
// moznost AND
else if (c == '&')
state = AUT_AND;
//moznost OR
else if (c == '|')
state = AUT_OR;
//skaceme na stav String
else if (c == '"')
state = AUT_STRING;
else {
if (!jirkaDouble) {
FERROR(ERR_LEXER, "Invalid character occurred (%c)", c);
} else {
FERROR(ERR_RUNTIME_INT_PARSE, "Invalid character occurred (%c)", c);
}
}
break;
case AUT_IDEN:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
//ak to uz nie je jedno z povolenych znakov tak..
if (! (isalnum(c) || c == '_' ||c == '$' )) {
if (c == '.') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_IDEN2;
} else {
state = Start_state;
return control_res_key_word(*string, reserved);
}
}
string_end(orig, c, &origLength, &origAlloc);
//nenastavil som ret IDEN STATE, kvoli implicitnemu vracaniu
break;
case AUT_IDEN2:
control_res_key_word(*string, reserved);
//kvoli lavej strane zlozeneho iden ! void. napr..
if(c =='.' && *reserved != 0){
string_end(orig, c, &origLength, &origAlloc);
state=Start_state;
return ERROR_IDEN;
}
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if ((c>='A' && c<='z') || c == '_' || c == '$') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_IDEN3;
} else {
state = Start_state;
return ERROR_IDEN;
}
break;
case AUT_IDEN3:
control_res_key_word(*string, reserved);
if(c =='.' && *reserved != 0){
//kvoli lavej strane compound ! void. napr..
state=Start_state;
return ERROR_IDEN;
}
string_end(orig, c, &origLength, &origAlloc);
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if(! (isalnum(c) || c == '_' ||c == '$' )) {
// vezme bodku a zprava string napr .for
if(strchr(*string,'.'))
string_end(orig, c, &origLength, &origAlloc);
// strch +1 vymaze bodku a ostane mi uz iba res a neres
control_res_key_word(strchr(*string,'.')+1, reserved);
if(*reserved != 0){
state = Start_state;
return ERROR_IDEN;
}
state = Start_state;
return IDEN_COMPOUND;
}
break;
case AUT_NUM:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
//podmienka if aby pri cislach sa vzdy vracalo spat na num
if (isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
} else if (c == '.') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_FLOAT1;
} else if (c == 'E' || c == 'e') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_EX1;
} else {
state = Start_state;
return NUMBER;
}
break;
//desatine cisla
case AUT_FLOAT1:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if (isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_FLOAT2;
} else {
state = Start_state;
return ERROR_NUMBER;
}
break;
case AUT_FLOAT2:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
//podmienka if aby pri cislach sa vzdy vracalo spat na float
if (isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
} else if (c == 'E' || c == 'e') {
string_end(orig, c, &origLength, &origAlloc);
//prechadzame do stavu s exponentom
state = AUT_EX1;
} else {
state = Start_state;
return FLOAT;
}
break;
case AUT_EX1:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if (c == '+' || c == '-') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_EX2;
} else if (isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_EX3;
} else {
state = Start_state;
return ERROR_NUMBER;
}
break;
case AUT_EX2:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if (isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_EX3;
} else {
state = Start_state;
return ERROR_NUMBER;
}
break;
case AUT_EX3:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
//ak nie je cislo tak nepokracujeme ale vraciame sa na start
if (! isdigit(c)) {
state = Start_state;
return FLOAT;
}
string_end(orig, c, &origLength, &origAlloc);
break;
case AUT_STRING:
//vynulovanie num
num = 0;
GET_CHAR(c, input, state, line, lineCol);
if(c == '"') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
return STRING;
} else if(c == '\\') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_ESC;
} else if(c == EOF) {
state = Start_state;
return ERROR_ESC;
//len urcite povolene znaky z ASCI
} else if (c > 31 && c <= 255) {
string_end(orig, c, &origLength, &origAlloc);
string_end(string, c, &stringLength, &stringAlloc);
} else {
state = Start_state ;
return ERROR_ASCII;
}
break;
//escape sekvencie
case AUT_ESC:
GET_CHAR(c, input, state, line, lineCol);
if(c == 'n'){
string_end(orig, c, &origLength, &origAlloc);
state = AUT_STRING;
string_end(string, c='\n', &stringLength, &stringAlloc);
} else if(c == 't') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_STRING;
string_end(string, c='\t', &stringLength, &stringAlloc);
} else if(c == '\\' || c == '"') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_STRING;
string_end(string, c, &stringLength, &stringAlloc);
} else if(isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
// cislo moze byt iba v tomto rozmedzi
if(c > '0' && c <= '3' ) {
num += (c - '0')*64;
state = AUT_ESCN;
}
else if (c == '0') {
state = AUT_ESC_ZERO;
}
else {
state = Start_state;
return ERROR_ESC;
}
}
else {
state = Start_state;
return ERROR_ESC;
}
break;
//nulovy stav
case AUT_ESC_ZERO:
GET_CHAR(c, input, state, line, lineCol);
if(isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
if (c == '0'){
// ak dva nuly
state = AUT_ESC_ZERO2;
} else if (c > '0' && c <= '7' ) {
//moznost dostat sa do stavu z cislami > 0
num += (c - '0')*8;
state = AUT_ESCN2;
} else {
state = Start_state;
return ERROR_ESC_ZERO;
}
}
else {
state = Start_state;
return ERROR_ESC_ZERO;
}
break;
// stav s cislami >0
case AUT_ESCN:
GET_CHAR(c, input, state, line, lineCol);
if(isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
if(c >= '0' && c <= '7' ) {
num += ((c - '0')*8);
state = AUT_ESCN2;
} else {
state = Start_state;
return ERROR_ESCN;
}
}
else {
state = Start_state;
return ERROR_ESCN;
}
break;
case AUT_ESC_ZERO2:
GET_CHAR(c, input, state, line, lineCol);
if(isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
// mame tri 0 cize error
if (c == '0') {
state = Start_state;
return ERROR_ESC_ZERO2;
} else if (c > '0' && c <= '7'){
num += (c - '0');
string_end(string, c, &stringLength, &stringAlloc);
state = AUT_STRING;
} else {
state = Start_state;
return ERROR_ESC_ZERO2;
}
}
else {
state = Start_state;
return ERROR_ESC_ZERO2;
}
break;
case AUT_ESCN2:
GET_CHAR(c, input, state, line, lineCol);
if(isdigit(c)) {
string_end(orig, c, &origLength, &origAlloc);
// ziskame povolene cislo pozostavajuce z 3 cislic
if(c >= '0' && c <= '7' ) {
num +=(c - '0');
string_end(string, num, &stringLength, &stringAlloc);
state = AUT_STRING;
} else {
state = Start_state;
return ERROR_ESCN2;
}
}
else {
state = Start_state;
return ERROR_ESCN2;
}
break;
//prechod do komentov pripadne vrateniu SLASH
case AUT_DIV:
GET_CHAR(c, input, state, line, lineCol);
if(c== '/')
//posielame do coment line
state = AUT_CMTL;
else if(c=='*')
// posleme do coment bloku
state = AUT_CMTB;
else {
//mozem hned vratit SLASH
state = Start_state;
*symbol = SYM_SLASH;
return AUT_SYMBOL;
}
break;
//coment line
case AUT_CMTL:
GET_CHAR(c, input, state, line, lineCol);
if(c == '\n' || c == EOF) {
state = NEUTRAL_STATE;
}
break;
//Coment Block
case AUT_CMTB :
// iba jednotlive fgetc bez getchar lebo nastavala chyba EOF
c = fgetc(input);
if(c == '*')
state = AUT_CMTB_END;
else if(c == '\n')
continue;
else if(c == EOF)
return ERROR_CMTB;
break;
case AUT_CMTB_END:
GET_CHAR(c, input, state, line, lineCol);
if(c == '/')
state = NEUTRAL_STATE;
else if (c == '*')
state = AUT_CMTB_END;
else
state = AUT_CMTB;
break;
// vsety mozne symboly
//equals alebo assign
case AUT_EQUALS:
GET_CHAR(c, input, state, line, lineCol);
if(c == '=') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_EQUALS;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_ASSIGN;
return AUT_SYMBOL;
}
break;
//less lebo less equal
case AUT_LESS:
GET_CHAR(c, input, state, line, lineCol);
if(c == '=') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_LESS_EQUAL;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_LESS;
return AUT_SYMBOL;
}
break;
//greater alebo greater equal
case AUT_GREAT:
GET_CHAR(c, input, state, line, lineCol);
if(c == '=') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_GREATER_EQUAL;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_GREATER;
return AUT_SYMBOL;
}
break;
//not alebo not equals
case AUT_NOT_EQUALS:
GET_CHAR(c, input, state, line, lineCol);
if(c == '=') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_NOT_EQUALS;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_NOT;
return AUT_SYMBOL;
}
break;
//rozsirenia UNARY + a -
case AUT_UNARY_PL:
GET_CHAR(c, input, state, line, lineCol);
if(c == '+') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_UNARY_PLUS;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_PLUS;
return AUT_SYMBOL;
}
break;
case AUT_UNARY_MN:
GET_CHAR(c, input, state, line, lineCol);
if(c == '-') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_UNARY_MINUS;
return AUT_SYMBOL;
} else {
state = Start_state;
*symbol = SYM_MINUS;
return AUT_SYMBOL;
}
break;
// rozsirenia AND
case AUT_AND:
GET_CHAR(c, input, state, line, lineCol);
if(c == '&') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_AND;
return AUT_SYMBOL;
} else {
state = Start_state;
return ERROR_AND;
}
break;
//rozsirenie OR
case AUT_OR:
GET_CHAR(c, input, state, line, lineCol);
if(c == '|') {
string_end(orig, c, &origLength, &origAlloc);
state = NEUTRAL_STATE;
*symbol = SYM_OR;
return AUT_SYMBOL;
} else {
state = Start_state;
return ERROR_OR;
}
break;
// Rozsirenie BASE
case AUT_BIN:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if (c>='0' && c<='7') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_OCT;
} else if (c == 'b') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_BIN2;
} else if (c == 'x') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_HEX;
} else if (c == '.') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_FLOAT1;
} else {
state = Start_state;
return NUMBER;
}
break;
//dvojkova sustava
case AUT_BIN2:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if(c>='0' && c<='1') {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_BIN2;
} else {
state = Start_state;
return EXT_BASE;
}
break;
case AUT_HEX:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if(isdigit(c) || (c>='A' && c<='F') || (c>='a' && c<='f') ) {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_HEX;
} else if ( c == '.') {
string_end(orig, c, &origLength, &origAlloc);
//po nacitani bodky do dalsieho stavu
state = AUT_HEX1;
} else {
state = Start_state;
return EXT_HEX;
}
break;
//desatiny stav 16kovej sustavy
case AUT_HEX1:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if((isdigit(c)) || (c>='A' && c<='F') || (c>='a' && c<='f') ) {
string_end(orig, c, &origLength, &origAlloc);
state = AUT_HEX1;
} else if ( c == 'p' || c == 'P') {
string_end(orig, c, &origLength, &origAlloc);
// po nacitani p,P ideme do dalsieho potrebneho stavu
state = AUT_HEX2;
} else {
state = Start_state;
return ERROR_HEX;
}
break;
case AUT_HEX2:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if ( c == '-') {
string_end(orig, c, &origLength, &origAlloc);
//po nacitani - sa dostavame do posledneho stavu
state = AUT_HEX3;
} else {
state = Start_state;
return ERROR_HEX;
}
break;
//finalny stav kde je lexer rozpozna desatine cislo 16kovej s.
case AUT_HEX3:
string_end(string, c, &stringLength, &stringAlloc);
GET_CHAR(c, input, state, line, lineCol);
if (isdigit(c)) {