-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cpp
161 lines (134 loc) · 4.75 KB
/
Lexer.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
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
#include "Lexer.h"
#include "ColonAutomaton.h"
#include "ColonDashAutomaton.h"
#include "CommaAutomaton.h"
#include "PeriodAutomaton.h"
#include "QuestionAutomaton.h"
#include "RightParenAutomaton.h"
#include "LeftParenAutomaton.h"
#include "SchemesAutomaton.h"
#include "FactsAutomaton.h"
#include "RulesAutomaton.h"
#include "QueriesAutomaton.h"
#include "IDAutomaton.h"
#include "StringAutomaton.h"
#include "CommentAutomaton.h"
#include "EOFAutomaton.h"
#include "AddAutomaton.h"
#include "MultiplyAutomaton.h"
#include <iostream>
#include <fstream>
#include <sstream>
Lexer::Lexer() {
CreateAutomata();
}
Lexer::~Lexer() {
// Deallocate tokens memeory
int tokenNum = tokens.size();
for (int i = 0; i < tokenNum; i++) {
delete tokens.at(i);
}
tokens.clear();
// Deallocate automatas memeory
int automatonNum = automata.size();
for (int i = 0; i < automatonNum; i++) {
delete automata.at(i);
}
automata.clear();
}
void Lexer::CreateAutomata() {
automata.push_back(new ColonAutomaton());
automata.push_back(new ColonDashAutomaton());
automata.push_back(new CommaAutomaton());
automata.push_back(new PeriodAutomaton());
automata.push_back(new QuestionAutomaton());
automata.push_back(new AddAutomaton());
automata.push_back(new MultiplyAutomaton());
automata.push_back(new RightParenAutomaton());
automata.push_back(new LeftParenAutomaton());
automata.push_back(new SchemesAutomaton());
automata.push_back(new FactsAutomaton());
automata.push_back(new RulesAutomaton());
automata.push_back(new QueriesAutomaton());
automata.push_back(new IDAutomaton());
automata.push_back(new StringAutomaton());
automata.push_back(new CommentAutomaton());
automata.push_back(new EOFAutomaton());
}
void Lexer::Run(std::string& input) {
int lineNumber = 1;
if (input.empty()) {
newToken = new Token(TokenType::ENDFILE, "", lineNumber);
tokens.push_back(newToken);
return;
}
// Parallel and Max Alg
while (input.size() > 0) {
int maxRead = 0;
maxAutomata = automata.at(0);
// You need to handle whitespace in between tokens
while (isspace(input.at(0))) {
// Increment line number
if (input.at(0) == '\n') {
lineNumber++;
}
// Remove Whitespace
input.erase(0,1);
// Check for an empty file
if (input.empty()) {
newToken = new Token(TokenType::ENDFILE, "", lineNumber);
tokens.push_back(newToken);
return;
}
}
// Each automaton runs with the same input
for (Automaton *automaton : automata) {
int inputRead = automaton->Start(input);
// Figure out the max automata
if (inputRead > maxRead) {
maxRead = inputRead;
maxAutomata = automaton;
}
}
// Check to see if String or Comment are never terminated
if (maxAutomata->terminateFlag == true) {
maxRead += maxAutomata->inputRead;
std::string test = input.substr(0, maxRead);
newToken = new Token(TokenType::UNDEFINED, test, lineNumber);
tokens.push_back(newToken);
if (maxAutomata->multiline) {
lineNumber += maxAutomata->multilineInc;
}
newToken = new Token(TokenType::ENDFILE, "", lineNumber);
tokens.push_back(newToken);
return;
}
// Here is the "Max" part of the algorithm
if (maxRead > 0) {
newToken = maxAutomata->CreateToken(input.substr(0, maxRead), lineNumber);
lineNumber += maxAutomata->NewLinesRead();
// No automaton accepted input
} else {
maxRead = 1;
std::string test = input.substr(0, maxRead);
newToken = new Token(TokenType::UNDEFINED, test, lineNumber);
}
if (maxAutomata->multiline) {
lineNumber += maxAutomata->multilineInc;
}
// Update input by removing characters read to create Token
tokens.push_back(newToken);
input.erase(0, maxRead);
}
}
void Lexer::printTotalTokens() {
int totalTokens = tokens.size();
while (!tokens.empty()) {
std::cout << tokens.front()->toString() << std::endl;
tokens.erase(tokens.begin());
}
std::cout << "Total Tokens = " << totalTokens << std::endl;
}
std::vector<Token*> Lexer::getTokens() {
return tokens;
}