Use yylval in bison to restore a string

Hi, I am embarrassed about how to get char * when I read a certain token ... I look at different sites and they provide suggestions, but not complete, I mean, for example, the yylval and yytext declaration is missing or how to convert types etc.

What is needed in a .l file? What is needed in a .y file?

What i have

in the .l file:

{WORD}      { yylval = strdup(yytext);return T_ValidWord;}

in the .y file:

%union{
  char *str;
}

%token<str> T_ValidWord

%%

element:
T_OpenTag T_ValidWord ele1 {printf("%s", $2);}
; 

Error:

xml.lex: In function ‘yylex’:
xml.lex:34: error: incompatible types when assigning to type ‘YYSTYPE’ from type ‘char *’

Another thing that confused me: In some places I see

yylval->something = yytext
yylval.something = yytext
yylval = yytext

In the bison manual I’ll tell you that yylval is a macro, I understand that the macro is replaced with text for other text, but in this situation I really do not understand it.

+3
source share
1 answer

yylval - YYSTYPE. yylval.str = strdup(yytext).

yylval - , , . yylval . yylval %union { ... } , . , , , T_NUM. , , , , yylval . yylval, , yylval.num = atoi(yytext) , num .

yytext - , . yytext, .

bison .tab.c, bison -d -t . *.tab.h. , yylval.

+9

Source: https://habr.com/ru/post/1777116/


All Articles