c++ - C String while using Bison/Flex -


i'm working on bison/flex project using string i'm not able use methods strlen() (same strcmp etc.)

to better explain wrote new short .l , .y files:

%{ #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 next line */         yyless(1);      /* give \n 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 @ %s in 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(); } 

and

%{ #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]=$3;             num_data_str++;         }         else printf("string: %s not saved!\n", $3); }     | block       begin_num identifier end_num       { if(num_data_int<max_var){             data_int[num_data_int]=$3;             num_data_int++; //saved         }         else printf("integer: %s not saved!\n", $3); }     ;  %%  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());  } 

as can see have

#include <string> using namespace std; 

in way have following error while compiling:

test.tab.c: in function ‘int yyparse()’: test.tab.c:1289:35: warning: deprecated conversion string constant ‘char*’ [-wwrite-strings]        yyerror (yy_("syntax error"));                                    ^ test.tab.c:1433:35: warning: deprecated conversion string constant ‘char*’ [-wwrite-strings]    yyerror (yy_("memory exhausted"));                                    ^ test.y: in function ‘void printvars()’: test.y:66:100: error: ‘strlen’ not declared in 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 can see have #include <string>

c string functions in c++ require <cstring> header, not <string>.

since flex/bison produce c code, consider turning off deprecated conversion warning. this q&a explains how.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -