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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include "Token.h"
using namespace std;
int main()
{
ifstream inFile;
map<string, Token*>tokenMap;
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******
if (charBuf == ' ' || charBuf == '\n' || charBuf == '\t')
{
if (strBuf.length > 0)
{
if (searchMap(strBuf, tokenMap))
{
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else if (keywordSearch(strBuf, keyword))
{
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.
}
else
{
Token * newToken = new Token("Identifier", 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.
}
}
else if (numBuf.length() > 0)
{
if (searchMap(numBuf, tokenMap&))
{
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
if (numBuf.find("."))
{
Token * newToken = new Token("Real", 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
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
Token * newToken = new Token("Integer", 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
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
}
}
break;
}
//******CASE IF NO DELIMITER IS READ, CHECK IF ITS AN ALFHA******
if (isalpha(charBuf))
{
numBuf.clear(); // Clearing Num Buffer because there is no <digit><character> in syntax
strBuf += charBuf;
if (searchMap(strBuf, tokenMap&))
{
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else if (keywordSearch(strBuf, keyword))
{
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
strBuf.clear(); // Clear strBuf to make room for next token.
}
}
else if (charBuf == '.')
{
numBuf += charBuf;
}
else if (isdigit(charBuf))
{
strBuf.clear(); // Clearing String Buffer because there is no <digit><character> in syntax
numBuf += charBuf;
if (searchMap(numBuf, tokenMap&))
{
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token
}
}
//****** The char is not alpha or digit, last case is special ******
if (specialSearch(charBuf, special))
{
if(strBuf.length > 0)
{
if (searchMap(strBuf, tokenMap&))
{
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else if (keywordSearch(strBuf, keyword))
{
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.
}
else
{
Token * newToken = new Token("Identifier", 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.
}
}
else if (numBuf.length() > 0)
{
if (searchMap(numBuf, tokenMap&))
{
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
if (numBuf.find("."))
{
if (numBuf.find(".") == numBuf[numBuf.length()])
{
numBuf = numBuf.substr(0, numBuf.length() - 1);
Token * newToken = new Token("Integer", 1); // DONT FORGET TO DELETE!
tokenMap.insert(pair<string, Token*>(numBuf, newToken)); // Not found in the map so create a new token and store pointer in map
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
Token * newToken = new Token("Real", 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
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
}
else
{
Token * newToken = new Token("Integer", 1); // DONT FORGET TO DELETE!
tokenMap.insert(pair<string, Token*>(numBuf, newToken)); // Not found in the map so create a new token and store pointer in map
numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
}
}
strBuf = charBuf; // Set strBuf to the special character to search through the map
if (searchMap(strBuf, tokenMap&))
{
strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
}
else
{
Token * newToken = new Token("Special", 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.
}
}
}
}
else
{
cout << "Something went wrong and the file was not opened.";
}
return 0;
|