在使用gcc编译yacc文件时,警告格式不是字符串文字而没有格式参数

时间:2014-01-06 07:19:15

标签: gcc

这是我的yacc文件的一部分,我已经用bison编译了,然后当我想用gcc编译成可执行文件时,得到一些警告

search2:WORD    {fprintf(outFile_p,$1);}
search2:WHITE   {fprintf(outFile_p,$1);}
       |TAB     {fprintf(outFile_p,"\t");}
       |EOL     {counter=counter+1;fprintf(outFile_p,"\n");}
       |LBRAK   {fprintf(outFile_p,$1);}
       |RBRAK   {fprintf(outFile_p,$1);}
       |SIMICOL {fprintf(outFile_p,",");}
       |PERCENT {fprintf(outFile_p,"%%");}
       |PLUS    {fprintf(outFile_p,"+");}
       |ANY     {fprintf(outFile_p,$1);}
       |TCOM    {fprintf(outFile_p,"\"");}
       |MUL     {fprintf(outFile_p,$1);}
       |COM     {fprintf(outFile_p,";");}
       |LB      {fprintf(outFile_p,"[");}
       |RB      {fprintf(outFile_p,"]");}

带有“ {fprintf(outFile_p,$ 1);} 的代码”当我想使用gcc进行编译时,我收到此警告

格式不是字符串文字,也不是格式参数

如果有人可以帮我修复此警告,或者我需要发布整个代码..........

警告看起来像这样

y.y:509:5: warning: format not a string literal and no format arguments [-Wformat-security]
 search2:WORD    {fprintf(outFile_p,$1);}
     ^
y.y:510:5: warning: format not a string literal and no format arguments [-Wformat-security]
 search2:WHITE   {fprintf(outFile_p,$1);}
     ^
y.y:513:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |LBRAK   {fprintf(outFile_p,$1);}
     ^
y.y:514:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |RBRAK   {fprintf(outFile_p,$1);}
     ^
y.y:518:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |ANY     {fprintf(outFile_p,$1);}
     ^
y.y:520:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |MUL     {fprintf(outFile_p,$1);}
     ^

1 个答案:

答案 0 :(得分:1)

以下是fprintf()函数的声明。

int fprintf(FILE *stream, const char *format, ...)

您的代码缺少$1

的格式
search2:WORD    {fprintf(outFile_p,"%s",$1);}

或者您也可以使用fputs -

search2:WORD    {fputs($1,outFile_p);}