-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgdlParser.l
618 lines (596 loc) · 19.9 KB
/
gdlParser.l
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
/* Companion source code for "flex & bison", published by O'Reilly
* Media, ISBN 978-0-596-15597-1
* Copyright (c) 2009, Taughannock Networks. All rights reserved.
* See the README file for license conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/fb1-4.l,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/
/* recognize tokens for the calculator and print them out */
%{
#include "GDLTreeNode.h"
#include "Cfg.h"
/*定义yylval的类型*/
union {
int num;
char* str;
} yylval;
//lexer的接口
void yyerror(char *s, ...);
int curr_lineno = 0;
char* curr_filename = NULL;
//存储整个graph树的根
GDLTreeNode* gdlRoot;
GDLTreeNode *currNode = NULL;
//存储控制流图
Cfg *cfg;
//函数申明
GDLTreeNode* buildGDLTree(char* argv);
void printGDLTree(char* outFile);
Cfg* buildCFG(string inFile);
//控制流分析
%}
%%
"\"" {
int prev,next,idx = 0;
char strs[50000]; //程序最大的bug!如果字符串长度过长,会引发段错误
prev = yyinput();
next = yyinput();
// 增加行数
if(prev == '\n')
curr_lineno++;
while(next != EOF){
if(next == EOF)
break;
if(prev == '\\' && next == '\"'){
strs[idx++] = prev;
}else if(prev != '\\' && next == '\"'){
strs[idx++] = prev;
break;
}else{
strs[idx++] = prev;
}
//更新prev和next
prev = next;
next = yyinput();
}
//strs[idx++] = '\"';
strs[idx] = '\0';
//printf("strlen(strs) = %d\n",strlen(strs));
//拷贝并回传字符串值
yylval.str = strdup(strs);
return STR_CONST;
}
"//"[^\n]* {curr_lineno++; /*增加行数*/}
[ \t]+ {}
[0-9]+ { yylval.num = atoi(yytext); return INT_CONST;}
\n {curr_lineno++; /*增加行数*/}
"graph" {return GRAPH;}
"title" {return TITLE;}
"manhattan_edges" {return MANHATTAN_EDGES;}
"layoutalgorithm" {return LAYOUTALGORITHM;}
"finetuning" {return FINETUNING;}
"layout_downfactor" {return LAYOUT_DOWNFACTOR;}
"layout_upfactor" {return LAYOUT_UPFACTOR;}
"layout_nearfactor" {return LAYOUT_NEARFACTOR;}
"xlspace" {return XLSPACE;}
"yspace" {return YSPACE;}
"colorentry" {return COLORENTRY;}
"node" {return NODE;}
"label" {return LABEL;}
"edge" {return EDGE;}
"sourcename" {return SOURCENAME;}
"targetname" {return TARGETNAME;}
"color" {return COLOR;}
"vertical_order" {return VERTICAL_ORDER;}
":" {return COMMA;}
"{" {return LEFT_PARA;}
"}" {return RIGHT_PARA;}
[a-z]+ {yylval.str = strdup(yytext);return SYM_VALUE;}
%%
// Main function
main(int argc, char** argv){
//Store the two cfgs
Cfg *srcCfg, *dstCfg;
FILE* fin;
// Set file as the standard input
if(argc == 3) {
if(!(fin = fopen(argv[1], "r"))) {
printf("Error: can't open file %s\n",argv[1]);
return (1);
}else{
yyrestart(fin);
//Construct the first CFG
gdlRoot = buildGDLTree(argv[1]);
if(gdlRoot == NULL){
fprintf(stderr,"Incomplete analysis of file %s\n",argv[2]);
return 1;
}
strcpy(&argv[1][0] + strlen(argv[1]) - 4,"\0");
srcCfg = buildCFG(argv[1]);
srcCfg->label2AsmList();
fclose(fin);
// Need to clean gdlRoot
}
if(!(fin = fopen(argv[2], "r"))){
printf("Error: can't open file %s\n",argv[2]);
return (1);
}else{
yyrestart(fin);
//Construct the second CFG
gdlRoot = NULL;
gdlRoot = buildGDLTree(argv[2]);
if(gdlRoot == NULL){
fprintf(stderr,"Incomplete analysis of file %s\n",argv[2]);
return 1;
}
strcpy(&argv[2][0] + strlen(argv[2]) - 4,"\0");
dstCfg = buildCFG(argv[2]);
dstCfg->label2AsmList();
fclose(fin);
// Need to clean gdlRoot
}
}
//TEST PART
// 1. Test the parser
/*fin = fopen("test.out","a");
srcCfg->printCFG(fin);
fclose(fin);*/
// 2. Test the instruction set
/*fin = fopen("undef.txt","a");
srcCfg->undefOpcodes(fin,argv[1]);
fclose(fin);*/
// 2. Test the instruction vector
/*srcCfg->setInstructionType();
fin = fopen("undef.txt","a");
srcCfg->printInstVector(fin,argv[1]);
fclose(fin);*/
// Start the similarity calculation.
}
/* bug:: 由于对某些模式分析的不是非常到位,因此,需要不断的完善buildGDLTree方法
*/
GDLTreeNode* buildGDLTree(char* argv){
int tok;
//初始化curr_lineno和curr_filename
curr_lineno = 0;
curr_filename = argv;
GDLTreeNode* gdlRoot;
//开始遍历整个文件
while(tok = yylex()) {
/*分别对待*/
switch(tok){
case GRAPH:
gdlRoot = new GDLTreeNode(GRAPH_N);
gdlRoot->setParent(NULL);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename , curr_lineno);
return NULL;
}
tok = yylex();
if(tok != LEFT_PARA){
printf("File '%s' : Without '{' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
break;
case TITLE:
currNode = new GDLTreeNode(TITLE_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置title值
currNode->setTitle(string(yylval.str));
break;
case MANHATTAN_EDGES:
currNode = new GDLTreeNode(MANHATTAN_EDGES_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SYM_VALUE){
printf("File '%s' : Not a symbol at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置manhattan_edges值
currNode->setManhattan_edges(string(yylval.str));
break;
case LAYOUTALGORITHM:
currNode = new GDLTreeNode(LAYOUTALGORITHM_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SYM_VALUE){
printf("File '%s' : Not a symbol at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置layoutalgorithm值
currNode->setLayoutalgorithm(string(yylval.str));
break;
case FINETUNING:
currNode = new GDLTreeNode(FINETUNING_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SYM_VALUE){
printf("File '%s' : Not a symbol at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置finetuning值
currNode->setFinetuning(string(yylval.str));
break;
case LAYOUT_DOWNFACTOR:
currNode = new GDLTreeNode(LAYOUT_DOWNFACTOR_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置layout_downfactor值
currNode->setLayout_downfactor(yylval.num);
break;
case LAYOUT_UPFACTOR:
currNode = new GDLTreeNode(LAYOUT_UPFACTOR_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置layout_upfactor值
currNode->setLayout_upfactor(yylval.num);
break;
case LAYOUT_NEARFACTOR:
currNode = new GDLTreeNode(LAYOUT_NEARFACTOR_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置layout_upfactor值
currNode->setLayout_nearfactor(yylval.num);
break;
case XLSPACE:
currNode = new GDLTreeNode(XLSPACE_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置xlspace值
currNode->setXlspace(yylval.num);
break;
case YSPACE:
currNode = new GDLTreeNode(YSPACE_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置yspace值
currNode->setYspace(yylval.num);
break;
case COLORENTRY:
currNode = new GDLTreeNode(COLORENTRY_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置颜色编号
currNode->setColorentry(yylval.num);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
int r,g,b;
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
r = yylval.num;
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
g = yylval.num;
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
b = yylval.num;
//设置colorentry的(r,g,b)值
currNode->setRgb_Color(r,g,b);
break;
case NODE:
currNode = new GDLTreeNode(NODE_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != LEFT_PARA){
printf("File '%s' : Without '{' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != TITLE){
printf("File '%s' : Without title at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置title和rank
currNode->setTitle(string(yylval.str));
currNode->setRank(string(yylval.str));
tok = yylex();
if(tok != LABEL){
printf("File '%s' : Without label at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置label
currNode->setLabel(string(yylval.str));
tok = yylex();
if(tok == RIGHT_PARA){
break;
}else if(tok == VERTICAL_ORDER){
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != INT_CONST){
printf("File '%s' : Not an int at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置vertical_order
currNode->setVertical_order(yylval.num);
tok = yylex();
if(tok != RIGHT_PARA){
printf("File '%s' : Without '}' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
}else if(tok == COLOR){
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SYM_VALUE){
printf("File '%s' : Not a symbol at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置color值
currNode->setNodeColor(string(yylval.str));
}else{
printf("File '%s' : Without '}' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
break;
case EDGE:
currNode = new GDLTreeNode(EDGE_N);
currNode->setParent(gdlRoot);
gdlRoot->insertChild(currNode);
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != LEFT_PARA){
printf("File '%s' : Without '{' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SOURCENAME){
printf("File '%s' : Not SOURCENAME at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置sourcename
currNode->setSourcename(string(yylval.str));
tok = yylex();
if(tok != TARGETNAME){
printf("File '%s' : Not TARGETNAME at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置sourcename
currNode->setTargetname(string(yylval.str));
tok = yylex();
if(tok == RIGHT_PARA){
break;
}else{
//说明还有label和color属性
//tok = yylex();
if(tok != LABEL){
printf("File '%s' : Not a label at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != STR_CONST){
printf("File '%s' : Not a string at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置label值
currNode->setLabel(string(yylval.str));
tok = yylex();
if(tok != COLOR){
printf("File '%s' : Not a color at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != COMMA){
printf("File '%s' : Without ':' at line %d\n",curr_filename,curr_lineno);
return NULL;
}
tok = yylex();
if(tok != SYM_VALUE){
printf("File '%s' : Not a symbol at line %d\n",curr_filename,curr_lineno);
return NULL;
}
//设置color值
currNode->setEdgeColor(string(yylval.str));
}
break;
default:
break;
} // end of switch
} // end of while
// return the Tree Root
return gdlRoot;
}
void printGDLTree(char* outFile){
FILE *f = fopen(outFile,"w");
fprintf(f,"digraph G{\n");
for(int i = 0; i < gdlRoot->getChildrenNum(); i++){
currNode = gdlRoot->getNthChild(i);
if(currNode != NULL){
GDLTREENODETYPE nodeType = currNode->getNodeType();
if(nodeType == NODE_N){
fprintf(f,"Node%d [label = \"%s\"]\n",currNode->getRank(),currNode->getLabel().c_str());
}else if(nodeType == EDGE_N){
if(currNode->getEdgeColor() == RED){
fprintf(f,"Node%d -> Node%d [color = \"red\"]\n",currNode->getSourcename(),currNode->getTargetname());
}else if(currNode->getEdgeColor() == DARKGREEN){
fprintf(f,"Node%d -> Node%d [color = \"green\"]\n",currNode->getSourcename(),currNode->getTargetname());
}else{
fprintf(f,"Node%d -> Node%d [color = \"black\"]\n",currNode->getSourcename(),currNode->getTargetname());
}
}
}
}
//Close the file
fprintf(f,"}");
fclose(f);
}
Cfg* buildCFG(string inFile){
Cfg* cfgx = new Cfg(inFile);
for(int i = 0; i < gdlRoot->getChildrenNum(); i++){
currNode = gdlRoot->getNthChild(i);
if(currNode != NULL){
GDLTREENODETYPE nodeType = currNode->getNodeType();
if(nodeType == NODE_N){
//Construct Basic Block
cfgx->insertBB(currNode->getRank(),currNode->getLabel());
}else if(nodeType == EDGE_N){
//Insert an edge
cfgx->insertEdge(currNode->getSourcename(),currNode->getTargetname());
if(currNode->getEdgeColor() == RED){
//false part
}else if(currNode->getEdgeColor() == DARKGREEN){
//true part
}else{
//a normal one
}
}
}
}
return cfgx;
}