I am trying to write a lexical analyzer program that will read in input from a file and produce a formatted output. I need help as I am stuck. I am sure I am not passing the file stream, and the function parameters correctly. Do note some of the functions are only to help me check if I have created them correctly. I only want to make sure they can accept the parameters. I will come back later and create the guts of the program.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function that scans the file, accepts the file stream and creates the lexeme
void gToken(fstream &fin, char lexeme);
//function that writes the output, accepts the lexeme as input
char wToken(char &lexeme);
int main()
{
char filename[20];
ifstream fin(filename); //input data file
char q;
string lexeme[20];
cout << "Enter the name of the file you wish to run a lexical analysis on. "
"If you type 'q', the program will terminate. "<<endl;
cin >> filename;
if(filename == "q")
{
cout <<"You have chosen to quit the program."<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
else
{
fin.open(filename);
if(!fin.bad()) //checks to see if the file will open
{
std::cerr <<"Error. Could not open the datafile "<< filename <<endl;
exit(8);
}
}
gToken(fin,lexeme);
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
}
//reads characters from the file beginning at the first character and stops
//at spaces. Writes everything into the lexeme
void gToken(fstream &fin, char lexeme)
{
char ch;
while (fin.open)
{
while(cin.get(ch) == ' ' || == '/t')//still working on the logic of this one
{
ch >> lexeme;
cout<<"the lexeme read is " <<lexeme<<endl;
cout<<"this is part of the getToken function" <<endl;
}
}
lexeme = wToken(lexeme); //is this even correct?
}
//takes the lexeme and matches it according to rules of the language
//rules not implemented yet
char writeToken(char &lexeme)
{
cout<<"the lexeme is "<< lexeme <<;
cout<<"this is the output from writetoken"<<endl;
}