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
|
cout << "Please enter the file name to by read: ";
string readFile;
getline(cin, readFile);
inFile.open(readFile);
if (inFile.is_open())
{
char charBuf; // To hold each character read by inFile stream
string strBuf; // To collect past characters read
string numBuf; // To collect past numbers read
array<string,5> keyword = {"if", "then", "else", "begin", "end"};
array<char,9> special = { '(', ')','[',']','+','-','=',',',';' };
while (inFile.get(charBuf))
{
// What if inFile reads a delimiter i.e \n \t or space
// Check if inFile reads a special character
for (auto spchar = special.begin(); spchar != special.end(); spchar++)
{
if (charBuf == *spchar)
{
if (strBuf.length > 0)
{
}
}
}
if (isalpha(charBuf))
{
numBuf.clear(); // Clearing Num Buffer because there is no digit character in syntax
strBuf = charBuf;
for (auto key = keyword.begin(); key != keyword.end(); key++) //Checking collection of characters against keywords
{
if (strBuf == *key)
{
for (map<string, Token*>::iterator map = tokenMap.begin(); map != tokenMap.end(); ++map) // Checking if the keyword is already in the map
{
if (map->first == strBuf)
{
map->second->plusCount(); //increment count variable for evertime the keyword is found in file
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
Token * newToken = new Token("keyword", 1); //DONT FORGET TO DELETE!
tokenMap.insert(pair<string, Token*>(strBuf, newToken)); //Not found in the map so create a new token and store pointer in map
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
}
}
}
}
|