2011-03-30 21 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(); 
    } 

私はまだ画面を見ていますが、まだ動作していません。どんな助けでも大歓迎です。

また、後でもう少しエラーが出ますが、まだ解決していない可能性があります。ここで

はyaccのコードを見てはいけないch3hdr2.h

#define NSYMS 20 /* max number of symbols */ 

struct symtab { 
    char *name; 
    double (*funcptr)(); 
    double value; 
} symtab [NSYMS]; 

struct symtab *symlook(); 
+0

あなたは書くべきではありません/非プロトタイプ関数宣言を使用するコードを使用します。そのコードはどこから来たのですか? –

答えて

2

struct symtabの代わりにaddfunc()の入力ミスがあります。

それ以外の場合、コードはGCC(MacOS X 10.6.7では4.6.0)でコンパイルされ、軽度のウィッターが発生します。

あなたはG ++の代わりに、GCCでコンパイルする場合は、あなたが得るのと同様のエラーを取得することができます

関数スタイルは、それが「OK」であっても、C++で無効であるためです
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 

(しかし、古風な)

どのコンパイラを実際に使用していますか?どのプラットフォームですか? Iは、ヘッダのリストに#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 10.6.6でbisonとflexを使用しています – davelupt

+0

MacportにGCC 4.6.0を入手する方法はありますか? – davelupt

+1

@David:Dunno - 見ていない。私は自分の昨日の午後を作りました、そしておそらく数時間かかりました。 GMP、MPFR、MPCライブラリを構築する必要があります。彼らはすべて日常的なビルドでもあります。その部分は、おそらくライブラリが構成、構築、チェックされ、順番にインストールされていなければならないため、おそらく最も長くかかりました。いったん私がそれらを手に入れたら、メインのGCCビルドが動いて、それ自身の時間に完了しました。 –

0

のために私が持っているものです。生成されたy.tab.cファイル(またはそれを呼び出したもの)を開き、Cコードを見てください。

これは、の問題が何であるかを示す良い指標になります。は、コンパイラが処理しようとしているものです。場合によっては、#file#lineは、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

私はsymtabとしてsumtabを編集しますが、私はまだ同じ問題を抱えています。私はあなたがそれを見ることができるようにch3hdr2ファイルを含めます。 – davelupt