Yacc范围问题

时间:2011-03-30 01:15:32

标签: bison

所以我四处寻找答案,但我唯一可以收集的是我有一个范围问题。

错误读取ch3-05.y:54:错误:预期'=',',',';','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();

3 个答案:

答案 0 :(得分:2)

struct sumtab中有一个拼写错误struct symtab而不是addfunc()

否则,代码会在GCC(MacOS X 10.6.7上的4.6.0)下进行编译,但会有一些小的干扰。

如果使用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时,代码会编译并发出警告,但它会编译:< / p>

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 definition

GCC 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]

答案 1 :(得分:0)

不要看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代码。

答案 2 :(得分:0)

我怀疑问题可能是addfunc中的struct sumtab。否则,它可能在ch3hdr2.h头文件或其它包含的其他头文件中出错: