-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
74 lines (70 loc) · 2.53 KB
/
test.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
#include "src/LexicalAnalysis.hpp"
#include <iostream>
#include <unordered_map>
#include <iomanip>
#include <fstream>
using namespace std;
//将类型映射到string,用于输出
const unordered_map<TokenType, string> type{
//16个关键字
{TokenType::STRUCT, "STRUCT"},
{TokenType::FLOAT, "FLOAT"},
{TokenType::BOOLEAN, "BOOLEAN"},
{TokenType::SHORT, "SHORT"},
{TokenType::LONG, "LONG"},
{TokenType::DOUBLE, "DOUBLE"},
{TokenType::INT8, "INT8"},
{TokenType::INT16, "INT16"},
{TokenType::INT32, "INT32"},
{TokenType::INT64, "INT64"},
{TokenType::UINT8, "UINT8"},
{TokenType::UINT16, "UINT16"},
{TokenType::UINT32, "UINT32"},
{TokenType::UINT64, "UINT64"},
{TokenType::CHAR, "CHAR"},
{TokenType::UNSIGNED, "UNSIGNED"},
{TokenType::LBRACES, "LBRACES"},
{TokenType::RBRACES, "RBRACES"},
{TokenType::SEMICOLON, "SEMICOLON"},
{TokenType::LBRACKETS, "LBRACKETS"},
{TokenType::RBRACKETS, "RBRACKETS"},
{TokenType::TIMES, "TIMES"},
{TokenType::PLUS, "PLUS"},
{TokenType::MINUS, "MINUS"},
{TokenType::TILDE, "TILDE"},
{TokenType::SLASH, "SLASH"},
{TokenType::PERCENT, "PERCENT"},
{TokenType::GREATER, "GREATER"},
{TokenType::LESS, "LESS"},
{TokenType::AMPERSAND, "AMPERSAND"},
{TokenType::CARET, "CARET"},
{TokenType::VERTICAL, "VERTICAL"},
{TokenType::COMMA, "COMMA"},
{TokenType::ID, "ID"},
{TokenType::STRING, "STRING"},
{TokenType::INTEGER, "INTEGER"},
{TokenType::BOOL, "BOOL"},
{TokenType::ERROR, "ERROR"}
};
//输出Token序列
void print(ostream& fout, vector<Token> &vec) {
const int width = 20;
fout << left;
fout << setw(width) << "line" << setw(width) << "TokenType" << setw(width) << "TokenValue" << endl;
for (const auto &token: vec) {
if (token.type == TokenType::ERROR) cout << "\033[31m";
fout << setw(width) << token.lineNo << setw(width) << type.at(token.type) << setw(width) << token.value << endl;
fout << "\033[0m";
}
}
int main() {
ifstream fin("test/test.txt");
if (!fin.is_open()) {
cout << "failed to open file" << endl;
return 0;
}
LexicalAnalysis lexicalAnalysis(fin);
vector<Token> vec = lexicalAnalysis.scan();
print(cout, vec);
return 0;
}