I see a lot of examples where some pass the yytext to yylval while others do not. Here is a code for a simple adder in lex and yacc
/* add.l */
digit [0-9]
%%
{digit}+ {sscanf(yytext, "%d", &yylval);
return(INT);
}
\+ return(PLUS);
\n return(NL);
. ;
%%
int yywrap() { return 1; }
and
/* add.y */
/* L = {INT PLUS INT NL} */
%token INT PLUS NL
%%
add: INT PLUS INT NL { printf("%d\n", $1 + $3);}
%%
#include "lex.yy.c"
yyerror(char *s) { printf("%s\n", s); }
main() {
return yyparse();
}
I see no code like printf(yylval) etc. Why the code sscanf(yytext, "%d", &yylval) exist here. Is yylval used here somehow,what happens if we don't add that line? When do we need to include such a line in lex?