Lex part :
%%
[0-9]+ { yyval = atoi (yytext); return num; }
%%
Yacc part :
%token num
%%
exp:num '+' num ; {$$ = $1 + $3;}
%%
$$
, $1
and $2
stand for? $$
now?5+9
as input to this program 5
and 9
are identified by the lex program, but what about the +
? Is the symbol +
sent to lex or not?exp:num ‘+’ num ; {$$ = $1 + $3;}
those $$ , $1 , $3 are the semantic values for for the symbols and tokens used in the rule in the order that they appear. The semantic value is that one that you get in yylval when the scanner gets a new token.
$1 has the semantic value of the first num.
$3 has the semantic value of the second num
$2 is not used as it is the token '+'. The lexical analyzer does send this token to the parser. Also it has semantic value '0'.
$$ identifies the semantic value of the 'exp' (the whole grouping under that rule).
Did you try something like:
exp:num ‘+’ num ; {$$ = $1 + $3;printf("%d", $$);}
Also check: why does $1 in yacc/bison has a value of 0