2

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

4

1 回答 1

2

您需要明确告诉野牛您计划为其分配值的每个令牌是什么类型(终端和非终端)。看起来您也没有声明任何令牌。

%type <Type> type 

会让你开始。但是现在您必须确保为所有其他类型(tINTTYPE 等)设置了 $$。

这是一个简单的示例,应该可以让您大致了解野牛的运作方式:http ://www.gnu.org/software/bison/manual/bison.html

于 2013-04-10T15:57:47.797 回答