· 8 years ago · Feb 07, 2018, 04:24 AM
1//Joseph William Delisle
2//MU7977
3//1/28/2018
4#include <iostream>
5#include <fstream>
6#include <string>
7#include <vector>
8#include <string>
9#include <map>
10#include <regex>
11using namespace std;
12struct symbol//symbol struct holds the type and how many times it has appeared
13{
14 int counter;
15 string symbolType;
16};
17map<string,symbol> symbolTable;//create map as our symbol table
18vector<string>::iterator iter;//iterator to search keywords later
19vector<string> keywords={"if","else","then","while","begin","end"};
20//it takes the token and makes and instantiates a symbol struct to store the number of times it has shown up and what its class is
21void AddToMap(map<string,symbol> &mapa,string inToken,string classType)
22{
23 std::map<string,symbol>::iterator it;//make map iterator to search the map
24 symbol type;//instantiate symbol
25 type.counter=1;
26 type.symbolType = (classType);
27 it = symbolTable.find(inToken); //attempt to find token
28 if (it != symbolTable.end())//if it has been found, increment the counter
29 {
30 it->second.counter++;
31 }
32 else//if not found insert into map
33 {
34 mapa.insert( pair<string,symbol>(inToken,type));
35 }
36}
37
38bool isInVector(vector<string> check, string inToken){
39 iter = find(check.begin(),check.end(),inToken);//try to find inToken in vector
40 if(iter != check.end())//if we didnt fint the token string in the vector return false
41 {
42 return true;
43 }
44 else
45 {
46 return false;
47 }
48 }
49int main()
50{
51 ifstream myfile;
52 string file;
53 while (true) //loop to make sure file exists and can be opened
54 {
55 cout << "Please Enter File Name with extension(file.txt): ";
56 cin >> file;
57 myfile.open(file);
58 if (myfile) break; //break out of while loop if file exists and has been opened
59 cout << "Invalid file. " << endl;
60 }
61
62 string s( (istreambuf_iterator<char>(myfile) ),(istreambuf_iterator<char>()) );//read whole file into a string for manipulation
63 regex specialsFinder (R"([-=,*+/;()\[\]\\])"); //find all the special character
64 regex realFinder(R"([0-9]+(\.[0-9]*)+)");//find only numbers with a decimal and atleast 1 number
65 regex characterFinder(R"([a-zA-Z_]+)");//finds groups of characters that are 1 or more
66 regex integerFinder(R"(\b\d+\b)");
67 regex_iterator<string::iterator> rend;//creating in iterator for our end
68 regex_iterator<string::iterator> sf ( s.begin(), s.end(), specialsFinder);//create iterator for special character finder
69
70 while (sf!=rend)
71 {
72 AddToMap(symbolTable,sf->str(),"special");//place each found token in map
73 ++sf;//increments the iterator
74 }
75
76 regex_iterator<string::iterator> rf ( s.begin(), s.end(), realFinder );//create iterator for reals finder
77
78 while (rf!=rend)
79 {
80 AddToMap(symbolTable,rf->str(),"real");//add real
81 ++rf;//move to next token
82 }
83 s = regex_replace(s,realFinder,"");//gets rid of doubles so regex doesn't re-find the numbers by accident
84
85 regex_iterator<string::iterator> cf ( s.begin(), s.end(), characterFinder );
86 while (cf!=rend)
87 {
88 if(isInVector(keywords, cf->str()))//determine if token is keyword or identifier
89 {
90 AddToMap(symbolTable,cf->str(),"Keyword");//add keyword
91 }
92 else
93 {
94 AddToMap(symbolTable,cf->str(),"Identifier");//add identifier
95 }
96 ++cf;//move to next token
97 }
98
99 regex_iterator<string::iterator> intf ( s.begin(), s.end(), integerFinder);//create iterator for integer finder
100 while (intf!=rend)
101 {
102 AddToMap(symbolTable,intf->str(),"Integer");//add integer
103 ++intf;//move to next token
104 }
105
106 using iterator = map< string, symbol >::iterator; //print the contents of the map
107 for ( iterator iter = symbolTable.begin(); iter != symbolTable.end(); ++iter )//iterate through whole symbol table and print out everything with its type and count
108 {
109 cout << "The symbol is: " << iter->first<< " and it shows up: " << iter->second.counter << " times and is of type: " << iter->second.symbolType << endl << endl;//print out everything
110 }
111
112 return 0;
113}