这是我的C扫描仪的一部分,它基于Lex n Yacc,我使用flex,bison和gcc编译器。这是我的yacc文件的一部分。所以我试图将扫描时间缩短到几秒钟,例如扫描仪将在命令行的最后一刻打印扫描时间,因为扫描仪是基于cmd的扫描仪。我已经添加了时间函数(位于此处的最后一个)文件)但作为输出我得到0.00秒。所以我不知道我在哪里犯错误。如果有人对此有任何想法。
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
FILE *outFile_p;
FILE *outFile_p1;
extern FILE *yyin;
int counter=0;
int pointer[500];
int pointer2;
char *temp[500];
char *temp2;
int i=0;
int yylex(void);
int yyparse(void);
void yyerror(const char* str)
{
fprintf(stderr,"error: %s in line: %d please check your function arguments!\n*** NOW:
if you are sure your code is correct, \n please insert// before your function call\nin orderto ignore this syntax error\n",str,counter+1);
}
int yywrap()
{
return 1;
}
%}
%token STRCPY
%token STRCAT
%token GETS
%left NUMBER
%%
commands:
{
fprintf(outFile_p,"%s","#include \"SafeLib.h\"\n");
} /*empty*/
| commands command
;
command:search1
|
search2
|
search3
|
search4
;
search3:CHAR WORD
{
fprintf(outFile_p,"%s%s","char",$2);
}
|
CHAR WORD LB WORD
{
printf("pointer_before=%d",pointer);
temp[i]=$2;
printf("\ni=%d\n",i);
if(!strcmp($4,"4"))
{
pointer[i]=1;
printf("pointer=%d",pointer[i]);
}
else
{
pointer[i]=0;printf("pointer=%d",pointer[i]);
}
i++;
fprintf(outFile_p,"%s%s%s%s","char",$2,$3,$4);
printf("\ntemp=%s\n",temp[i-1]);
}
|
CHAR WORD LB WORD SIMICOL search4
{
printf("pointer_before=%d",pointer);
temp[i]=$2;
printf("\ni=%d\n",i);
if (!strcmp($4,"4"))
{
pointer[i]=1;printf("pointer=%d",pointer[i]);
}
else
{
pointer[i]=0;printf("pointer=%d",pointer[i]);
}
i++;
fprintf(outFile_p,"%s%s%s%s","char",$2,$3,$4);
printf("\ntemp=%s\n",temp[i-1]);
}
|
NEW WORD
{
fprintf(outFile_p,"%s%s","new ",$2);
}
|
NEW CHAR
{fprintf(outFile_p,"%s%s","new ","char");}
|
NEW
{fprintf(outFile_p,"new");}
|
CHAR
{fprintf(outFile_p,"char");}
|
CHAR WORD LB RB
{fprintf(outFile_p,"%s%s%s%s","char ",$2,$3,$4);}
|
WORD LB RB
{fprintf(outFile_p,"%s%s%s",$1,$2,$3);}
|
WORD LB PLUS
{fprintf(outFile_p,"%s%s%s",$1,$2,$3);}
|
GETS LBRAK WORD RBRAK
{int flipper1=0;int fals=0;temp2=$3;printf("\ntemp2=%s\n",temp2);int j=0;int k=0;
for (j=0;j<i;j++)
{if(!strcmp(temp[k],temp2) && pointer[k]==1)
{fprintf(outFile_p,"%s%s%s%s","/*warning: This function gets() has a buffer overflow security problem*/","gets(",$3,")");
fprintf(outFile_p1,"\n Very High-Risk Warning::There is a gets() buffer overflow security problem in line %d::It is recommended to use fgets() instead....\n",counter+1);flipper1=1;fals=1;break;}
if(!strcmp(temp[k],temp2))
{fprintf(outFile_p,"%s%s%s%s","/*warning: This function gets() has a buffer overflow security problem*/","gets(",$3,")");fprintf(outFile_p1,"\n Very High-Risk Warning::There is a gets() buffer overflow security problem in line %d::It is recommended to use fgets() instead....\n",counter+1);flipper1=1;break;}k++;}if(flipper1==0)
fprintf(outFile_p,"%s%s%s","gets(",$3,")");}
|
........................................
.........................................
.........................................
|COM {fprintf(outFile_p,";");}
|LB {fprintf(outFile_p,"[");}
|RB {fprintf(outFile_p,"]");}
%%
int main(int argc,char*argv[])
{
FILE *fp;
if(argc<3)
{
printf("please specify the input and out file \n");
exit(0);
}
fp=fopen(argv[1],"r");
if(!fp)
{
printf("couldn't open file for reading \n");
exit(0);
}
outFile_p=fopen(argv[2],"w");
outFile_p1=fopen(argv[3],"w");
if(!outFile_p)
{
printf("couldn't open temp for writing outfile_p \n");
exit(0);
}
if(!outFile_p1)
{
printf("couldn't open temp for writing outfile_p1 \n");
exit(0);
}
clock_t begin, end;
double time_spent;
begin = clock();
yyin=fp;
yyparse();
fclose(fp);
fclose(outFile_p);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\nTotal Time for Scanning %f", time_spent);
}
答案 0 :(得分:0)
@zwol写道:
OP写道:这是显而易见的,我必须遗漏一些内容,但是:
clock
的第一次通话应该在致电yyparse
之前。
根据@Zack和@ user3121023的建议,我做了一些更改,yacc用于更大的输入文件扫描器显示时间,但对于较小的输入显示0.00秒,是否有任何解决方法... ..
我已经使用
gettimeofday
函数修复了问题。现在扫描仪显示时间,但我不确定准确度是多少。