使用 Bison/Flex 时的 C 字符串

C String while using Bison/Flex

我正在做一个 bison/flex 项目,我在其中使用字符串,但我无法使用像 strlen() 这样的好方法(对于 strcmp 等也是如此)

为了更好地解释,我写了一个新的简短 .l 和 .y 文件:

%{
#include <string>
#include "test.tab.h"
void yyerror(char*);
extern void printVars();
int yyparse(void);
char linebuf[500];


%}

%option yylineno

blanks          [ \t\n]+
text            [^<>]+


%%

\n.*  { strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save the next line */
        yyless(1);      /* give back all but the \n to rescan */
       }

{blanks}        { /* ignore */ };

"<test>"        return(START);
"</test>"       return(STOP);
"<string>"      return(BEGIN_STRING);
"</string>"     return(END_STRING);
"<num>"         return(BEGIN_NUM);
"</num>"        return(END_NUM);

{text}          { yylval.str_val=strdup(yytext);
                  return(IDENTIFIER);
                }

.               return yytext[0];

%%


void yyerror(char *s){ 
    printf("--------- ERROR ---------\n");
    printf("%d: %s at %s in this line:\n%s\n", yylineno, s, yytext, linebuf);
    }

int yywrap (void){
    printf("--------- EOF ---------\n");
    }

int main(int num_args, char** args){
    if(num_args != 2) {printf("usage: ./parser filename\n"); exit(0);}
    FILE* file = fopen(args[1],"r");
    if(file == NULL) {printf("couldn't open %s\n",args[1]); exit(0);}
    yyin = file;
    yyparse();
    fclose(file);

    printVars();
}

%{
#include <stdio.h>
#include <string>
#define MAX_VAR     10

using namespace std;
extern int yylex();
extern void yyerror(char*);
void printVars();

string data_str[MAX_VAR];
string data_int[MAX_VAR];
int num_data_str = 0;
int num_data_int = 0;

%}



//Symbols
%union
{
    char *str_val;
};

%token START
%token STOP
%token BEGIN_NUM
%token END_NUM
%token BEGIN_STRING
%token END_STRING

%token <str_val>    IDENTIFIER

%start MyTest

%%

MyTest:
    START Block STOP
    ;

Block:
    /* empty */
    | Block 
      BEGIN_STRING IDENTIFIER END_STRING
      { if(num_data_str<MAX_VAR){
            data_str[num_data_str]=;
            num_data_str++;
        }
        else printf("string: %s not saved!\n", ); }
    | Block
      BEGIN_NUM IDENTIFIER END_NUM
      { if(num_data_int<MAX_VAR){
            data_int[num_data_int]=;
            num_data_int++; //saved
        }
        else printf("integer: %s not saved!\n", ); }
    ;

%%

void printVars(){
    printf("Printing Strings:\n");
    for(int i=0;i<num_data_str;i++) printf("-- %s of length %d\n",data_str[i].c_str(),strlen(data_str[i]));
    printf("Printing Integers:\n");
    for(int i=0;i<num_data_int;i++) printf("-- %s \n",data_int[i].c_str());

}

如你所见,我有

#include <string>
using namespace std;

这样我在编译的时候出现如下错误:

test.tab.c: In function ‘int yyparse()’:
test.tab.c:1289:35: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
       yyerror (YY_("syntax error"));
                                   ^
test.tab.c:1433:35: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
   yyerror (YY_("memory exhausted"));
                                   ^
test.y: In function ‘void printVars()’:
test.y:66:100: error: ‘strlen’ was not declared in this scope
  for(int i=0;i<num_data_str;i++) printf("-- %s of length %d\n",data_str[i].c_str(),strlen(data_str[i]));

As you can see I have #include <string>

C++ 中的 C 字符串函数需要 <cstring> header,而不是 <string>.

由于 flex/bison 生成 C 代码,请考虑关闭已弃用的转换警告。 This Q&A explains how.