gpt4 book ai didi

c - 扫描时间无法正确显示

转载 作者:行者123 更新时间:2023-11-30 17:36:40 26 4
gpt4 key购买 nike

这是我的 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);
}

最佳答案

( Answered in the comments )

@zwol 写道:

This is so obvious that I must be missing something, but: The first call to clock should be before the call to yyparse.

OP 写道:

As taken advice from @Zack and @user3121023 I have made some changes, yacc for a bigger input file scanner is showing the time but for a smaller input its showing 0.00 seconds,is there any workaround for this.......

I have fixed the problem using gettimeofday function.Right now scanner is showing the time but I'm not sure of how much that is accurate.

关于c - 扫描时间无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com