Search code examples
cbisonyacc

Error: undeclared variable $


When I run the bisonprogram below (by bison file.y) , I get the error missing a declaration type for $2 in 'seq' :

%union {
       char cval;
}
%token <cval> AMINO 
%token STARTCODON STOPCODON

%%
series: STARTCODON seq STOPCODON {printf("%s", $2);}
seq : AMINO
         | seq AMINO
         ;
%%

I would like to know why I get this error, and how I can correctly declare the variable $2


Solution

  • You haven't told Bison what type seq is, so it doesn't know what to do with $2.

    Use the %type directive:

    %type <cval> seq
    

    Note that the type used for $2 is a single char, which is not a string as expected by the "%s" format. You need to come up with a way to create your own string from the sequence.