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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
using namespace std;
enum statetype {newtkn, resvd, var, intr, real, del1, laststate};
enum chartype {letter, digit, period, del2, blank, pod, eoln, illegal, lastchar};
void readreserve(vector<string>&);
void writereserve(vector<string>&, ofstream&);
void readstate(vector<vector<statetype>>&);
void writestate(vector<vector<statetype>>&, ofstream&);
void readaction(vector<vector<int>>&);
void writeaction(vector<vector<int>>&, ofstream&);
void writeexp(ofstream&);
string enumtostring(statetype);
statetype strtoenum(string);
chartype charreturn(char);
void compresvd(vector<string>&, string, statetype&);
void takeaction(int, string&, char, statetype&, vector<string>&, ofstream&);
void tokenizer(vector<string>&, vector<vector<statetype>>&, vector<vector<int>>&, ofstream&);
int main()
{
ofstream _outf("output.dat");
vector<string> _resvdV;
vector<vector<int>> _actionV(laststate, vector<int>(lastchar));
vector<vector<statetype>> _FSM(laststate, vector<statetype>(lastchar));
readreserve(_resvdV);
writereserve(_resvdV, _outf);
readstate(_FSM);
writestate(_FSM, _outf);
readaction(_actionV);
writeaction(_actionV, _outf);
writeexp(_outf);
tokenizer(_resvdV, _FSM, _actionV, _outf);
_outf.close();
return 0;
}
void readreserve(vector<string>& _r)
{
ifstream _inf("reserve.dat");
string _temp;
while (!_inf.eof())
{
_inf >> _temp;
_r.push_back(_temp);
}
_inf.close();
}
void writereserve(vector<string>& _r, ofstream& _outf)
{
vector<string>::iterator iter;
_outf << "Reserved words:" <<
"\n---------------" << endl;
for (iter = _r.begin(); iter != _r.end(); ++iter)
{
_outf << *iter << " ";
}
_outf << endl << endl;
}
void readstate(vector<vector<statetype>>& _s)
{
ifstream _inf("statetable.dat");
string _temp;
for (int i = newtkn; i < laststate; i++)
for (int j = letter; j < lastchar; j++)
{
_inf >> _temp;
_s[i][j] = strtoenum(_temp);
}
_inf.close();
}
void writestate(vector<vector<statetype>>& _s, ofstream& _outf)
{
_outf << right << setw(58) << "State Table\n" <<
setw(58) <<"-----------\n";
_outf << left << setw(12) << " " << setw(12) << "Letter" << setw(12) << "Digit" <<
setw(12) << "Period" << setw(12) << "Delimiter" << setw(12) << "Blank" <<
setw(12) << "% or $" << setw(12) << "EOLN" << setw(12) << "Illegal" << endl;
_outf << left << setfill('-') << setw(108) << "-" << setfill(' ') << endl;
for (int i = newtkn; i < laststate; i++)
{
_outf << setw(10) << enumtostring((statetype)i) << "| ";
for (int j = letter; j < lastchar; j++)
_outf << setw(12) << enumtostring(_s[i][j]);
_outf << endl;
}
_outf << endl;
}
void readaction(vector<vector<int>>& _a)
{
ifstream _inf("actiontable.dat");
for (int i = newtkn; i < laststate; i++)
for (int j = letter; j < lastchar; j++)
_inf >> _a[i][j];
_inf.close();
}
void writeaction(vector<vector<int>>& _a, ofstream& _outf)
{
_outf << right << setw(59) << "Action Table\n" <<
setw(59) << "------------\n";
_outf << left << setw(12) << " " << setw(12) << "Letter" << setw(12) << "Digit" <<
setw(12) << "Period" << setw(12) << "Delimiter" << setw(12) << "Blank" <<
setw(12) << "% or $" << setw(12) << "EOLN" << setw(12) << "Illegal" << endl;
_outf << left << setfill('-') << setw(108) << "-" << setfill(' ') << endl;
for (int i = newtkn; i < laststate; i++)
{
_outf << setw(10) << enumtostring((statetype)i) << "| ";
for (int j = letter; j < lastchar; j++)
_outf << setw(12) << _a[i][j];
_outf << endl;
}
_outf << endl;
}
void writeexp(ofstream& _outf)
{
ifstream _inf("explanationofaction.dat");
string _temp;
_outf << "Explanation of Action Table entries: " <<
"\n------------------------------------" << endl;
while (!_inf.eof())
{
getline(_inf, _temp);
_outf << _temp << endl;
}
_outf << endl;
_inf.close();
}
string enumtostring(statetype _s)
{
string _temp;
if(_s == newtkn)
return "newtoken";
else if(_s == resvd)
return "reserved";
else if(_s == var)
return "variable";
else if(_s == intr)
return "integer";
else if(_s == real)
return "real";
else if(_s == del1)
return "delimeter";
else
return "laststate";
}
statetype strtoenum(string _s)
{
if (_s == "newtkn")
return newtkn;
else if (_s == "resvd")
return resvd;
else if (_s == "var")
return var;
else if (_s == "intr")
return intr;
else if (_s == "real")
return real;
else if (_s == "del1")
return del1;
else
return laststate;
}
chartype charreturn(char _ch)
{
if ((_ch >= 'A' && _ch <= 'Z') || (_ch >= 'a' && _ch <= 'z'))
return letter;
else if (_ch >= '0' && _ch <= '9')
return digit;
else if(_ch == '+'|| _ch == '-' || _ch == '/' || _ch == '*' ||
_ch == '^' || _ch == '=' || _ch == '<' || _ch == '>' ||
_ch == ',' || _ch == '\"' || _ch == '(' || _ch == ')')
return del2;
else if (_ch == ' ')
return blank;
else if (_ch == '%' || _ch == '$')
return pod;
else if (_ch == '\n')
return eoln;
else if (_ch == '.')
return period;
else
return illegal;
}
void compresvd(vector<string>& _resvdV, string _token, statetype& _st)
{
vector<string>::iterator iter;
for (iter = _resvdV.begin(); iter != _resvdV.end(); ++iter)
{
if (_token == *iter)
{
_st = resvd;
break;
}
else
_st = var;
}
}
void takeaction(int _act, string& _token, char _ch, statetype& _st, vector<string>& _resvdV, ofstream& _outf)
{
switch(_act)
{
case 1:
_token += _ch;
break;
case 2:
compresvd(_resvdV, _token, _st);
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = "";
break;
case 3:
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = "";
break;
case 4:
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl <<
setw(8) << _ch << "Improper usage " << endl;
_token = "";
break;
case 5:
_outf << setw(8) << _ch << "Improper usage " << endl;
break;
case 6:
break;
case 7:
_outf << setw(8) << _ch << "Illegal character " << endl;
break;
case 8:
compresvd(_resvdV, _token, _st);
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = _ch;
break;
case 9:
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = _ch;
break;
case 10:
_token += _ch;
_st = var;
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = "";
break;
case 11:
compresvd(_resvdV, _token, _st);
_outf << setw(8) << _token << setw(12) << enumtostring(_st) <<
setw(8) << _ch << "Illegal character " << endl;
break;
case 12:
_token += _ch;
_outf << setw(8) << _token << setw(12) << enumtostring(_st) << endl;
_token = "";
break;
case 13:
_outf << setw(8) << _token << setw(12) << enumtostring(_st) <<
setw(8) << _ch << "Illegal character " << endl;
_token = "";
break;
}
}
void tokenizer(vector<string>& _resvdV, vector<vector<statetype>>& _FSM, vector<vector<int>>& _actionV, ofstream& _outf)
{
ifstream _inf("prog1.bas");
char _ch;
int _act;
chartype _ct;
string _token = "";
statetype _st = newtkn;
_outf << left << setw(8) << "Token:" << setw(12) << "Token type:" << endl;
while (!_inf.eof())
{
_inf.get(_ch);
_ct = charreturn(_ch);
_act = _actionV[_st][_ct];
takeaction(_act, _token, _ch, _st, _resvdV, _outf);
_st = _FSM[_st][_ct];
}
_inf.close();
}
|