2011-03-30 124 views
1

因此,我環顧四周尋找答案,但我唯一能夠收集的是我有一個範圍問題。Yacc範圍問題

錯誤讀取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(); 
+0

你不應該寫/使用使用非原型函數聲明的代碼。代碼從哪裏來的? – 2011-03-30 13:58:41

回答

2

你有一個錯字struct sumtab而不是struct symtabaddfunc()

否則,代碼將在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++,即使它是「OK」無效(但過時)in C.

你實際使用哪種編譯器 - 在哪個平臺上?


固定錯字後,當我添加#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 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] 
+0

我一直在Mac OSX上使用野牛和flex 10.6.6 – davelupt 2011-03-30 14:52:20

+0

有沒有辦法在Macports上獲得GCC 4.6.0? – davelupt 2011-03-30 15:24:22

+1

@David:不知道 - 沒有看過;我昨天下午建立了自己的,花了幾個小時。它確實需要建立GMP,MPFR和MPC庫;它們都是日常構建。那部分時間可能最長,可能只是因爲庫必須按順序進行配置,構建,檢查和安裝。一旦我掌握了這些內容,主要的GCC構建就開始運行並在自己的時間內完成。 – 2011-03-30 15:35:28

0

不要看YACC代碼。打開生成的y.tab.c文件(或任何您稱之爲)並查看C代碼。

這將是一個更好的指示器,因爲是編譯器正在嘗試處理的問題。有時候,yacc放入C代碼中的構造就是妨礙了。

我的第一個想法是,它與古老的功能「原型」有關。您可能要考慮重新做這些,如:

struct symtab *symlook (char *s) { // this is hopefully where the error isn't :-) 
: : : 

做不到這一點,我經常看到錯誤類似於其中一個返回類型的定義不存在。確保struct symtab在此時已被完全定義。

然後,如果它仍然不起作用,發佈生成的C源代碼(包括頭文件)以及yacc代碼。

0

我懷疑問題可能是addfunc中的struct sumtab。否則,它可能在ch3hdr2.h頭文件或其他頭文件中包含錯誤...

+0

我將sumtab編輯爲symtab,但我仍然遇到同樣的問題。我將包含ch3hdr2文件,以便大家可以查看它。 – davelupt 2011-03-30 11:44:23