-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.c
executable file
·3882 lines (3642 loc) · 88.6 KB
/
compiler.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
/* My own C compiler */
/* Date: 01 Oct 2007 */
/* Author: Srinivas Nayak */
/* Compile using gcc */
/* This code is distributed under the GPL License. */
/* For more info check: */
/* http://www.gnu.org/copyleft/gpl.html */
/*******************************************************************************
* This program comprises of a lexical analyzer along with a syntax analyzer
* for Programming Language C (C89) as defined in K&R book second edition.
*******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
char stack[100]; //stores temporarily the string of character
//being processed by lexical analyzer.
int tos=-1; //top of the stack.
FILE *f; //file pointer referring to the source file.
long pos=1; //only used in get_next_token function
//to count a '\n' in the source file as a new line,
//only once, even if we encounter the same
//'\n' character many times while moving forward
//and backword,passing over it.
int line_number=1; //this is used to hold the line number of
//source file at which the compiler failed (or sucseed).
int identifier_flag=0; //become true only when lexical analyzer
//found an identifier.
int integer_constant_flag=0; //same as above; name is self explanatory.
int floating_constant_flag=0;
int character_constant_flag=0;
int string_flag=0;
void reset_flags();//clear the flags.
//prints the data processed by parser till now
void print_processed_data(long start_pos, long end_pos);
//each of these below functions are utility functions
//which helps lexical analyzer.
int isdigit(char);// returns true if the character provided to it is a digit.
int isletter(char);// returns true if the character provided to it is a letter.
int isspecial(char);// returns true if the character provided to it
// is a character other than digit or letter.
//stack utility functions.
void push(char ch);
void pop();
void flush(); //flushes our stack.
void token_error();//prints error message if lexical analyzer
//find some non-recognizable token
int get_next_token(); //our LEXICAL ANALYZER. It scans the next token
//in the source file and saves it in stack
//and returns true if it is a valid C token.
int check_terminal(char *terminal);//this function when provided a
//C keyword/token/terminal symbol, returns true or false
//based on whether lexical analyzer found the same
//keyword/token/terminal symbol as provided or not.
//It compares the current stack content to the provided
//string to see if they are the same.
//these below are the functions each corresponding to a NONterminal
//in C grammar as defined in C89.
int translation_unit();
int external_declaration();
int function_definition();
int declaration();
int declaration_list();
int declaration_specifiers();
int storage_class_specifier();
int type_specifier();
int type_qualifier();
int struct_or_union_specifier();
int struct_or_union();
int struct_declaration_list();
int init_declarator_list();
int init_declarator();
int struct_declaration();
int specifier_qualifier_list();
int struct_declarator_list();
int struct_declarator();
int enum_specifier();
int enumerator_list();
int enumerator();
int declarator();
int direct_declarator();
int pointer();
int type_qualifier_list();
int parameter_type_list();
int parameter_list();
int parameter_declaration();
int identifier_list();
int initializer();
int initializer_list();
int type_name();
int abstract_declarator();
int direct_abstract_declarator();
int typedef_name();
int statement();
int labeled_statement();
int expression_statement();
int compound_statement();
int statement_list();
int selection_statement();
int iteration_statement();
int jump_statement();
int expression();
int assignment_expression();
int assignment_operator();
int conditional_expression();
int constant_expression();
int logical_OR_expression();
int logical_AND_expression();
int inclusive_OR_expression();
int exclusive_OR_expression();
int AND_expression();
int equality_expression();
int relational_expression();
int shift_expression();
int additive_expression();
int multiplicative_expression();
int cast_expression();
int unary_expression();
int unary_operator();
int postfix_expression();
int primary_expression();
int argument_expression_list();
int constant();
//these below are the functions each corresponding to a
//terminal/token in C grammar(C89).
int identifier();
int string();
int integer_constant();
int floating_constant();
int enumeration_constant();
int character_constant();
//each of these below defines corresponds to a keyword in
//C grammar(C89). These have been defined to a function call,
//which when provided the actual C keyword, returns true or false
//based on whether lexical analyzer found the same keyword as provided or not.
#define VOID (check_terminal("void"))
#define AUTO (check_terminal("auto"))
#define REGISTER (check_terminal("register"))
#define STATIC (check_terminal("static"))
#define EXTERN (check_terminal("extern"))
#define TYPEDEF (check_terminal("typedef"))
#define CHAR (check_terminal("char"))
#define SHORT (check_terminal("short"))
#define INT (check_terminal("int"))
#define LONG (check_terminal("long"))
#define FLOAT (check_terminal("float"))
#define DOUBLE (check_terminal("double"))
#define SIGNED (check_terminal("signed"))
#define UNSIGNED (check_terminal("unsigned"))
#define CONST (check_terminal("const"))
#define VOLATILE (check_terminal("volatile"))
#define STRUCT (check_terminal("struct"))
#define UNION (check_terminal("uion"))
#define ENUM (check_terminal("enum"))
#define CASE (check_terminal("case"))
#define DEFAULT (check_terminal("default"))
#define IF (check_terminal("if"))
#define ELSE (check_terminal("else"))
#define SWITCH (check_terminal("switch"))
#define WHILE (check_terminal("while"))
#define DO (check_terminal("do"))
#define FOR (check_terminal("for"))
#define GOTO (check_terminal("goto"))
#define CONTINUE (check_terminal("continue"))
#define BREAK (check_terminal("break"))
#define RETURN (check_terminal("return"))
//each of these below defines corresponds to a terminal symbol/operator
//in C grammar(C89). These have been defined to a function call,
//which when provided the actual C terminal symbol/operator,
//returns true or false based on whether lexical analyzer found
//the same terminal symbol/operator as provided or not.
//Each group of defines has been listed in decreasing order of precedence.
//In a group, allhave the same precedence.
//------------------start-----------------
#define LEFT_PARENTHESIS (check_terminal("("))
#define RIGHT_PARENTHESIS (check_terminal(")"))
#define LEFT_BRACKET (check_terminal("["))
#define RIGHT_BRACKET (check_terminal("]"))
#define ARROW (check_terminal("->"))
#define DOT (check_terminal("."))
//-------------------------------
#define NOT (check_terminal("!"))
#define TILDE (check_terminal("~"))
#define PLUS_PLUS (check_terminal("++"))
#define MINUS_MINUS (check_terminal("--"))
#define PLUS (check_terminal("+"))
#define MINUS (check_terminal("-"))
#define ASTERISK (check_terminal("*"))
#define AMPERSAND (check_terminal("&"))
//(type)
#define SIZEOF (check_terminal("sizeof"))
//-----------------------------------
//*
#define FRONT_SLASH (check_terminal("/"))
#define PERCENTAGE (check_terminal("%"))
//------------------------------------
//+
//-
//------------------------------------
#define LESSTHAN_LESSTHAN (check_terminal("<<"))
#define GREATERTHAN_GREATERTHAN (check_terminal(">>"))
//------------------------------------
#define LESSTHAN (check_terminal("<"))
#define LESSTHAN_EQUAL (check_terminal("<="))
#define GREATERTHAN (check_terminal(">"))
#define GREATERTHAN_EQUAL (check_terminal(">="))
//--------------------------------------
#define EQUAL_EQUAL (check_terminal("=="))
#define NOT_EQUAL (check_terminal("!="))
//-------------------------------------
//&
//-------------------------------------
#define CARAT (check_terminal("^"))
//----------------------------------
#define PIPE (check_terminal("|"))
//--------------------------------
#define AMPERSAND_AMPERSAND (check_terminal("&&"))
//------------------------------------
#define PIPE_PIPE (check_terminal("||"))
//-----------------------------------
//both make a single operator
#define QUESTION_MARK (check_terminal("?"))
#define COLON (check_terminal(":"))
//----------------------------------
#define EQUAL (check_terminal("="))
#define PLUS_EQUAL (check_terminal("+="))
#define MINUS_EQUAL (check_terminal("-="))
#define ASTERISK_EQUAL (check_terminal("*="))
#define FRONTSLASH_EQUAL (check_terminal("/="))
#define PERCENTAGE_EQUAL (check_terminal("%="))
#define AMPERSAND_EQUAL (check_terminal("&="))
#define CARAT_EQUAL (check_terminal("^="))
#define PIPE_EQUAL (check_terminal("|="))
#define LESSTHAN_LESSTHAN_EQUAL (check_terminal("<<="))
#define GREATERTHAN_GREATERTHAN_EQUAL (check_terminal(">>="))
//----------------------------------
#define COMMA (check_terminal(","))
//--------------end-----------------
//following are not operators but terminal symbols
#define LEFT_BRACE (check_terminal("{"))
#define RIGHT_BRACE (check_terminal("}"))
#define SEMICOLON (check_terminal(";"))
#define ELLIPSIS (check_terminal("..."))
void reset_flags()
{
//clear all the flags.
identifier_flag=0;
integer_constant_flag=0;
floating_constant_flag=0;
character_constant_flag=0;
string_flag=0;
}
int check_terminal(char *terminal)
{
int retval;
long pos; //stores the current offset of the source file
//upon entering to this function.
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1) //if the lexical analyzer
//found a valid C token.
{
if(strcmp(terminal,stack)==0) //compare the string in the stack
//which has been stored by lexical analyzer
//upon finding the token with the string passed to
//this function. If they are equal go into.
{
// printf("\n-----%s-----\n\n",stack);
flush(); // clean the stack.
reset_flags(); //reset all the flags.
return 1; //return true to caller saying that
//caller got the token which one he wanted.
}
else //if caller didn't get what he wanted,
{
flush(); //still clean the stack,because some token will be there
//although caller didn't get what he wanted.
fseek(f,pos,SEEK_SET); //since caller didn't get what he wanted,
//the token found will not be processed,so go back to
//the position from where we have started looking for
//next token.
reset_flags(); //still clean the flags, because some token of
//some type was found and any one of these may have been set.
return 0; //return failure notice to the caller.
}
}
else //if lexical analyser got an invalid C token or it got EOF.
{
flush(); //make sure that the stack is clean
//before leaving this function.
fseek(f,pos,SEEK_SET); //before returning,make sure that
//we don't disturb the position till which processiong
//has been done. That is, return without disturbing
//the file position from where we have started
//while entering to this function.
reset_flags(); //also make sure no flag is set.
return 0; //return failure notice to the caller.
}
}
int identifier()
{
//all the comments of check_terminal function (above) is valid.
int retval;
long pos;
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1)
{
if (identifier_flag == 1) //if an identifier is found
{
// printf("\n-----%s-----\n\n",stack);
flush();
reset_flags();
return 1;
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
int string()
{
//all the comments of check_terminal function (above) are valid here.
int retval;
long pos;
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1)
{
if (string_flag == 1)//if a C string is found.
{
// printf("\n-----%s-----\n\n",stack);
flush();
reset_flags();
return 1;
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
int integer_constant()
{
//all the comments of check_terminal function (above) are valid here.
int retval;
long pos;
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1)
{
if (integer_constant_flag == 1)//if an integer constant is found.
{
// printf("\n-----%s-----\n\n",stack);
flush();
reset_flags();
return 1;
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
int floating_constant()
{
//all the comments of check_terminal function (above) are valid here.
int retval;
long pos;
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1)
{
if (floating_constant_flag == 1)//if a floating constant is fount.
{
// printf("\n-----%s-----\n\n",stack);
flush();
reset_flags();
return 1;
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
int character_constant()
{
//all the comments of check_terminal function (above) are valid here.
int retval;
long pos;
pos = ftell(f);
if((retval=get_next_token())!=EOF && retval == 1)
{
if (character_constant_flag == 1)//if a character constant is found.
{
// printf("\n-----%s-----\n\n",stack);
flush();
reset_flags();
return 1;
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
else
{
flush();
fseek(f,pos,SEEK_SET);
reset_flags();
return 0;
}
}
int enumeration_constant()
{
//all the comments of check_terminal function (above) are valid here.
flush();
return 0;
}
int main(int argc,char **argv)
{
int retval;
if(argc!=2) //if a source file name is not given, exit.
exit(0);
f=fopen(argv[1],"r"); //open the source file to read.
if(f==NULL)
{
printf("error");
exit(0);
}
translation_unit(); //check if the source file contains a C translation unit.
return 0;
}
//our lexical analyzer.
int get_next_token()
{
char ch=0; //stores each character read from the source file.
int state=0; //current state of the state machine.
reset_flags(); //make sure no flag is set before we proceed.
s0:
state = 0; // we are in state 0.
ch=getc(f); //get the next character from the source file.
if(ch==EOF) //if we reach at EOF then return to the caller
//saying so.
return EOF;
if(!(ch==' ' || ch=='\n' || ch =='\t' || ch ==0x0d))//if we get
//a non-space character, push it into the stack.
push(ch);
if(ch==' ' || ch=='\n' || ch =='\t' || ch ==0x0d) //if we get
//a space, goto state 0.
{
/* This below code(temporary) is to report line number where error found */
if (ch =='\n')
{
if(ftell(f)>pos)
{
pos=ftell(f);
line_number++;
}
}
/* This above code(temporary) is to report line number where error found */
goto s0;//if we get a space, goto state 0.
}
else if(isletter(ch) || ch=='_')
{
goto s1; //we may get an identifier, so go to state 1.
}
else if(ch=='.')
{
goto s3; //we may now get a floating point constant,
//so go to state 3.
}
else if(isdigit(ch))
{
goto s2; //it can be an integer constant,so go to state 2.
}
else if(ch=='\'')
{
goto s4; //may be a character constant,so go to state 4.
}
else if(ch=='\"')
{
goto s8; //we may get a string constant,so go to state 8.
}
else if(isspecial(ch)) //if non of the above, it may be a
//C operator or a C terminal.
{
goto s10; //so go to state 10.
}
else
{
goto END; // else go to END to see what we get.
}
s1: //final state for identifier.
state = 1;
ch=getc(f);
push(ch);
if(isletter(ch) || isdigit(ch) || ch == '_')
{
goto s1; //we may get an identifier.
}
else
{
goto END; // else go to END to see what we get.
}
s2: //final state for integer constant
state = 2;
ch=getc(f);
push(ch);
if(ch == '.')
{
goto s3; //we may get a floating point constant,
//so go to state 3.
}
else if(isdigit(ch))
{
goto s2; //it may be an integer constant.
}
else
{
goto END; // else go to END to see what we get.
}
s3: //final state for dot operator (used with structures).
state = 3;
ch=getc(f);
push(ch);
if(isdigit(ch))
{
goto s2; //we may get an floating constant,so go to state 2.
}
else if(ch == '.')
{
goto s33;//we may get an ellipsis.
}
else
{
goto END; // else go to END to see what we get.
}
s4:
state = 4;
ch=getc(f);
push(ch);
if(isletter(ch) || (isspecial(ch) && ch !='\'' && ch !='\\')
|| isdigit(ch))
{
goto s6; //this may be the character of
//that character constant, so go to state 6.
}
else if(ch == '\\')
{
goto s5; //it could be an escape character, so go to state 5.
}
else
{
goto END; // else go to END to see what we get.
}
s5:
state = 5;
ch=getc(f);
push(ch);
if(isletter(ch) || isspecial(ch) || isdigit(ch))
{
goto s6; //it could be the character of that escape character,
//so go to state 6.
}
else
{
goto END; // else go to END to see what we get.
}
s6:
state = 6;
ch=getc(f);
push(ch);
if(ch == '\'')
{
goto s7; //we might have got a (escape)character constant,
//so go to state 7.
}
else
{
goto END; // else go to END to see what we get.
}
s7: //final state for character constant
state = 7;
goto END; //go to END to see what we get.
s8:
state = 8;
ch=getc(f);
push(ch);
if(isletter(ch) || (isspecial(ch) && ch != '\"') || isdigit(ch))
{
goto s8; //could be the character of that string constant,
//so go to state 8.
}
else if(ch == '\"')
{
goto s9; // we might have got a string constant.
}
else
{
goto END; // else go to END to see what we get.
}
s9: //final state of string constant.
state = 9;
goto END; // go to END to see what we get.
s10: //final state for symbols(eg. parenthesis etc.)
state = 10;
fseek(f,-1,SEEK_CUR); //we are in state 10,it means we
//consumed a character which might be a part of an operator
//or a special symbol (eg. left parenthesis etc.) which
//we have already consumed. So go back one character
//and move forward to see what it is...
pop(); //since we are going back one character,
//we must pop it from the stack, because before we know
//what character it is, we have put it into the stack.
ch=getc(f);
push(ch);
if(ch=='-')
goto s11;
else if(ch=='!')
goto s14;
else if(ch=='~')
goto s16;
else if(ch=='+')
goto s17;
else if(ch=='*')
goto s19;
else if(ch=='/')
goto s20;
else if(ch=='%')
goto s21;
else if(ch=='<')
goto s22;
else if(ch=='>')
goto s24;
else if(ch=='=')
goto s26;
else if(ch=='&')
goto s27;
else if(ch=='^')
goto s29;
else if(ch=='|')
goto s30;
else if(ch==',')
goto s32;
else
goto END; // else go to END to see what we get.
s11: //final state for -
state = 11;
ch=getc(f);
push(ch);
if(ch=='-')
goto s12;
else if(ch=='=')
goto s15;
else if(ch=='>')
goto s13;
else
goto END; // else go to END to see what we get.
s12: //final state for --
state = 12;
goto END; // go to END to see what we get.
s13: //final state for ->
state = 13;
goto END; // go to END to see what we get.
s14: //final state for !
state = 14;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s15: //final state for !=,+=,-=,*=,etc.
state = 15;
goto END; // go to END to see what we get.
s16: //final state for ~
state = 16;
goto END; // go to END to see what we get.
s17: //final state for +
state = 17;
ch=getc(f);
push(ch);
if(ch=='+')
goto s18;
else if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s18: //final state for ++
state = 18;
goto END; // go to END to see what we get.
s19: //final state for *
state = 19;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s20: //final state for /
state = 20;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s21: //final state for %
state = 21;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s22: //final state for <
state = 22;
ch=getc(f);
push(ch);
if(ch=='<')
goto s23;
else if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s23: //final state for <<
state = 23;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END;
s24: //final state for >
state = 24;
ch=getc(f);
push(ch);
if(ch=='>')
goto s25;
else if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s25: //final state for >>
state = 25;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s26: //final state for =
state = 26;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s27: //final state for &
state = 27;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else if(ch=='&')
goto s28;
else
goto END; // else go to END to see what we get.
s28: //final state for &&
state = 28;
goto END; // go to END to see what we get.
s29: //final state for ^
state = 29;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else
goto END; // else go to END to see what we get.
s30: //final state for |
state = 30;
ch=getc(f);
push(ch);
if(ch=='=')
goto s15;
else if(ch=='|')
goto s31;
else
goto END; // else go to END to see what we get.
s31: //final state for ||
state = 31;
goto END; // go to END to see what we get.
s32: //final state for ,
state = 32;
goto END; // go to END to see what we get.
s33:
state = 33;
ch=getc(f);
push(ch);
if(ch=='.')
goto s34;// we may get an ellipsis.
else
goto END; // go to END to see what we get.
s34: //final state for ...(ellipsis)
state = 34;
goto END; // go to END to see what we get.
END:
if(!(state==7 //after coming to this state, we directly came to END,
//so no extra character has been consumed by
//the lexical analyzer and also no extra character
//pused in to stack, so for this case,we need not
//go one character back as well as
//need not pop the stack.
|| state==9 //same for this case
|| state==10 //when we reached state 10, we immediately
//went one character back and then we lookforward
//for a character which may be either an operator
//or a symbol. So when we reached END,
//it is because we got a symbol,and itis not
//a C operator,so we have that symbol on the stack
//and we have already consumed it knowingly,
//so no need to regret,no need to go back one character
//and no need to pop the stack.
|| state==12 //same as case of state 7, and also it is same
//for rest of the states listed here.
|| state==13
|| state==15
|| state==16
|| state==18
|| state==28
|| state==31
|| state==32
|| state==34
))
{
fseek(f,-1,SEEK_CUR); //go back one character
pop(); //also pop that unwanted charcter from the stack
}
if(state==1 // we got an identifier.
|| state==2 //we got either an integer constant
//or a floating point constant
|| state==3 //we got dot(.) operator
|| state==7 //we got a character constant
|| state==9 //we got a string constant
|| state==10 //we got a special symbol (eg. parenthesis etc.)
|| (state>=11 && state<=32) //we got an operator
|| state==34 //we got an ellipsis(...)
)
{
switch(state)
{
case 1:
if (
(strcmp(stack,"void")!=0)
&& (strcmp(stack,"auto")!=0)
&& (strcmp(stack,"register")!=0)
&& (strcmp(stack,"static")!=0)
&& (strcmp(stack,"extern")!=0)
&& (strcmp(stack,"typedef")!=0)
&& (strcmp(stack,"char")!=0)
&& (strcmp(stack,"short")!=0)
&& (strcmp(stack,"int")!=0)
&& (strcmp(stack,"long")!=0)
&& (strcmp(stack,"double")!=0)
&& (strcmp(stack,"float")!=0)
&& (strcmp(stack,"signed")!=0)
&& (strcmp(stack,"unsigned")!=0)
&& (strcmp(stack,"const")!=0)
&& (strcmp(stack,"volatile")!=0)
&& (strcmp(stack,"struct")!=0)
&& (strcmp(stack,"union")!=0)
&& (strcmp(stack,"enum")!=0)
&& (strcmp(stack,"case")!=0)
&& (strcmp(stack,"default")!=0)
&& (strcmp(stack,"if")!=0)
&& (strcmp(stack,"else")!=0)
&& (strcmp(stack,"switch")!=0)
&& (strcmp(stack,"while")!=0)
&& (strcmp(stack,"do")!=0)
&& (strcmp(stack,"for")!=0)
&& (strcmp(stack,"goto")!=0)
&& (strcmp(stack,"continue")!=0)
&& (strcmp(stack,"break")!=0)
&& (strcmp(stack,"return")!=0)
&& (strcmp(stack,"sizeof")!=0)
)
{ // filter the keywords out, so that we only get
//user defind identifiers.
identifier_flag=1; //we got an user defined identifier
//printf("%s ","id");
}
break;
case 2:
{
integer_constant_flag=1; //either we got an
//integer constant
floating_constant_flag=1; //or we got a
//floating constant
//printf("%s ","int_const");
}
break;
case 3://we got a dot operator
//printf("%s ","op");
break;
case 7:
{
character_constant_flag=1; //we got a character constant
//printf("%s ","ch_const");
}
break;
case 9:
{
string_flag=1; //we got a string constant
//printf("%s ","st_const");
}
break;
case 10: //we got a special symbol (eg. parenthesis etc.)
case 34: //we got an ellipsis symbol
//printf("%s ","symbol");
break;
default: //we got an operator
//printf("%s ","op");
break;
}
}
else //something is wrong! we got an invalid C token,
//return a failure notice.
{