Dear all , I need to pass the file name to my program to get the tokens in it, my program still in early stage , have a look and coment please
I have the 'code_file' . which should be passed
And the 'bin' file which holds the reserved words and the grammars as the following
bin.txt
Code:
[RESERVED_WORDS]
NEWJOB
CONST
VARIABLE
BEGINB
ENDB
EXIT
[GRAMMAR]
Code:
#include < iostream.h>
#include < fstream.h >
#include < string.h >
//using namespace std;
ifstream input("c:\\compiler_test\\code_file.txt"); // the code text file
ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
// array holds the reserved words
typedef char string[20] ;
string reserved[20];
// separators list
// 10 the end line , 32 the space char
// you can add new separator symbol here
char sep_list[] = { 10,32, '.', ',', ';', ',', '+', '-', '='};
const int is_Sep(char) ; // returns 1 if in the sep_list ,0 if not
// scanning functions
void get_Reserved();
void scan_bin();
//
//
//
int main()
{
char d;
if ( !bin_file.is_open() )
cout << "the file couldn't be opened !" ;
else cout << "the file is opend successfully !" ;
scan_bin(); // get all the information needed
// printing the RESERVED_WORDS list
cout << endl;
for(int i =0 ; i < 20 ; i++)
cout << reserved[i] << endl;
cout << "\n \n press any key to exit";
cin.get();
return 0; // main end
}
//----------------------------------------------------------
//__________________________________________________________
void scan_bin(){
// this function scans the bin file to get all the informations
int row=0;
int col=0;
char ch;
char temp[20];
while(!bin_file.eof())
{ bin_file.get(ch);
while ( !is_Sep(ch) & !input.eof() & ch!=-1)
{ temp[col++]=ch;
bin_file.get(ch); }
temp[col] = '\0';
if(!strcmp(temp , "[RESERVED_WORDS]"))
get_Reserved(); // calling the reserved words scanning function
}
}
void get_Reserved()
{// the tokens scanned here should be under RESERVED_WORDS tag
// if new tag bracket faced , the function will exit
int row=0;
int col=0;
char ch; // temporary for scanned symbol
char temp[20]; // temporary for scanned word
while(!bin_file.eof())
{ bin_file.get(ch);
while ( !is_Sep(ch) & !input.eof() & ch!=-1)
{ if( ch=='[' | ch==']')
return;
temp[col++]=ch;
bin_file.get(ch); }
temp[col] = '\0';
strcpy(reserved[row] , temp); // copying the word into the list
col=0;
row++;
}
}
//--------------
const int is_Sep(char c)
{ //This function checks the symbol c if in the separator
// using sizeof for flexible add in the array
for(int i=0 ; i<sizeof(sep_list) ; i++)
if(c==sep_list[i])
return 1; // in the list
return 0; // not in the list
}
thanks