-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
185 lines (166 loc) · 4.78 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <vector>
#include <string>
#include "lexer.h"
using namespace std;
/*
Supported operations:
add: 123+4
sub: 4-2
multiply: 1*1
divide: 4/2
parentheses: (4+1)
negative operator: 3 + -2
variable: x = 4 + 2 - y
comparision: x = a < 4, y = a > b, z = b==x
conditional: if(a<b) { x=1;} else {x=2;};
loop: while(a<10) {a++;};
print: print(19+1), print(var)
*/
const unordered_map<string, Token> RESERVED_KEYWORDS = {
{"if", Token(IF, "if")},
{"else", Token(ELSE, "else")},
{"while", Token(WHILE, "while")},
{"print", Token(PRINT, "print")}
};
Token::Token() {
this->tokenType = EMPTY;
}
Token::Token(TokenType tt, string val) {
this->tokenType = tt;
this->value = val;
}
bool operator==(const Token& lhs, const Token& rhs) {
return lhs.tokenType == rhs.tokenType && lhs.value == rhs.value;
}
bool Lexer::isTerminal(char c) {
if (isspace(c))
return true;
if(c=='+' || c=='-' || c=='*' || c=='/' || c=='(' || c==')' || c==';' || c=='=' || c=='<' || c=='>' || c=='{' || c=='}')
return true;
else
return false;
}
Token Lexer::getMultiCharToken(string currTokenValue) {
int alphaCount = 0;
int digitCount = 0;
for(int x=0; x<currTokenValue.size(); x++) {
if(isalpha(currTokenValue[x]))
alphaCount++;
else if(isdigit(currTokenValue[x]))
digitCount++;
else {
cerr << "Unable to parse token " << currTokenValue << endl;
throw INVALID_TOKEN;
}
}
if(digitCount == currTokenValue.size())
return Token(NUM, currTokenValue);
else {
// treat language keywords specially
if(RESERVED_KEYWORDS.find(currTokenValue) != RESERVED_KEYWORDS.end())
return RESERVED_KEYWORDS.find(currTokenValue)->second;
else
return Token(ID, currTokenValue);
}
}
Lexer::Lexer(string program) {
nextTokenStart = 0;
this->program = program;
prevToken = Token();
}
bool Lexer::hasNextToken() {
return nextTokenStart < program.size();
}
Token Lexer::getNextToken() {
int currentIndex = nextTokenStart;
while(currentIndex < program.size()) {
char currChar = program[currentIndex];
// terminate matching process once you reach a terminal symbol
if(isTerminal(currChar)) {
// If current terminal is only one character long
if(currentIndex == nextTokenStart) {
if(isspace(currChar)) {
// skip whitespace tokens
nextTokenStart++;
currentIndex++;
continue;
}
Token currToken;
switch(currChar) {
case '+':
currToken = Token(ADD, string(1,program[currentIndex]));
break;
case '-':
currToken = Token(SUB, string(1,program[currentIndex]));
break;
case '*':
currToken = Token(MULT, string(1,program[currentIndex]));
break;
case '/':
currToken = Token(DIV, string(1,program[currentIndex]));
break;
case ';':
currToken = Token(SEMI, string(1,program[currentIndex]));
break;
case '(':
currToken = Token(OPAREN, string(1,program[currentIndex]));
break;
case ')':
currToken = Token(CPAREN, string(1,program[currentIndex]));
break;
case '=':
currToken = Token(EQUAL, string(1,program[currentIndex]));
break;
case '<':
currToken = Token(LESSTHAN, string(1,program[currentIndex]));
break;
case '>':
currToken = Token(GREATERTHAN, string(1,program[currentIndex]));
break;
case '{':
currToken = Token(OBRACE, string(1,program[currentIndex]));
break;
case '}':
currToken = Token(CBRACE, string(1,program[currentIndex]));
break;
}
nextTokenStart = currentIndex + 1;
prevToken = currToken;
return currToken;
} else {
string currTokenValue = program.substr(nextTokenStart, currentIndex-nextTokenStart);
Token extractedToken = getMultiCharToken(currTokenValue);
// Token validation
switch(extractedToken.tokenType) {
case NUM:
try {
stoi(currTokenValue);
} catch(...) {
throw INVALID_NUMBER;
}
break;
default:
break;
}
nextTokenStart = currentIndex;
prevToken = extractedToken;
return extractedToken;
}
}
// continue if you haven't reached a terminal symbol
else {
currentIndex++;
}
}
return Token(EMPTY, "");
}
Token Lexer::peek() {
// save all state, call getNextToken, and then restore state
int savedNextTokenStart = nextTokenStart;
Token savedPrevToken = prevToken;
Token peekedToken = getNextToken();
nextTokenStart = savedNextTokenStart;
prevToken = savedPrevToken;
return peekedToken;
}