1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
const string text = "Data.txt";
void readFile(string text, stack<string> &resv, stack<string> &iden, stack<int> &intg, stack<float> &real, stack<char> &spec);
void printFile(stack<string> resv, stack<string> iden, stack<int> intg, stack<float> real, stack<char> spec);
int main()
{
stack<string> RESV;
stack<string> IDEN;
stack<int> INTG;
stack<float> REAL;
stack<char> SPEC;
cout << "Opening text file to store data" << endl;
readFile(text, RESV, IDEN, INTG, REAL, SPEC);
system("PAUSE");
return 0;
}
void readFile(string text, stack<string> &resv, stack<string> &iden, stack<int> &intg, stack<float> &real, stack<char> &spec)
{
string line;
char buffer[500];
ifstream f(text);
if (f.is_open())
{
while (f >> ws, getline(f, line)) //eliminate leading white space, read line per line until hitting comma
{
for (int i = 0; i <= line.length(); i++)
{
if (isalpha(line[i])) //if letter
{
if (isalnum(line[i])) //if letter or digit
{
buffer[i] = line[i];
string test(buffer);
if (test == "int" || test == "float")
{
cout << test << "RESERVED" << endl;
resv.push(test);
}
else if (line[i + 1] == ' ' || line[i + 1] == ',' || line[i + 1] == ';' || line[i + 1] == '=') //if we look ahead and hit white space or special
{
cout << test << "IDENTIFIER" << endl;
iden.push(test);
}
}
else
{
cout << "INVALID" << endl;
}
}
if (line[i] == ',' || line[i] == '=' || line[i] == ';')
{
cout << line[i] << "SPECIAL CHARACTER" << endl;
spec.push(line[i]);
}
if (isdigit(line[i]))
{
buffer[i] = line[i];
if (line[i + 1] == ' ' || line[i + 1] == ',' || line[i + 1] == ';' || line[i + 1] == '=')
{
int buff = atoi(buffer);
cout << buff << "INTEGER" << endl;
intg.push(buff);
}
}
}
}
}
f.close();
}
|