Tech Support > Computers & Technology > Programming > Actions in Bison
Actions in Bison
Posted by Christoph Brunner on December 30th, 2005


Hi,

I have a problem regarding the semantic values in my actions.
Extract from my bison file:
single_command : v_name ASSIGNMENT
{printf("cmd:\"%s\",\"%d\"\n",$1, $2);}
expression
| LET declaration IN single_command;
v_name : IDENTIFIER { $$ = $1; };

ASSIGNMENT and IDENTIFIER are defined as tokens, the first with type
int, the second with type char* (using %union).
I defined v_name to be of the type denoting char*.

Now the problem:
The input to my parser is "LET var n : Int IN n := 3" and the action
defined above should therefore print "cmd: 'n','271'".
But this is not the case, instead both $1 and $2 have the value "n :=".
Clearly, $1 does not refer to the v_name and $2 does not refer to
ASSIGNMENT, instead, they have the same (char*) value.

Why?

I had a similiar problem with another rule. The right-hand side
VAR IDENTIFIER COLON type_denoter {printf("..." ,$2)}
always gave "n : " instead of "n" (again $2 did not refer to IDENTIFIER
alone).
I changed this rule to
VAR IDENTIFIER { printf("", $2); } COLON type_denoter
and now it works.

Can anyone explain this behaviour to me?

Thank You!


Similar Posts