This repository was archived by the owner on Dec 12, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlexer.h
More file actions
214 lines (203 loc) · 6.15 KB
/
Copy pathlexer.h
File metadata and controls
214 lines (203 loc) · 6.15 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef LEXER_H
#define LEXER_H
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include "tokens.h"
#include "error.h"
using namespace std;
class LexicalAnalyzer {
private:
string line;
int currPos;
char currChar;
int lineNumber;
vector<Token> tokens;
stack<int> keywordPositions;
void incrementChar() {
currPos += 1;
currChar = (currPos < line.size()) ? line[currPos] : '\0';
}
public:
void initialize(string currLine, int currLineNum) {
line = currLine;
currPos = -1;
currChar = '\0';
lineNumber = currLineNum;
tokens.clear();
incrementChar();
}
vector<Token> getTokens() {
return tokens;
}
void tokenize() {
while(currChar != '\0') {
//comments
if(currChar == '#') {
addEndStmntTokenIfNecessary();
while(currChar != '\0')
incrementChar();
continue;
}
//empty space
else if(isblank(currChar)) {
goToNotEmpty();
addEndStmntTokenIfNecessary();
continue;
}
//operator
else if(currChar == '+' || currChar == '-' || currChar == '*' || currChar == '/' ||
currChar == '=' || currChar == '>' || currChar == '<') {
addEndStmntTokenIfNecessary();
tokens.push_back(getOperatorToken(currChar));
incrementChar();
continue;
}
//separator
else if(currChar == '(' || currChar == ')' || currChar == '[' || currChar == ']' ||
currChar == ',' || currChar == ':') {
addEndStmntTokenIfNecessary();
tokens.push_back(getSeparatorToken(currChar));
incrementChar();
continue;
}
//integer
else if(isdigit(currChar)) {
addEndStmntTokenIfNecessary();
tokens.push_back(Token(T_INT, makeInteger(), lineNumber, currPos));
continue;
}
//string literal
else if(currChar == '\"') {
addEndStmntTokenIfNecessary();
incrementChar();
string tk = makeStringLiteral();
tokens.push_back(Token(T_String_Literal, tk, lineNumber, currPos));
incrementChar();
continue;
}
//identifiers and keywords
else if(isalpha(currChar)) {
addEndStmntTokenIfNecessary();
int firstPos = currPos;
string tk = makeLiteral();
if(isKeyword(tk)) {
tokens.push_back(Token(T_Keyword, tk, lineNumber, currPos));
if(tk == "if" || tk == "else" || tk == "def" || tk == "while") {
keywordPositions.push(firstPos);
}
} else {
tokens.push_back(Token(T_Identifier, tk, lineNumber, currPos));
}
continue;
}
//error
else {
RaiseError(InvalidCharacterError, string(1,currChar), lineNumber);
}
}
//marks the end of the line
tokens.push_back(Token(T_EndLine, "", lineNumber, -1));
}
//goes to the next non-empty char
void goToNotEmpty() {
while(currChar != '\0' && isblank(currChar)) {
incrementChar();
}
}
//returns operator token
Token getOperatorToken(char c) {
switch(c) {
case '+': return Token(T_Plus, string(1,c), lineNumber, currPos);
case '-': return Token(T_Minus, string(1,c), lineNumber, currPos);
case '*': return Token(T_Mult, string(1,c), lineNumber, currPos);
case '/': return Token(T_Div, string(1,c), lineNumber, currPos);
case '=': return Token(T_EQ, string(1,c), lineNumber, currPos);
case '>': return Token(T_Greater, string(1,c), lineNumber, currPos);
case '<': return Token(T_Less, string(1,c), lineNumber, currPos);
default: return Token(T_OPERATOR, string(1,c), lineNumber, currPos);
}
}
//returns separator token
Token getSeparatorToken(char c) {
switch(c) {
case '(': return Token(T_OpenParen, string(1,c), lineNumber, currPos);
case ')': return Token(T_CloseParen, string(1,c), lineNumber, currPos);
case '[': return Token(T_OpenBracket, string(1,c), lineNumber, currPos);
case ']': return Token(T_CloseBracket, string(1,c), lineNumber, currPos);
case ',': return Token(T_Comma, string(1,c), lineNumber, currPos);
case ':': return Token(T_Colon, string(1,c), lineNumber, currPos);
default: return Token(T_SEPARATOR, string(1,c), lineNumber, currPos);
}
}
//returns the integer as a string
string makeInteger() {
string int_str = "";
while(currChar != '\0' && isdigit(currChar)) {
int_str += currChar;
incrementChar();
}
return int_str;
}
//returns the string literal as a string
string makeStringLiteral() {
string lit = "";
while(currChar != '\0' && currChar != '\"') {
lit += currChar;
incrementChar();
}
//if no end quote found
if(currChar != '\"'){
//TODO: make error class and invoke here
// cout << "Error: Expected \" at line " << lineNumber << endl;
// exit(0);
RaiseError(DefaultError, string(1,currChar), lineNumber);
}
return lit;
}
//returns identifier or keyword as a string
string makeLiteral() {
string lit = "";
while(currChar != '\0' && isalnum(currChar)) {
lit += currChar;
incrementChar();
}
return lit;
}
//returns true if given string is a keyword
bool isKeyword(string str) {
if(str == "if" || str == "else" || str == "def" || str == "while" || str == "return" || str == "print" || str == "len") {
return true;
}
return false;
}
//adds a end statement token if the if/else/while/def block has ended
void addEndStmntTokenIfNecessary(bool eofFlag=false) {
if(eofFlag) {
while(!keywordPositions.empty()) {
tokens.push_back(Token(T_StatementEnd, "", lineNumber, currPos));
tokens.push_back(Token(T_EndLine, "", lineNumber, currPos));
keywordPositions.pop();
}
return;
}
if(keywordPositions.size() > 0 && currPos != -1) {
if(currPos == 0 || currPos == keywordPositions.top() || currPos < keywordPositions.top()) {
tokens.push_back(Token(T_StatementEnd, "", lineNumber, currPos));
tokens.push_back(Token(T_EndLine, "", lineNumber, currPos));
keywordPositions.pop();
}
}
while(!keywordPositions.empty()) {
if(currPos == 0 || currPos == keywordPositions.top() || currPos < keywordPositions.top()) {
tokens.push_back(Token(T_StatementEnd, "", lineNumber, currPos));
tokens.push_back(Token(T_EndLine, "", lineNumber, currPos));
keywordPositions.pop();
} else {
break;
}
}
}
};
#endif