-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
67 lines (62 loc) · 2.21 KB
/
parser.cpp
File metadata and controls
67 lines (62 loc) · 2.21 KB
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
#include "parser.hh"
#include "token.hh"
// Parser constructor that save all token recognizer in a vector.
Parser::Parser(std::string filename) {
file.open(filename);
if (!file)
throw (std::string("Bad file"));
token_tab.push_back(std::make_unique<SpaceChecker>());
token_tab.push_back(std::make_unique<IntChecker>());
token_tab.push_back(std::make_unique<CharChecker>());
token_tab.push_back(std::make_unique<SintChecker>());
token_tab.push_back(std::make_unique<IfChecker>());
token_tab.push_back(std::make_unique<ElseChecker>());
token_tab.push_back(std::make_unique<WhileChecker>());
token_tab.push_back(std::make_unique<ReturnChecker>());
token_tab.push_back(std::make_unique<IdChecker>());
token_tab.push_back(std::make_unique<LstrChecker>());
token_tab.push_back(std::make_unique<OperatorChecker>());
token_tab.push_back(std::make_unique<ComparatorChecker>());
token_tab.push_back(std::make_unique<ParenChecker>());
token_tab.push_back(std::make_unique<BracketChecker>());
token_tab.push_back(std::make_unique<EqualChecker>());
token_tab.push_back(std::make_unique<SemiChecker>());
token_tab.push_back(std::make_unique<CommaChecker>());
}
// Simple function to output all the tokens saved.
void Parser::PrintResult(void) {
for (auto &i : result) {
i.Print();
if (i.typePrint == "SEMICO")
std::cout << std::endl;
}
}
// This parse function will take one line at a time from a file.
// Then try every token recognition function on this line.
// When a token recognizer found a token, it will erase it from the line.
// It continue until no line is left.
// In a line if no token is recognized, a exception is throw.
void Parser::ParseFile(void) {
std::string line;
int countLine = 0;
bool changed = true;
std::string savedLine;
while (std::getline(file, line)) {
savedLine = line;
while (changed == true && line.length() != 0) {
changed = false;
for (auto &i: token_tab) {
Token tmp = i->CheckToken(line);
if (tmp.value != "") {
result.push_back(tmp);
changed = true;
}
if (line.length() == 0)
break;
}
}
if (line.length() != 0)
throw "Lexical error at line " + std::to_string(countLine) + "\n\t" + savedLine;
countLine++;
}
};