I dont want to go too deep into flex and bison so I try to explain it as simply as possible.
The $$
can interpreted as the return value of a grammar rule, once the rule is finnished ( reduced ) by the parser the $$
value will be given through.
So for example, the rule :
addition : NUMBER PLUS NUMBER { }
The inside of the curly bracelets is called an action rule. The $1 $2 $3
... represents the first, second and third argument of the rule.
This means:
$1 = NUMBER
$2 = PLUS
$3 = NUMBER
If you want to add the NUMBER to the other NUMBER you have to write something like this in your action rule :
addition : NUMBER PLUS NUMBER { printf("%d",$1+$3);}
The Lexer is something like the subroutine of the parser, the parser requests the next symbol and flex scans the input for the next symbol and passes it to bison.
Something to mention: The parser has no idea what is inside the number, he just gets the symbol NUMBER. Thats why you need something like this in your flex/bison:
flex file:
[0-9]+ { yylval.val = atoi(yytext); return NUMBER; }
bison:
%union {
int val;
};
%token <val> NUMBER
Once a NUMBER symbol is scanned, flex writes its value into the val variable so bison can access it.
To understand better what happens, I recommend that you do some printf in every grammar rule. Hope that helps!