Code:
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
using namespace std;
typedef enum { ENDFILE, ERROR,IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE,ID, NUM,
ASSIGN, EQ, LT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI
} TokenType;
ofstream wfile;
void printToken( TokenType token, char arg[], ostream &outFile)
{ switch (token)
{ case IF: outFile << "reserved word: " << arg << endl; break;
case THEN: outFile << "reserved word: " << arg << endl; break;
case ELSE: outFile << "reserved word: " << arg << endl; break;
case END: outFile << "reserved word: " << arg << endl; break;
case REPEAT: outFile << "reserved word: " << arg << endl; break;
case UNTIL: outFile << "reserved word: " << arg << endl; break;
case READ: outFile << "reserved word: " << arg << endl; break;
case WRITE: outFile << "reserved word: " << arg << endl; break;
case ASSIGN: outFile << ":=\n"; break;
case LT: outFile << "<\n"; break;
case EQ: outFile << "=\n"; break;
case LPAREN: outFile << "(\n"; break;
case RPAREN: outFile << ")\n"; break;
case SEMI: outFile << ";\n"; break;
case PLUS: outFile << "+\n"; break;
case MINUS: outFile << "-\n"; break;
case TIMES: outFile << "*\n"; break;
case OVER: outFile << "/\n"; break;
case ENDFILE: outFile << "EOF\n"; break;
case NUM: outFile << "NUM, val= " << arg << endl; break;
case ID: outFile << "ID, name= " << arg << endl; break;
case ERROR: outFile << "ERROR: \n"; break;
default: outFile << "Unknown token: \n",token;
arg = NULL;
}
}
int main ( void )
{
char junk;
char ch;
char read[80];
char write[80];
char tokString[256];
int lineno = 0;
int tokenIndex = 0;
cout<<"\nEnter source file name: " << endl;
cin >> read;
ifstream rfile ( read );
if (! rfile.is_open())
{
cout << "Error opening file";
exit (1);
}
cout<<"\nEnter destination file name: " << endl;
cin >> write;
ofstream wfile ( write );
if (! wfile.is_open())
{
cout << "Error opening file";
exit (1);
}
TokenType currentToken;
while( !rfile.eof() )
{
ch = rfile.get();
if(isdigit(ch))
{
rfile.unget();
while(isdigit(ch))
{
ch = rfile.get();
tokString[tokenIndex++] = ch;
}
currentToken = NUM;
} // if(isdigit(ch))
else if(isalpha(ch))
{
rfile.unget();
while(isalpha(ch))
{
ch = rfile.get();
tokString[tokenIndex++] = ch;
}
if(strcmp(tokString, "IF")==0 || strcmp(tokString,"if"))
{
currentToken = IF;
}
if(strcmp(tokString, "THEN")==0 || strcmp(tokString,"then"))
{
currentToken = THEN;
}
if(strcmp(tokString, "ELSE")==0 || strcmp(tokString,"else"))
{
currentToken = ELSE;
}
if(strcmp(tokString, "END")==0 || strcmp(tokString,"end"))
{
currentToken = END;
}
if(strcmp(tokString, "REPEAT")==0 || strcmp(tokString,"repeat"))
{
currentToken = REPEAT;
}
if(strcmp(tokString, "UNTIL")==0 || strcmp(tokString,"until"))
{
currentToken = UNTIL;
}
if(strcmp(tokString, "READ")==0 || strcmp(tokString,"read"))
{
currentToken = READ;
}
if(strcmp(tokString, "WRITE")==0 || strcmp(tokString,"write"))
{
currentToken = WRITE;
}
else
{
currentToken = ID;
}
} // if(isalpha(ch))
else if( ch == ':' )
{
ch = rfile.get();
currentToken = ASSIGN;
}
else if( ch == '{' )
{
while(isalpha(ch))
{
ch = rfile.get();
tokString[tokenIndex++] = ch;
}
}
else
{
switch (ch)
{
case '\n':
lineno++;
break;
case EOF:
currentToken = ENDFILE;
break;
case '=':
currentToken = EQ;
break;
case '<':
currentToken = LT;
break;
case '+':
currentToken = PLUS;
break;
case '-':
currentToken = MINUS;
break;
case '*':
currentToken = TIMES;
break;
case '/':
currentToken = OVER;
break;
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case ';':
currentToken = SEMI;
break;
} // switch (ch)
} // else
// cout << "token read" << endl;
printToken( currentToken, tokString, wfile);
tokenIndex = 0;
} // while( !rfile.eof() )
//cout << "\nScan is done Hit Enter" << endl;
//cin >> junk;
/* close files */
rfile.close();
wfile.close();
return 0;
}
Mod edit: