-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.cpp
78 lines (75 loc) · 2.21 KB
/
Scanner.cpp
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
#include "uselib.h"
#include "Scanner.h"
#include <map>
#include <cctype>
using namespace std;
ostream &operator<<(ostream &os, const token_type &type)
{
os << token_type_toString(type);
return os;
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
cout << "Please enter the correct argument\n";
return 1;
}
string Path(argv[1]);
Scanner scanner = Scanner(Path);
string fileName, filePath;
bool name = true;
for (int i = Path.size() - 1; i >= 0; i--)
{
if (Path[i] == '/')
name = false;
if (name)
fileName = Path[i] + fileName;
else
filePath = Path[i] + filePath;
}
ofstream fout(filePath + "scanner_" + fileName);
if (fout.fail())
{
cout << "Create output file fail\n";
return 1;
}
cout << "Create output file in \"" << filePath + "scanner_" + fileName << "\"\n";
map<string, int> token_list[16];
int count = 0;
while (!scanner.reader.eof())
{
Token token = scanner.getToken();
if (token.getType() != token_type::NUL)
{
fout << ++count << ".\ttoken " << token.getText() << " belongs to " << token.getType() << '\n';
if (token_list[(int)token.getType()].find(token.getText()) == token_list[(int)token.getType()].end())
token_list[(int)token.getType()][token.getText()] = 1;
else
token_list[(int)token.getType()][token.getText()] += 1;
}
}
fout << "\nTotal: " << count << " tokens\n";
for (int i = 0; i < 16; i++)
{
int size = token_list[i].size();
if (size == 0)
continue;
int count = 0;
for (const auto &element : token_list[i])
count += element.second;
string type_name = token_type_toString((token_type)i);
type_name[0] = toupper(type_name[0]);
fout << '\n'
<< type_name << ": " << count << '\n';
for (const auto &element : token_list[i])
{
fout << element.first;
if (element.second > 1)
fout << " (x" << element.second << ")";
fout << '\n';
}
}
fout.close();
return 0;
}