I'm doing an entry level Lexical analyser here. My code is
bool DfaTab :: isAccepted(string s){
FILE *fp1;
int i;
fp1 = fopen("javalist.dat","r");
while(!(feof(fp1))){
fscanf(fp1,"%s%d%[^\n]",tkname.c_str(),&tkval);
if(strcmp(s.c_str(),tkname.c_str()) == 0){
setTkVal(tkval);
setTkName(tkname.c_str());
state = true;
return state;
break;
}
else{
//What should I do here to skip to next line
}
}
return state;
fclose(fp1);
}
which will be called from here :
while (!(feof(src))) {
fscanf(src,"%s[^\t^ ^\n]",sym.c_str());
if (d.isAccepted(sym)) {
fprintf(des,"<%s, %d>\n",d.getTkName().c_str(),d.getTkVal());
}
else{
cout << "Can not find symbol " << d.getTkName().c_str();
cout << "Rejected.\n";
break;
}
}
My problem is that the fscanf() function which is in the isAccepted() function does not skip to the new line and repeatedly printing the the first line that was read at the beginning of the is printed rest of the execution. What should I do now?
the file contains :
//javalist.dat
boolean 0
brake 1
case 2
catch 3
const 4
continue 5
default 6
....