所以我四处寻找答案,但我唯一能收集到的是,我有一个范围问题。
错误读取ch3-05.y:54: error:预期的‘=’、‘asm’或‘属性’之前的‘{’令牌
这是我的代码
%{
#include <stdio.h>
#include "ch3hdr2.h"
#include <string.h>
#include <math.h>
%}
%union {
double dval;
struct symtab *symp;
}
%token <symp> NAME
%token <dval> NUMBER
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS
%type <dval> expression
%%
statement_list : statement '\n'
| statement_list statement '\n'
;
statement : NAME '=' expression {$1->value = $3;}
| expression { printf("= %g \n", $1); }
expression : expression '+' expression { $$ = $1 + $3;}
| expression '-' expression { $$ = $1 - $3;}
| expression '*' expression { $$ = $1 * $3;}
| expression '/' expression {
if($3 == 0.0)
yyerror("divide by zero");
else
$$ = $1 / $3;
}
| '_' expression %prec UMINUS {$$ = -$2;}
| '(' expression ')' { $$ = $2;}
| NUMBER
| NAME {$$ = $1->value; }
| NAME '(' expression ')' {
if($1 ->funcptr)
$$ = ($1->funcptr) ($3);
else {
printf("%s not a function\n", $1->name);
$$ = 0.0;
}
}
;
%%
struct symtab *
symlook(s)
char *s;
{ // this is where the error is
char *p;
struct symtab *sp;
for(sp = symtab; sp < &symtab[NSYMS]; sp++){
if(sp -> name && !strcmp(sp->name, s))
return sp;
if(!sp->name) {
sp->name = strdup(s);
return sp;
}
}
yyerror("TOO MANY SYMBOLS");
exit(1);
}
addfunc(name, func)
char *name;
double (*func)();
{
struct sumtab *sp = symlook(name);
sp->funcptr = func;
}
main()
{
extern double sqrt(), exp(), log();
addfunc("sqrt", sqrt);
addfunc("exp", exp);
addfunc("log", log);
yyparse();
}我一直盯着屏幕看,但还没有效果。任何帮助都将不胜感激。
另外,稍后我还会收到一些错误,但我认为这可能是因为我还没有解决这个问题。
这里是我对ch3hdr2.h的看法。
#define NSYMS 20 /* max number of symbols */
struct symtab {
char *name;
double (*funcptr)();
double value;
} symtab [NSYMS];
struct symtab *symlook();发布于 2011-03-30 14:09:37
您有一个错误的struct sumtab,而不是struct symtab在in addfunc()。
否则,代码将在GCC (4.6.0在MacOS X 10.6.7上)下编译,并带有一些小妙语。
如果用G++而不是GCC编译,您可以得到一个类似于您所得到的错误:
xx.tab.c: In function ‘int yyparse()’:
xx.tab.c:1252:16: error: ‘yylex’ was not declared in this scope
xx.y:32:41: error: ‘yyerror’ was not declared in this scope
xx.y:42:91: error: too many arguments to function
xx.tab.c:1432:35: error: ‘yyerror’ was not declared in this scope
xx.tab.c:1578:35: error: ‘yyerror’ was not declared in this scope
xx.y: At global scope:
xx.y:52:9: error: ‘symtab* symlook’ redeclared as different kind of symbol
ch3hdr2.h:9:16: error: previous declaration of ‘symtab* symlook()’
xx.y:52:9: error: ‘s’ was not declared in this scope
xx.y:54:1: error: expected unqualified-id before ‘{’ token这是因为函数样式在C++中是无效的,即使它在C中是'OK‘(但过时的)。
您实际使用的是哪个编译器-在哪个平台上?
修复错误后,当我将#include <stdlib.h>添加到标头列表中并使用GCC 4.2.1 (XCode 3)或GCC 4.6.0时,代码将使用警告进行编译,但它会编译:
yacc ch3-05.y
/usr/bin/gcc -g -I/Users/jleffler/inc -std=c99 -Wall -Wextra -Wmissing-prototypes \
-Wstrict-prototypes -Wold-style-definition -c y.tab.c
In file included from ch3-05.y:3:
ch3hdr2.h:5: warning: function declaration isn’t a prototype
ch3hdr2.h:9: warning: function declaration isn’t a prototype
y.tab.c: In function ‘yyparse’:
y.tab.c:1253: warning: implicit declaration of function ‘yylex’
ch3-05.y:33: warning: implicit declaration of function ‘yyerror’
ch3-05.y: At top level:
ch3-05.y:54: warning: function declaration isn’t a prototype
ch3-05.y: In function ‘symlook’:
ch3-05.y:55: warning: old-style function definition
ch3-05.y:56: warning: unused variable ‘p’
ch3-05.y: At top level:
ch3-05.y:73: warning: return type defaults to ‘int’
ch3-05.y:73: warning: function declaration isn’t a prototype
ch3-05.y: In function ‘addfunc’:
ch3-05.y:74: warning: function declaration isn’t a prototype
ch3-05.y:75: warning: old-style function definition
ch3-05.y:78: warning: control reaches end of non-void function
ch3-05.y: At top level:
ch3-05.y:81: warning: return type defaults to ‘int’
ch3-05.y:81: warning: function declaration isn’t a prototype
ch3-05.y: In function ‘main’:
ch3-05.y:81: warning: old-style function definitionGCC 4.6.0的输出很有趣--更容易看出是什么触发了每个警告:
In file included from ch3-05.y:3:0:
ch3hdr2.h:5:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
ch3hdr2.h:9:8: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
y.tab.c: In function ‘yyparse’:
y.tab.c:1253:7: warning: implicit declaration of function ‘yylex’ [-Wimplicit-function-declaration]
ch3-05.y:33:17: warning: implicit declaration of function ‘yyerror’ [-Wimplicit-function-declaration]
ch3-05.y: At top level:
ch3-05.y:53:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
ch3-05.y: In function ‘symlook’:
ch3-05.y:53:1: warning: old-style function definition [-Wold-style-definition]
ch3-05.y:56:11: warning: unused variable ‘p’ [-Wunused-variable]
ch3-05.y: At top level:
ch3-05.y:72:1: warning: return type defaults to ‘int’ [enabled by default]
ch3-05.y:72:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
ch3-05.y: In function ‘addfunc’:
ch3-05.y:74:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
ch3-05.y:72:1: warning: old-style function definition [-Wold-style-definition]
ch3-05.y: At top level:
ch3-05.y:80:1: warning: return type defaults to ‘int’ [enabled by default]
ch3-05.y:80:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
ch3-05.y: In function ‘main’:
ch3-05.y:80:1: warning: old-style function definition [-Wold-style-definition]
ch3-05.y: In function ‘addfunc’:
ch3-05.y:78:1: warning: control reaches end of non-void function [-Wreturn-type]发布于 2011-03-30 01:31:40
不要看yacc代码。打开生成的y.tab.c文件(或您所称的任何文件),查看C代码。
这将更好地指示问题所在,因为这是编译器试图处理的。有时候,yacc将其放入C代码中的#file和#line构造只会阻碍。
我最初的想法是,它与古老的功能“原型”有关。你可能会考虑重新做这些事情,比如:
struct symtab *symlook (char *s) { // this is hopefully where the error isn't :-)
: : :如果做不到这一点,我经常会看到类似于不存在返回类型定义的错误。确保在这一点上已经完全定义了struct symtab。
然后,如果它仍然不工作,发布生成的C源代码(包括头文件)以及yacc代码。
发布于 2011-03-30 04:32:53
我怀疑问题可能是struct sumtab在addfunc中的问题。否则,它很可能在ch3hdr2.h头文件中出错,或者它包含的其他头文件.
https://stackoverflow.com/questions/5480845
复制相似问题