forked from sarnold/cyclo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.C
1292 lines (1126 loc) · 32.5 KB
/
main.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
/*
(c) 1993 Roger Binns
These tools were produced by Roger Binns for a fourth year project as part of
a computer science degree, for the Computer Science department, Brunel
University, Uxbridge, Middlesex UB8 3PH, United Kingdom.
This software is provided in good faith, having been developed by Brunel
University students as part of their normal course work. It should not be
assumed that Brunel has any rights of ownership, and the University cannot
accept any liability for its subsequent use. It is a condition of any such
use that the user idemnifies the University against any claim (including
third party claims) arising therefrom.
*/
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
using namespace std;
#define extern /* instantiate lexer variables */
#include "tokens.h"
#undef extern
extern "C" TOKENS yylex (void);
struct Token
{
enum TOKENS type; /* token type (from tokens.h) */
int lineno; /* lineno the token appeared on */
char *string; /* optional information */
Token ()
{
string = 0;
}
~Token ()
{
if (string)
free (string);
string = 0;
}
};
#undef _LIST_CLASSNAME
#undef _LIST_CLASSTYPE
#define _LIST_CLASSNAME TokenList
#define _LIST_CLASSTYPE Token
#include "list.h"
#undef _LIST_CLASSNAME
#undef _LIST_CLASSTYPE
#define _LIST_CLASSNAME IntList
#define _LIST_CLASSTYPE int
#include "list.h"
struct Place
{
int tokenno; /* which token this place corresponds to */
int node; /* node number of this place */
IntList flows; /* where we can go from this place */
int implicitnext; /* do we implicitly go to the next place */
int display; /* display this place in flowchart? */
Place ()
{
tokenno = -1;
implicitnext = 0;
display=1;
node=0;
}
};
#undef _LIST_CLASSNAME
#undef _LIST_CLASSTYPE
#define _LIST_CLASSNAME PlaceList
#define _LIST_CLASSTYPE Place
#include "list.h"
struct Function
{
char *name;
PlaceList placelist; /* list of places in function */
int inl; /* was it inline (nested in a struct/class/union) */
int start; /* token number function starts on (opening brace) */
int end; /* token number function ends on (closing brace) */
Function ()
{
name = 0;
inl = start = end = -1;
}
~Function ()
{
if (name)
free (name);
name = 0;
}
};
#undef _LIST_CLASSNAME
#undef _LIST_CLASSTYPE
#define _LIST_CLASSNAME FunctionList
#define _LIST_CLASSTYPE Function
#include "list.h"
#undef _STACK_TYPE
#undef _STACK_CLASSNAME
#define _STACK_TYPE int
#define _STACK_CLASSNAME ScuStack
#include "stack.h"
TokenList tokenlist; /* list of tokens */
FunctionList funclist; /* list of functions */
ScuStack scustack; /* nesting of struct/class/union scopes */
int ignoreinline; /* should inline functions be ignored */
struct Info /* for the recursive descent */
{
int ibreak; /* token number that keyword jumps to */
int icontinue;
int ireturn;
int iswitch; /* location of last switch statement */
Function *function; /* current Function */
Info ()
{
ibreak = icontinue = ireturn = iswitch = -1;
function = 0;
}
};
/* consistency checking macro */
#define CHECK(x) { if(!(x)) { cerr << "Check " << #x << " failed in " << __FILE__ << " line " << __LINE__ << endl; COREDUMP;} }
/* give Mr. User a nice output */
void print_token(int);
void pretty_token(int, ostream &);
/* some convenience macros - note that they also reduce the cyclomatic number! */
#define OUTOFMEMORY { cerr << "Out of memory in " << __FILE__ << " line " << __LINE__ << endl; exit(1); }
#define CHKMEM(x) { if(!(x)) {cerr << "Out of memory in " << __FILE__ << " line " << __LINE__ << endl; exit(1);} }
/* recursive descent parser */
static int rd_buffer(int parse, int startat, Info &jumps);
static int rd_scudefinition(int parse, int startat, Info &jumps);
static int rd_topscope(int parse, int startat, Info &jumps);
static int rd_topstatement(int parse, int startat, Info &jumps);
static int rd_functiondefinition(int parse, int startat, Info &jumps);
static int rd_statements(int parse, int startat, Info &jumps);
static int rd_statement(int parse, int startat, Info &jumps);
static int rd_scope(int parse, int startat, Info &jumps);
static int rd_while(int parse, int startat, Info &jumps);
static int rd_switch(int parse, int startat, Info &jumps);
static int rd_return(int parse, int startat, Info &jumps);
static int rd_if(int parse, int startat, Info &jumps);
static int rd_elseif(int parse, int startat, Info &jumps);
static int rd_else(int parse, int startat, Info &jumps);
static int rd_goto(int parse, int startat, Info &jumps);
static int rd_for(int parse, int startat, Info &jumps);
static int rd_do(int parse, int startat, Info &jumps);
static int rd_default(int parse, int startat, Info &jumps);
static int rd_case(int parse, int startat, Info &jumps);
static int rd_break(int parse, int startat, Info &jumps);
static int rd_continue(int parse, int startat, Info &jumps);
/* what to do if a rd_* function returns an error */
#define check(x) {end=(x); if (end<-1) return end;}
/* macro to handle parse error */
#define PARSEFAIL(m,x) { parsefail(m,x); return -10;}
void parsefail(char *msg, int at)
{
cerr << "Parsing failure \"" << msg << "\" - detectected at token ";
pretty_token(at, cerr);
cerr << " at line number " << tokenlist[at].lineno << endl;
}
/* Adds a new place to the current function */
Place *AddPlace(Info & jumps, int tokennum, int impnext=1)
{
Place *p;
CHKMEM(p=new Place);
p->implicitnext=impnext;
p->tokenno=tokennum;
CHKMEM(jumps.function->placelist.Add(p));
return p;
}
/* Adds 'jump' to flows for place corresponding to 'which' */
void AddToPlace(Info &jumps, int which, int jump)
{
int *t;
CHKMEM(t=new int);
*t=jump;
CHECK((jumps.function->placelist[which-jumps.function->start].tokenno)==which);
CHKMEM(jumps.function->placelist[which-jumps.function->start].flows.Add(t));
}
/* Turns implicitnext off for a token */
void NoImplicit(Info &jumps, int which)
{
CHECK(jumps.function->placelist[which-jumps.function->start].tokenno==which);
jumps.function->placelist[which-jumps.function->start].implicitnext=0;
}
/* The actual recursive descent parser starts here */
int rd_buffer(int parse, int startat, Info &jumps)
{
int end;
while(startat<tokenlist.Count())
{
if(tokenlist[startat].type==ENDSCOPE)
break;
check(rd_topstatement(parse, startat, jumps));
if(end==-1)
return -1;
startat=end;
}
return startat;
}
int rd_scudefinition(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=CLASS && tokenlist[startat].type!=STRUCT && tokenlist[startat].type!=UNION)
return -1;
if(startat+1>=tokenlist.Count() || tokenlist[startat+1].type!=BEGINSCOPE)
return -1;
int *t;
CHKMEM(t=new int);
*t=startat;
CHKMEM(scustack.Push(t));
check(rd_buffer(parse, startat+2, jumps));
if(end==-1) return end;
if(end>=tokenlist.Count() || tokenlist[end].type!=ENDSCOPE)
PARSEFAIL((char *)"No terminating '}' found", startat);
scustack.Pop();
return end+1;
}
int rd_topscope(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=BEGINSCOPE) return -1;
check(rd_buffer(parse, startat+1, jumps));
if(end==-1 || end>=tokenlist.Count())
PARSEFAIL((char *)"Invalid scope contents", startat);
if(tokenlist[end].type!=ENDSCOPE)
PARSEFAIL((char *)"No terminating '}' found", startat);
return end+1;
}
int rd_topstatement(int parse, int startat, Info &jumps)
{
int end;
while(startat<tokenlist.Count())
{
#define ISIT(x) check( x (0, startat, jumps)); \
if(end!=-1) \
{ \
if(parse) \
check( x (parse, startat, jumps)); \
return end; \
}
ISIT(rd_scudefinition);
ISIT(rd_functiondefinition);
ISIT(rd_topscope);
#undef ISIT
if(tokenlist[startat].type==ENDSCOPE)
return startat;
startat++;
}
return startat;
}
int rd_functiondefinition(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=FUNCTION)
return -1;
if(startat+1>=tokenlist.Count() || tokenlist[startat+1].type!=BEGINSCOPE)
return -1;
check(rd_statements(0, startat+2, jumps));
again:
if(end==-1)
PARSEFAIL((char *)"Unrecognised function body", startat);
if(end>=tokenlist.Count() || tokenlist[end].type!=ENDSCOPE)
PARSEFAIL((char *)"Function body has no terminating '}'", startat);
if(scustack.Size() && ignoreinline)
parse=0;
if(parse)
{
Info i=jumps;
Function *f;
CHKMEM(f=new Function);
CHKMEM(funclist.Add(f));
f->start=startat+1;
f->end=end;
if(scustack.Size())
{
int a;
int sz=0;
f->inl=1;
for(a=0; a<scustack.Size(); a++)
sz+=2+strlen(tokenlist[scustack.Top(a)].string);
sz+=strlen(tokenlist[startat].string);
CHKMEM(f->name=new char[sz+1]);
f->name[0]=0;
for(a=scustack.Size()-1; a>=0; a--)
{
strcat(f->name, tokenlist[scustack.Top(a)].string);
strcat(f->name, "::");
}
strcat(f->name, tokenlist[startat].string);
}
else
CHKMEM(f->name=strdup(tokenlist[startat].string));
i.function=f;
i.ireturn=end;
AddPlace(i,startat+1);
check(rd_statements(parse, startat+2, i));
if(end>=0) AddPlace(i,end,0);
parse=0;
goto again; /* always useful for error handling */
}
return end+1; /* token after terminating '}' */
}
int rd_statements(int parse, int startat, Info &jumps)
{
int end=0; /* stop compiler warning */
while(startat<tokenlist.Count())
{
check(rd_statement(parse, startat, jumps));
if(end==-1)
return startat;
startat=end;
}
return end;
}
int rd_statement(int parse, int startat, Info &jumps)
{
int end;
while(startat<tokenlist.Count())
{
#define ISIT(x) check( x (0, startat, jumps)); \
if(end!=-1) \
{ \
if(parse) \
check( x (parse, startat, jumps)); \
return end; \
}
ISIT(rd_scope);
ISIT(rd_while);
ISIT(rd_switch);
ISIT(rd_return);
ISIT(rd_if);
ISIT(rd_elseif);
ISIT(rd_else);
ISIT(rd_goto);
ISIT(rd_for);
ISIT(rd_do);
ISIT(rd_default);
ISIT(rd_case);
ISIT(rd_break);
ISIT(rd_continue);
#undef ISIT
switch(tokenlist[startat].type)
{
case FUNCTION:
case NEWLINE:
case LABEL:
case STRUCT:
case CLASS:
case UNION:
case AND:
case OR:
case IGNORE:
if(parse)
AddPlace(jumps, startat);
break;
case ENDSCOPE:
return -1;
case ENDOFSTATEMENT:
if(parse)
AddPlace(jumps, startat);
return startat+1;
default:
PARSEFAIL((char *)"Unexpected token", startat);
}
startat++;
}
return startat;
}
int rd_scope(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=BEGINSCOPE)
return -1;
if(parse)
AddPlace(jumps, startat);
check(rd_statements(0, startat+1, jumps));
if(end==-1 || end>=tokenlist.Count() || tokenlist[end].type!=ENDSCOPE)
PARSEFAIL((char *)"Scope has no closing '}'", startat);
if(parse)
{
check(rd_statements(parse, startat+1, jumps));
if(end==-1 || end>=tokenlist.Count() || tokenlist[end].type!=ENDSCOPE)
PARSEFAIL((char *)"Scope has no closing '}'", startat);
AddPlace(jumps, end);
}
return end+1;
}
/* Ensures that there was a following statement */
#define HASSTATEMENT(x) { if(end==-1 || end>=tokenlist.Count()) PARSEFAIL((char *)"'" x "' has no following ';' or '{'..'}'", startat); }
int rd_while(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=WHILE)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("while");
if(parse)
{
AddPlace(jumps, startat);
AddToPlace(jumps, startat, end);
Info i=jumps;
i.ibreak=end;
i.icontinue=startat;
check(rd_statement(parse, startat+1, i));
HASSTATEMENT("while");
NoImplicit(jumps, end-1);
AddToPlace(jumps, end-1, startat);
}
return end;
}
int rd_switch(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=SWITCH)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("switch");
if(parse)
{
Place *p=AddPlace(jumps, startat, 0);
Info i=jumps;
i.ibreak=end;
i.iswitch=startat;
check(rd_statement(parse, startat+1, i));
HASSTATEMENT("switch");
/* check for default */
int founddefault=0;
for(int a=0; a<p->flows.Count(); a++)
if(tokenlist[p->flows[a]].type==DEFAULT)
founddefault=1;
if(!founddefault)
AddToPlace(jumps, startat, end);
}
return end;
}
int rd_return(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=RETURN)
return -1;
check(rd_statement(0, startat+1, jumps));
again:
HASSTATEMENT("return"); /* must have a following ; */
if(parse)
{
AddPlace(jumps, startat, 0);
check(rd_statement(parse, startat+1, jumps));
parse=0;
AddToPlace(jumps, startat, jumps.ireturn);
goto again;
}
return end;
}
int rd_elseif(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=ELSEIF)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("elseif");
if(parse)
{
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("elseif");
AddToPlace(jumps, startat, end);
}
if(tokenlist[end].type!=ELSE)
return end;
startat=end; /* else statement */
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("else");
if(parse)
{
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("else");
NoImplicit(jumps, startat-1);
AddToPlace(jumps, startat-1, end);
}
return end;
}
int rd_else(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=ELSE)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("else");
if(parse)
{
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("else");
NoImplicit(jumps, startat-1);
AddToPlace(jumps, startat-1, end);
}
return end;
}
int rd_if(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=IF)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("if");
if(parse)
{
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("if");
AddToPlace(jumps, startat, end);
}
return end;
}
int rd_goto(int parse, int startat, Info &jumps)
{
int a, end;
if(tokenlist[startat].type!=GOTO)
return -1;
check(rd_statement(0, startat+1, jumps));
again:
HASSTATEMENT("goto");
if(parse)
{
AddPlace(jumps, startat, 0);
for(a=jumps.function->start; a<jumps.function->end; a++)
if(tokenlist[a].type==LABEL && !strcmp(tokenlist[a].string, tokenlist[startat].string))
AddToPlace(jumps, startat, a);
if(a<jumps.function->end)
PARSEFAIL((char *)"Label not found", startat);
check(rd_statement(parse, startat+1, jumps));
parse=0;
goto again;
}
return end;
}
int rd_for(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=FOR)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("for-initial");
check(rd_statement(0, end, jumps));
HASSTATEMENT("for-condition");
check(rd_statement(0, end, jumps));
HASSTATEMENT("for(;;)");
int endloop=end;
if(parse)
{
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("for-initial");
check(rd_statement(parse, end, jumps));
HASSTATEMENT("for-condition");
Info i=jumps;
i.ibreak=endloop;
i.icontinue=startat;
check(rd_statement(parse, end, i));
HASSTATEMENT("for(;;)");
AddToPlace(jumps, startat, end);
AddToPlace(jumps, end-1, startat);
NoImplicit(jumps, end-1);
}
return end;
}
int rd_do(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=DO)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("do");
/*
if(tokenlist[end].type!=WHILE)
PARSEFAIL((char *)"Expecting 'while' after 'do'", end);
*/
int whileat=end;
check(rd_statement(0, end+1, jumps));
HASSTATEMENT("'do'..'while'");
if(parse)
{
Info i=jumps;
AddPlace(jumps, startat);
i.ibreak=end;
i.icontinue=whileat;
check(rd_statement(parse, startat+1, i));
HASSTATEMENT("do");
CHECK(end==whileat);
AddPlace(jumps, whileat);
check(rd_statement(parse, end+1, jumps));
HASSTATEMENT("'do'..'while'");
AddToPlace(jumps, end-1, startat);
}
return end;
}
int rd_default(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=DEFAULT)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("default");
if(parse)
{
/*
if(jumps.iswitch==-1)
PARSEFAIL((char *)"No enclosing switch statement", startat);
*/
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("default");
AddToPlace(jumps, jumps.iswitch, startat);
}
return end;
}
int rd_case(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=CASE)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("case");
if(parse)
{
/*
if(jumps.iswitch==-1)
PARSEFAIL((char *)"No enclosing switch statement", startat);
*/
AddPlace(jumps, startat);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("case");
AddToPlace(jumps, jumps.iswitch, startat);
}
return end;
}
int rd_break(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=BREAK)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("break");
if(parse)
{
/*
if(jumps.ibreak==-1)
PARSEFAIL((char *)"No enclosing loop to break from", startat);
*/
AddPlace(jumps, startat,0);
AddToPlace(jumps, startat, jumps.ibreak);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("break");
}
return end;
}
int rd_continue(int parse, int startat, Info &jumps)
{
int end;
if(tokenlist[startat].type!=CONTINUE)
return -1;
check(rd_statement(0, startat+1, jumps));
HASSTATEMENT("continue");
if(parse)
{
/*
if(jumps.icontinue==-1)
PARSEFAIL((char *)"No enclosing loop to continue from", startat);
*/
AddPlace(jumps, startat,0);
AddToPlace(jumps, startat, jumps.icontinue);
check(rd_statement(parse, startat+1, jumps));
HASSTATEMENT("continue");
}
return end;
}
/* work out control graph for all functions in tokenlist */
void
cyclo (void)
{
Info i;
int x;
x=rd_buffer(1, 0, i);
if(x>=0 && x<tokenlist.Count())
parsefail((char *)"Unexpected token at top level", x);
if(x!=tokenlist.Count()) exit(1);
}
/* tokenise standard input */
void
tokenise (void)
{
enum TOKENS c;
int line = 1;
Token *t;
int inparens=0;
while (1)
{
c = yylex ();
if(c=='"' || c=='\'')
{
cerr << "WARNING: This source should be run through 'mcstrip' first." << endl;
}
if (c == NEWLINE)
{
line++;
continue;
}
if(c=='(' || atelparen)
{ inparens++; atelparen=0; if(inparens>1) continue; }
else if(c==')')
{
inparens-=inparens?1:0;
continue;
}
else if(c && inparens && c!=AND && c!=OR && c!=ENDOFSTATEMENT) continue; /* have to keep ';' for 'for' */
if(c>0 && c<NEWLINE)
continue;
t = new Token;
if (!t)
{
cerr << "Out of memory in " << __FILE__ << " line " << __LINE__ << "\n";
exit (1);
}
if (!c)
{
t->type = IGNORE; /* end of file buffer */
t->lineno=-1;
}
else
{
t->type = c;
t->lineno = line;
}
if (c == GOTO || c == FUNCTION || c == LABEL || c == STRUCT || c == UNION || c == CLASS)
t->string = yylval.yy_str;
if (!tokenlist.Add (t))
{
cerr << "Out of memory in " << __FILE__ << " line " << __LINE__ << "\n";
exit (1);
}
if (!c)
break;
}
}
/* print a 'pretty' token */
void pretty_token(int which, ostream &os)
{
switch(tokenlist[which].type)
{
case ENDOFSTATEMENT: os << "';'"; break;
case IF: os << "'if'"; break;
case ELSE: os << "'else'"; break;
case ELSEIF: os << "'elseif'"; break;
case FOR: os << "'for'"; break;
case WHILE: os << "'while'"; break;
case BREAK: os << "'break'"; break;
case CONTINUE: os << "'continue'"; break;
case BEGINSCOPE: os << "'{'"; break;
case ENDSCOPE: os << "'}'"; break;
case RETURN: os << "'return'"; break;
case DO: os << "'do'"; break;
case SWITCH: os << "'switch'"; break;
case CASE: os << "'case'"; break;
case AND: os << "'&&'"; break;
case OR: os << "'||'"; break;
case DEFAULT: os << "'default'"; break;
case NEWLINE:
case IGNORE: os << "INTERNAL"; break;
case GOTO: os << "'goto " << tokenlist[which].string << "'"; break;
case FUNCTION: os << "'" << tokenlist[which].string << "()'"; break;
case LABEL: os << "'" << tokenlist[which].string << ":'"; break;
case STRUCT: os << "'struct " << tokenlist[which].string << "'"; break;
case CLASS: os << "'class " << tokenlist[which].string << "'"; break;
case UNION: os << "'union " << tokenlist[which].string << "'"; break;
default: os << "character '" << (char)tokenlist[which].type << "'"; break;
}
}
/* print a token */
void
print_token (int which)
{
char temp[1024]; /* temporary space */
switch (tokenlist[which].type)
{
#define tk(x) case x: cout << #x << " "; break;
tk (ENDOFSTATEMENT);
tk (IF);
tk (ELSE);
tk (ELSEIF);
tk (FOR);
tk (WHILE);
tk (BREAK);
tk (CONTINUE);
tk (BEGINSCOPE);
tk (ENDSCOPE);
tk (RETURN);
tk (DO);
tk (SWITCH);
tk (CASE);
tk (AND);
tk (OR);
tk (DEFAULT);
tk (IGNORE);
tk (NEWLINE);
#undef tk
#define tk(x) case x:sprintf(temp,"%s %s",#x,tokenlist[which].string);cout<<temp<<" ";break;
tk (GOTO);
tk (FUNCTION);
tk (LABEL);
tk (STRUCT);
tk (CLASS);
tk (UNION);
break;
}
}
/* print results of tokenisation */
void
print_tokens (void)
{
int line = -1, curscope = 1;
for (int a = 0; a < tokenlist.Count (); a++)
{
if(tokenlist[a].type==IGNORE) continue;
if (tokenlist[a].lineno != line)
{
cout << endl;
line = tokenlist[a].lineno;
cout << setw (5) << line;
for (int b = 0; b < curscope; b++)
cout << " ";
}
print_token (a);
if (tokenlist[a].type == BEGINSCOPE)
curscope++;
if (tokenlist[a].type == ENDSCOPE)
curscope--;
}
cout << endl;
}
/* print results of cyclo */
void
print_funcs (void)
{
int a, indent;
for (a = 0; a < funclist.Count (); a++)
{
cout << "Function Name: " << funclist[a].name << endl;
indent=1;
for (int b = 0; b < funclist[a].placelist.Count (); b++)
{
cout << setw (5) << tokenlist[funclist[a].placelist[b].tokenno].lineno << setw (5) << funclist[a].placelist[b].tokenno;
for (int c = 0; c < indent; c++)
cout << " ";
cout << setw (32 - indent * 2) << setiosflags (ios::left);
if (tokenlist[funclist[a].placelist[b].tokenno].type == BEGINSCOPE)
indent++;
if (tokenlist[funclist[a].placelist[b].tokenno].type == ENDSCOPE)
indent--;
print_token (funclist[a].placelist[b].tokenno);
cout << resetiosflags (ios::left);
if (funclist[a].placelist[b].implicitnext)
cout << " next";
for (int d = 0; d < funclist[a].placelist[b].flows.Count (); d++)
cout << setw (5) << funclist[a].placelist[b].flows[d];
cout << endl;
}
}
}
int TotalComplexity=0;