-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmLexer.hpp
More file actions
187 lines (176 loc) · 4.99 KB
/
smLexer.hpp
File metadata and controls
187 lines (176 loc) · 4.99 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef SM_LEXER_HPP
#define SM_LEXER_HPP
#include <iostream>
#include <vector>
#include <fstream>
#include <chrono>
#include "smGrammarDefs.hpp"
#include "smSymbolTable.hpp"
using namespace std;
class smLexer {
private:
vector<string> source;
int pos;
char lookahead;
int lineno;
string buffer;
smSymbolTable<string, Symbol> keywords;
Symbol getToken();
char nextchar();
void error(string msg);
public:
smLexer(vector<string> src);
smLexer();
smLexer(const smLexer&);
TokenList* lex();
};
smLexer::smLexer() {
}
smLexer::smLexer(vector<string> src) {
source = src;
lineno = 0;
pos = 0;
keywords.insert("begin", beginsym);
keywords.insert("end", endsym);
keywords.insert("procedure", procsym);
keywords.insert("call", callsym);
keywords.insert("if", ifsym);
keywords.insert("do", dosym);
keywords.insert("then", thensym);
keywords.insert("while", whilesym);
keywords.insert("var", varsym);
keywords.insert("odd", oddsym);
keywords.insert("const", constsym);
cout<<"Symbol table Initialized.\n";
}
smLexer::smLexer(const smLexer& o) {
source = o.source;
pos = o.pos;
buffer = o.buffer;
lookahead = o.lookahead;
lineno = o.lineno;
keywords = o.keywords;
}
void smLexer::error(string msg) {
cout<<msg;
exit(0);
}
Symbol smLexer::getToken() {
buffer.clear();
if (lookahead == ' ') {
return whitespace;
} else if (lookahead == '~') {
return eofsym;
} else if (lookahead == '(') {
buffer = "(";
return lparen;
} else if (lookahead == ')') {
buffer = ")";
return rparen;
} else if (lookahead == '+') {
buffer = "+";
return add;
} else if (lookahead == '-') {
buffer = "-";
return sub;
} else if (lookahead == '*') {
buffer = "*";
return multiply;
} else if (lookahead == ';') {
buffer = ";";
return semicolon;
} else if (lookahead == ',') {
buffer = ",";
return comma;
} else if (lookahead == '.') {
buffer = ".";
return period;
} else if (lookahead == '=') {
lookahead = nextchar();
if (lookahead == '=') {
buffer = "==";
return equals;
} else {
string msg = "unknown symbol on line " + to_string(lineno) + " near '" + lookahead + "'";
error(msg);
}
} else if (lookahead == ':') {
lookahead = nextchar();
if (lookahead == '=') {
buffer = ":=";
return assignsym;
} else {
string msg = "unknown symbol on line " + to_string(lineno) + " near '" + lookahead + "'";
error(msg);
}
} else if (isalpha(lookahead)) {
buffer.clear();
while (1) {
if (isalnum(lookahead)) {
buffer.push_back(lookahead);
} else {
pos--;
break;
}
lookahead = nextchar();
}
Symbol ret = keywords.lookup(buffer);
if (ret == errsym) {
return idsym;
} else {
return ret;
}
} else if (isdigit(lookahead)) {
buffer.push_back(lookahead);
while (1) {
lookahead = nextchar();
if (isdigit(lookahead)) {
buffer.push_back(lookahead);
} else {
pos--;
break;
}
}
return number;
} else {
if (lookahead == '~')
return eofsym;
string msg = "Coult not determine token on line " + to_string(lineno) + " positon " + to_string(pos) + " near " + lookahead;
error(msg);
}
}
char smLexer::nextchar() {
if (pos < source[lineno].size()) {
return source[lineno][pos++];
} else if (lineno < source.size()) {
lineno += 1;
pos = 0;
cout<<"Analyzing line: "<<lineno<<"\n";
if (lineno == source.size()) {
cout<<"Lexing Complete.\n";
return '~';
}
return nextchar();
}
}
//Lexer's Gonna Lex.
TokenList* smLexer::lex() {
auto start = chrono::steady_clock::now();
cout<<"Lexer Starting...\n";
TokenList dummy("dummy", whitespace, 0);
TokenList* list = &dummy;
lookahead = 'a';
while (lookahead != '~') {
lookahead = nextchar();
Symbol curr = getToken();
if (curr != whitespace) {
list->next = new TokenList(buffer, curr, lineno);
list = list->next;
}
}
auto end = chrono::steady_clock::now();
auto timing = chrono::duration_cast<chrono::milliseconds>(end - start);
cout<<"Lexing Completed in: "<<timing.count()<<"ms.\n";
return dummy.next;
}
#endif