views:

79

answers:

2

Hi!

I want to make some variables I generate in b available in c:

a   : b c { ...some code...}

A simple example:

b :  X  { int result = 0; } 
  |  Y  { int result = 1; }

so I can, later on in c say:

c : D   { printf(result + 1); }
  | E   { printf(result + 2);  }

Is there any chance to do that? Any help would really be appreciated!

+1  A: 

result should be a global variable. You can do this by including

%{
    int result;
%}

at the top of your YACC file. Of course, you should also replace int result = 0 and int result = 1 with result = 0 and result = 1 respectively.

Can Berk Güder
A: 

You can do as Can suggested, however generally it is not a good idea to use globals in syntax rules. Declare a type for b and c so your rules look like this:

%union {
    int result;
};

%type <result> a b

%start a

%%

b : X {$$ = 0;} | Y {$$ = 1;} ;
c : D {$$ = 1;} | E {$$ = 2;} ;
a : b c {printf("%d", $1 + $2);};
qrdl