yrazlik
yrazlik

Reputation: 10777

Returning an attribute from flex to bison

i am trying yo write some kind of a simple compiler that detects undeclared variable, and does some extra stuff. The problem is, i cannot use "$$" in my bison file, it says "$$ of `type' has no declared type". Here are the related parts of my flex and bison files:

flx file:

int[ \t\n]+matrix {yylval.type_id.Type = 4;return tINTMATRIXTYPE; }

bison file:

%}

%union semrec
{
struct
{
  int Type;
  char *id;
 }type_id;
}

%start prog



%%
prog: stmtlst
;

stmtlst : stmt
    | stmt stmtlst
;

tmt : decl //baktım
     | asgn
     | if
;

decl : type vars '=' expr ';' 
;

type : tINTTYPE     
     | tINTVECTORTYPE    
     | tINTMATRIXTYPE   {$$.Type=$1.Type;}
     | tREALTYPE         
     | tREALVECTORTYPE    
     | tREALMATRIXTYPE    
;




%%

Writing $1.Type in bison file works, but $$.Type does not work. Can anyone help? Thanks

Upvotes: 2

Views: 299

Answers (1)

Josh
Josh

Reputation: 1774

You need to explicitly tell bison what type each token is (both terminal and non-terminal) that you plan on assigning a value to. It also looks like you don't have any of your tokens declared either.

%type <Type> type 

Will get you started. But now you'll have to ensure $$ is set for all of the other types (tINTTYPE, etc) as well.

Here's a simple example that should give you a general gist of how bison operates: http://www.gnu.org/software/bison/manual/bison.html

Upvotes: 2

Related Questions