1

私はこのフレックスとバイソンの初心者です。小さな質問があります。lex.yy.cファイルとtab.cファイルを取得したら、lex.yy.cファイルに準拠すると、エラーが発生します。

stojk_2.l: In function ‘int yylex()’:
stojk_2.l:3: error: ‘PLUS’ was not declared in this scope
stojk_2.l:4: error: ‘MINUS’ was not declared in this scope
stojk_2.l:5: error: ‘MULT’ was not declared in this scope
stojk_2.l:6: error: ‘DIVIDE’ was not declared in this scope
stojk_2.l:8: error: ‘LPAREN’ was not declared in this scope
stojk_2.l:9: error: ‘RPAREN’ was not declared in this scope
stojk_2.l:12: error: ‘yylval’ was not declared in this scope
stojk_2.l:13: error: ‘UNSIGNEDINTEGER’ was not declared in this scope

tab.cファイルをコンパイルすると、次のエラーが発生します。

stojk_3.y: In function ‘void yyerror(char*)’:
stojk_3.y:12: error: ‘print’ was not declared in this scope
stojk_3.tab.c: At global scope:
stojk_3.tab.c:1056: error: redefinition of ‘double yylval’
stojk_3.y:8: error: ‘double yylval’ previously declared here
stojk_3.tab.c: In function ‘int yyparse()’:
stojk_3.tab.c:1253: error: ‘yylex’ was not declared in this scope
stojk_3.tab.c:1401: warning: deprecated conversion from string constant to ‘char*’
stojk_3.tab.c:1547: warning: deprecated conversion from string constant to ‘char*’

彼らはお互いを見ることができないようですが、私はそれらを同じフォルダに入れているので、私は何をすべきかわかりません.....どんな助けもいただければ幸いです......

まだそれを理解しようとしている助けをくれてGuysに感謝しますが、ここに私のコードがあります:

stojk_2.l

%%

"+"            {return PLUS;}
"-"            {return MINUS;}
"*"            {return MULT;}
"/"            {return DIVIDE;}

"("            {return LPAREN;}
")"            {return RPAREN;}

[0-9]+     {
            sscanf(yytext,  "%lf", &yylval);
        return UNSIGNEDINTEGER;
            }

[ \t]      {  }
[\n]       {return yytext[0];}

.          {return yytext[0];}

%%

int yywrap()
{
        return 0;
}

stojk_3.y

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define YYSTYPE double

YYSTYPE yylval;

void yyerror(char *s)
{
        print("yyerror: %s\n", s);
}

%}

%token PLUS
%token MINUS
%token MULT
%token DIVIDE

%token LPAREN
%token RPAREN

%token UNSIGNEDINTEGER

%left PLUS MINUS
%left MULT DIVIDE

%%

lines     :         lines expr   '\n'         {printf("%g\n", $2);}
          |         lines '\n'
          |         /*empty*/
          ;

expr      :          expr PLUS  expr           {$$  =  $1  +  $3;}  
          |          expr MINUS  expr           {$$  =  $1  -  $3;} 
          |          expr MULT  expr           {$$  =  $1  *  $3;} 
          |          expr DIVIDE  expr           {$$  =  $1  /  $3;} 
          |          LPAREN  expr  RPAREN        {$$ =  $2;}
          |          UNSIGNEDINTEGER
          ;

%%

#include  "lex.yy.c"

int yylex();
int yyparse(void);

int main()
{
return yyparse();
}
4

2 に答える 2

2

最初の一連のエラーは#include、.l ファイル内の .tab.h ファイルに失敗したように見えます。2 番目のエラー セットは、.y ファイルに何か問題があるように見えますが、.y ファイルを見ないと正確に何が問題なのかを判断するのは困難です。

于 2012-11-16T02:42:44.413 に答える
0

bison定数を定義するヘッダーを生成する必要があります。そのヘッダーを語彙アナライザーに含める必要があります。

bison -d stojk_3.y    # generates stojk_3.tab.c and stojk_3.tab.h

stojk_3.tab.hしたがって、Flex コードに含める必要があります。

関数はのprint()タイプミスである可能性がありprintf()ます。それは確かに標準機能ではありません。

の複数の定義はyylval、あなたがそれを定義したが、その必要がないためである可能性があります (Bison がそれを行います)。extern int yylex(void);コードで宣言するか、同等のものが必要になる場合があります。変換に関する苦情は、 a を取る関数にリテラル文字列を渡したということですchar *(ただし、文字列は正式には変更できないため、代わりに a を取るように関数を宣言および定義する必要がありconst char *ます)。

コードをもっと見なければ、より具体的にすることは困難です。


提供されたコードで

の問題は、予測どおりprint()、 のタイプミスです。代わりにprintf()あるべき強力な議論があります。fprintf(stderr, ...)エラーは ではなく に移動する必要がstderrありstdoutます。

定義する必要はありませんyylval。その行をコメントアウトします。

int yylex(void);文法ファイルの先頭 (%{ ... %}セクション内)で宣言しました。

yyerror()静的関数にしました。2 つの警告が残っていました。

gcc -O3 -g -std=c99 -Wall -Wextra -Wstrict-prototypes stojk_3.tab.c -o stojk_3.tab
In file included from stojk_3.y:50:0:
lex.yy.c:1112:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
lex.yy.c:1153:16: warning: ‘input’ defined but not used [-Wunused-function]

固定コード

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define YYSTYPE double

int yylex(void);

static
void yyerror(char *s)
{
        printf("yyerror: %s\n", s);
}

%}

%token PLUS
%token MINUS
%token MULT
%token DIVIDE

%token LPAREN
%token RPAREN

%token UNSIGNEDINTEGER

%left PLUS MINUS
%left MULT DIVIDE

%%

lines     :         lines expr   '\n'         {printf("%g\n", $2);}
          |         lines '\n'
          |         /*empty*/
          ;

expr      :          expr PLUS  expr           {$$  =  $1  +  $3;}  
          |          expr MINUS  expr           {$$  =  $1  -  $3;} 
          |          expr MULT  expr           {$$  =  $1  *  $3;} 
          |          expr DIVIDE  expr           {$$  =  $1  /  $3;} 
          |          LPAREN  expr  RPAREN        {$$ =  $2;}
          |          UNSIGNEDINTEGER
          ;

%%

#include  "lex.yy.c"

int yylex(void);
int yyparse(void);

int main(void)
{
    return yyparse();
}

const 以外の文字ポインターに関する警告が表示されません。何が問題なのかわかりません。

于 2012-11-16T02:42:36.210 に答える