Bison-Flex extern文件*yyin不工作(C语言)

时间:2021-06-04 09:37:59

I know that in flex you just have to do yyin = fopen(filename, "r"); to read a file but if you want to do it from bison how is it possible? I'm trying to combine flex and bison for my purpose(read a file with 4 + 5 + 7; and print the outcome) but I have a hard time trying to open a file from bison. I tried to use extern FILE *yyin at the declaration room and after that yyin = fopen(filename, "r"); but I got the error:

我知道在flex中你只需要做yyin = fopen(文件名,r);读一个文件,但是如果你想从bison那里得到它怎么可能呢?我想把flex和bison结合起来(读一个4 + 5 + 7的文件;然后打印结果)但是我很难打开bison的文件。我试着在申报单上使用“外部文件”*yyin,然后使用yyin = fopen(文件名,r);但是我犯了一个错误:

C2065: 'yyin' : undeclared identifier.

C2065:“yyin”:未声明的标识符。

This is my code just in case it helps to find it.

这是我的代码,以防它帮助找到它。

bison code:

野牛代码:

%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
extern FILE *yyin;
int yyerror(char *message)
{
    return 0;
}
%}

%union {
 double val;
 char sym;
}   
%token <val> NUMBER
%token <sym> PLUS Q_MARK
%type <val> addition_list addition
%start addition_list
%%

addition_list : addition Q_MARK  {printf("apotelesma: %d\n", $1);}
| addition_list addition Q_MARK  {}
;

addition : NUMBER PLUS NUMBER {$$ = $1 + $3;}
| addition PLUS NUMBER {$$ = $1 + $3;}
;

%%
void main(int argc, char *argv[])
{
    yyin = fopen("argv[1]", "r");
    yyparse();
}

flex code:

flex代码:

%option noyywrap
%{
#include "flex2.tab.h"
%}

%%
\+ { flex2lval.sym = yytext[0];printf("%c\n", yytext[0]);  return PLUS; }
; { flex2lval.sym = yytext[0]; return Q_MARK; }
0|([-+]?(([1-9][0-9]*)|(0\.[0-9]+)|([1-9][0-9]*\.[0-9]+))) {flex2lval.val = atof(yytext); return NUMBER; }
%%

1 个解决方案

#1


2  

Did you compile the file that flex produced along with the file bison produced?

您是否编译了flex所产生的文件和bison文件生成的文件?

flex lexical_file.l (produces lex.yy.c)

flex lexical_file。l(生产lex.yy.c)

bison -d syntax_file.y (produces syntax_file.tab.c and syntax_file.tab.h)

野牛- d syntax_file。y(产生syntax_file.tab。c和syntax_file.tab.h)

gcc syntax_file.tab.c lex.yy.c -lfl -o myparser.out

Also you should remove the quotes around "argv[1]". It should be yyin = fopen(argv[1], "r"); if you want to successfully open the file that was passed as the first argument to the parser instead of trying to open a file that is actually named "argv[1]".

此外,您应该删除“argv[1]”周围的引号。应该是yyin = fopen(argv[1], r);如果您希望成功地打开作为解析器的第一个参数传递的文件,而不是尝试打开一个实际上被命名为“argv[1]”的文件。

#1


2  

Did you compile the file that flex produced along with the file bison produced?

您是否编译了flex所产生的文件和bison文件生成的文件?

flex lexical_file.l (produces lex.yy.c)

flex lexical_file。l(生产lex.yy.c)

bison -d syntax_file.y (produces syntax_file.tab.c and syntax_file.tab.h)

野牛- d syntax_file。y(产生syntax_file.tab。c和syntax_file.tab.h)

gcc syntax_file.tab.c lex.yy.c -lfl -o myparser.out

Also you should remove the quotes around "argv[1]". It should be yyin = fopen(argv[1], "r"); if you want to successfully open the file that was passed as the first argument to the parser instead of trying to open a file that is actually named "argv[1]".

此外,您应该删除“argv[1]”周围的引号。应该是yyin = fopen(argv[1], r);如果您希望成功地打开作为解析器的第一个参数传递的文件,而不是尝试打开一个实际上被命名为“argv[1]”的文件。