Reputation: 217
Can anybody tell me what {$$ = 0}
or {$$ = 1}
or {$$ = $1 +1}
mean in Bison/Yacc rule actions? I know what $$ = $1
means in Bison.
Upvotes: 3
Views: 4267
Reputation: 6621
Bison is used for declaring rules of your grammar. Whatever is in the braces after the rule, it is the action to be taken when the rule applies to a certain group of tokens.
On the other hand, $$ stands for the semantic value of the grouping created under the respective rule.
Below I build a brief example based on your questions. The first expression states that the int_var derived from a NUM should be assigned the value that was assigned to NUM.
The second expression states that if: int_var is followed by a '=' which is followed by a 'FALSE' string, then the int_var token should be set to 0. Then, when the int_token is followed by '=' and 'TRUE' then it is set to 1.
The 4th rule states that if an int_var is followed by a '++' string, then the value assigned to the token should be the value of the int_var found plus 1.
In the same manner, this can be applied to rules for different arithmetic expressions as the addition:
int_var: NUM { $$ = $1 }
| int_var '=' 'FALSE' { $$ = 0; }
| int_var '=' 'TRUE' { $$ = 1; }
| int_var '++' { $$ = $1 + 1; }
| int_var '+' int_var { $$ = $1 + $3; }
...
;
Hope this solves your question and good luck taming Bison.
Upvotes: 7