Problem using yyin with yacc file
I am using Yacc and lex to parse a C type language , I have built the data structures using c++. everything works fine but i am not able to read the input file using yyin in main.cpp.
the following is the code : Please help !
#include "parse_tree.h"
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include"y.tab.h"
extern "C" FILE *yyin;
FILE *fp;
using namespace std;
int main() {
system("clear");
yyin=fopen("input_file","r+");
if(yyin==NULL)
{
cout<开发者_JAVA百科;<"\n Error ! \n";
}
do{
cout<<"am parsing !";
yyparse();
}while(!feof(yyin));
return 0;
fp=fopen("outfile","w");
yyparse();
}
int yywrap()
{
return 1;
}
Firstly I don't understand
extern "C" FILE *yyin;
when you can simply write
extern FILE *yyin;
anyway why are you opening the input file for updating (the mode parameter '+') if you are not making any changes to the file then its needless. Just "r" is sufficient. The same with the second statement
fp=fopen("outfile","w");
if needless don't open file in write mode
Also you should probably add an else statement to your code.. without it your error checking becomes useless...
yyin=fopen("input_file","r+");
if(yyin==NULL)
{
cout<<"\n Error ! \n";
}
else
{
cout<<"am parsing !";
yyparse();
}
Also you are returning from function
return 0;
Again
}while(!feof(yyin));
is useless if you have written your grammar recursively. This statement is only needed when your grammar can parse only one statement at a time.
精彩评论