阅读背景:

野牛:'stmt'的1美元没有声明的类型

来源:互联网 
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


,); } | TOK_PRINTLN TOK_ID { printf("the value of id %d",); } | TOK_LPARA stmts TOK_RPARA { if(
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


=="{") { presentlevel=presentlevel+1; } if(=="}") { if(presentlevel>1) { presentlevel=presentlevel-1; } } }; expr: TOK_NUM { $$=atoi(
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


); } | TOK_ID { myvar *h ; h=getvar(presentlevel,
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


); $$=h->val; } | expr TOK_ADD expr {$$=
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


+;} | expr TOK_MUL expr {$$=
%{

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mycalc.h"

extern int int_num;
extern char* yytext;

%}

%token TOK_NUM TOK_ID TOK_SEMICOLON TOK_VAR TOK_EQ TOK_PRINTLN TOK_LPARA TOK_RPARA TOK_ADD TOK_MUL

%union
{
  int int_val;
  char *id_val;
}

%type <id_val> expr TOK_ID 
%type <int_val> stmt TOK_NUM

%left TOK_LPARA TOK_RPARA
%left TOK_MUL
%left TOK_ADD

%%

prog:
    stmts {  startit();  }
;


stmts:

    | stmt TOK_SEMICOLON stmts
;



stmt:
      TOK_VAR TOK_ID    {  defvar(presentlevel,yylval.id_val,0);   }
    | TOK_ID TOK_EQ expr  {  assignvar(presentlevel,$1,$3);  }
    | TOK_PRINTLN TOK_ID {  printf("the value of id %d",$2); }
    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 
                        { 
                        presentlevel=presentlevel+1;
                        }

                       if($3=="}")
                    {
                       if(presentlevel>1)
                       {
                            presentlevel=presentlevel-1;
                       }
                        }  };

expr:
      TOK_NUM  {  $$=atoi($1);  }
    | TOK_ID  { myvar *h ;
            h=getvar(presentlevel,$1);
            $$=h->val;
          }
    | expr TOK_ADD expr {$$=$1+$2;}
    | expr TOK_MUL expr {$$=$1*$2;}
;

%%

int yyerror(char *s,int x)
{

printf("Syntax Error at %d",line_num);
return 0;

}

int main()
{

startit();

presentlevel=1;

yyparse();
return 0;

}

I have declared the types for the id_val and int_val below the union as you can see. Still it is causing an error. Here are the errors that I am getting.

我已经在union下面声明了id_val和int_val的类型。它仍然导致错误。这是我得到的错误。

calc.y:46.44-45: $1 of `stmt' has no declared type
calc.y:51.43-44: $3 of `stmt' has no declared type
calc.y:65.47-48: $2 of `expr' has no declared type
calc.y:66.47-48: $2 of `expr' has no declared type
make: *** [calc] Error 1

Can somebody tell us why is it showing the error inspite of declaring the type.

有人可以告诉我们为什么它会显示错误,尽管声明了类型。

1 个解决方案

#1


1  

Well, the error messages tell you pretty much exactly what is going on. For the first one, line 46 is:

好吧,错误消息告诉你几乎到底发生了什么。对于第一个,第46行是:

    | TOK_LPARA stmts TOK_RPARA {   if($1=="{") 

this is a rule for stmt, and the error tells you that $1 (which comes from TOK_LPARA) has no type. You can see that from its delaration:

这是stmt的规则,错误告诉你$ 1(来自TOK_LPARA)没有类型。你可以从它的delaration看到:

%left TOK_LPARA TOK_RPARA

If you want to be able to access a value from TOK_LPARA here, you need to give it a type, probably <id_val>. Then you'll have the problem that == will compare pointers, not pointed at strings. The other errors all indicate similar problems.

如果您希望能够在此处访问TOK_LPARA中的值,则需要为其指定一个类型,可能是 。然后你会遇到这样的问题:==会比较指针,而不是指向字符串。其他错误都表明了类似的问题。

You also have problems with not setting $$ in stmt actions (you've declared a type for stmt) causing them to have garbage values.

您还没有在stmt操作中设置$$(您已声明stmt的类型)导致它们具有垃圾值。


*;} ; %% int yyerror(char *s,int x) { printf("Syntax Error at %d",line_num); return 0; } int main() { startit(); presentlevel=1; yyparse(); return 0; } %{ #include<stdio.h> #include<string.h> #inclu



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: