Code:
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
using namespace std;
typedef enum
/* book keeping tokens */
{ ENDFILE, ERROR,
/* reserved words */
IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE,
/* multicharacter tokens */
ID, NUM,
/* special symbols */
ASSIGN, EQ, LT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI,
/* Output */
OUTPUT
} TokenType;
/* print function */
void printToken( TokenType token, char arg[], ofstream &outfile)
{
switch ( token )
{
case IF: cout << "reserved word IF: " << arg << endl; break;
case THEN: cout << "reserved word THEN: " << arg << endl; break;
case ELSE: cout << "reserved word ELSE: " << arg << endl; break;
case END: cout << "reserved word END: " << arg << endl; break;
case REPEAT: cout << "reserved word REPEAT: " << arg << endl; break;
case UNTIL: cout << "reserved word UNTIL: " << arg << endl; break;
case READ: cout << "reserved word READ: " << arg << endl; break;
case WRITE: cout << "reserved word WRITE: " << arg << endl; break;
case ASSIGN: cout << ":=\n"; break;
case LT: cout << "<\n"; break;
case EQ: cout << "=\n"; break;
case LPAREN: cout << "(\n"; break;
case RPAREN: cout << ")\n"; break;
case SEMI: cout << ";\n"; break;
case PLUS: cout << "+\n"; break;
case MINUS: cout << "-\n"; break;
case TIMES: cout << "*\n"; break;
case OVER: cout << "/\n"; break;
case ENDFILE: cout << "EOF\n"; break;
case NUM: cout << "NUM, val= " << arg << endl; break;
case ID: cout << "ID, name= " << arg << endl; break;
case OUTPUT: cout << arg << endl; break;
case ERROR: cout << "ERROR: \n"; break;
default: cout << "Unknown token: \n" << token;
}
}
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 );
}
/* Preform scan*/
TokenType currentToken;
short tokenFound;
ch = rfile.get();
while( ! rfile.eof() )
{
tokenFound = TRUE;
/* digit check */
while ( isspace ( ch ) && ! rfile.eof () )
ch = rfile.get();
if( isdigit ( ch ) )
{
tokenIndex = 0;
while ( isdigit ( ch ) )
{
tokString [ tokenIndex ++ ] = ch;
ch = rfile . get ( ) ;
}
tokString [ tokenIndex ] = 0;
currentToken = NUM;
} //END DIGIT
else if ( isalpha ( ch ) )
{
tokenIndex = 0;
while( isalpha ( ch ) )
{
tokString[ tokenIndex ++ ] = ch;
ch = rfile.get();
}
tokString [ tokenIndex ] = 0; //add null to end of string
if(strcmp(tokString, "IF")==0 || strcmp(tokString,"if") == 0 )
{
currentToken = IF;
}
else if(strcmp(tokString, "THEN")==0 || strcmp(tokString,"then") == 0 )
{
currentToken = THEN;
}
else if(strcmp(tokString, "ELSE")==0 || strcmp(tokString,"else") == 0 )
{
currentToken = ELSE;
}
else if(strcmp(tokString, "END")==0 || strcmp(tokString,"end") == 0 )
{
currentToken = END;
}
else if(strcmp(tokString, "REPEAT")==0 || strcmp(tokString,"repeat") == 0)
{
currentToken = REPEAT;
}
else if(strcmp(tokString, "UNTIL")==0 || strcmp(tokString,"until")== 0)
{
currentToken = UNTIL;
}
else if(strcmp(tokString, "READ")==0 || strcmp(tokString,"read")== 0)
{
currentToken = READ;
}
else if(strcmp(tokString, "WRITE")==0 || strcmp(tokString,"write") == 0)
{
currentToken = WRITE; /* This is were it gets hung up at */
}
else
{
currentToken = ID;
}
}
else if( ch == ':' )
{
ch = rfile.peek();
if ( ch == '=' )
{
rfile.get();
ch = rfile.get();
currentToken = ASSIGN;
}
}
else if( ch == '{' ) //comment
{
ch = rfile.get();
tokenIndex = 0;
while(ch != '}' && ! rfile.eof () )
{
tokString[ tokenIndex ++ ] = ch;
ch = rfile.get();
}
tokString [ tokenIndex ] = 0;
ch = rfile.get(); // get }
currentToken = OUTPUT;
}
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;
default:
{
cout << "bad token: " << ch << endl;
tokenFound = FALSE;
};
}
ch = rfile.get();
}
if ( tokenFound )
printToken( currentToken, tokString, wfile);
else
tokenFound = TRUE;
}
printToken ( ENDFILE , "eof" , wfile ); //loop broke
/* end scan */
/* pause output */
cout << "\nScan is done hit a char and Hit Enter" << endl;
cin >> junk;
/* close files */
rfile.close();
wfile.close();
return 0;
}
Any ideas are welcomed,