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:
C2065: 'yyin' : undeclared identifier.
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:
%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; }
%%