I know that in flex you just need to do yyin = fopen(filename, "r");to read the file, but if you want to do it from a bison, how is this possible? I am trying to combine flex and bison for my purpose (read the file with 4 + 5 + 7;and print the result), but it is difficult for me to find the file from the bison. I tried using extern FILE *yyinthe ads in the room even after that yyin = fopen(filename, "r");, but I got an error:
C2065: 'yyin': undeclared identifier.
This is my code if 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:
%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; }
%%
source
share