-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cc
235 lines (212 loc) · 6.15 KB
/
lexer.cc
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (C) Rida Bazzi, 2017
*
* Do not share this file with anyone
*/
#include <iostream>
#include <istream>
#include <vector>
#include <string>
#include <cctype>
#include "lexer.h"
#include "inputbuf.h"
using namespace std;
string reserved[] = { "END_OF_FILE",
"VAR", "FOR", "IF", "WHILE", "SWITCH", "CASE", "DEFAULT", "INPUT", "OUTPUT", "ARRAY",
"PLUS", "MINUS", "DIV", "MULT",
"EQUAL", "COLON", "COMMA", "SEMICOLON",
"LBRAC", "RBRAC", "LPAREN", "RPAREN", "LBRACE", "RBRACE",
"NOTEQUAL", "GREATER", "LESS",
"NUM", "ID", "ERROR"
};
#define KEYWORDS_COUNT 9
void Token::Print()
{
cout << "{" << this->lexeme << " , "
<< reserved[(int) this->token_type] << " , "
<< this->line_no << "}\n";
}
LexicalAnalyzer::LexicalAnalyzer()
{
this->line_no = 1;
tmp.lexeme = "";
tmp.line_no = 1;
tmp.token_type = ERROR;
Token token = GetTokenMain();
index = 0;
while (token.token_type != END_OF_FILE)
{
tokenList.push_back(token); // push token into internal list
token = GetTokenMain(); // and get next token from standatd input
}
// pushes END_OF_FILE is not pushed on the token list
}
bool LexicalAnalyzer::SkipSpace()
{
char c;
bool space_encountered = false;
input.GetChar(c);
line_no += (c == '\n');
while (!input.EndOfInput() && isspace(c)) {
space_encountered = true;
input.GetChar(c);
line_no += (c == '\n');
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
return space_encountered;
}
int LexicalAnalyzer::FindKeywordIndex(string s)
{
string keyword[] = { "VAR", "FOR", "IF", "WHILE", "SWITCH", "CASE", "DEFAULT", "input", "output", "ARRAY" };
for (int i = 0; i < KEYWORDS_COUNT; i++) {
if (s == keyword[i]) {
return i + 1;
}
}
return -1;
}
Token LexicalAnalyzer::ScanNumber()
{
char c;
input.GetChar(c);
if (isdigit(c)) {
if (c == '0') {
tmp.lexeme = "0";
} else {
tmp.lexeme = "";
while (!input.EndOfInput() && isdigit(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
}
tmp.token_type = NUM;
tmp.line_no = line_no;
return tmp;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
tmp.line_no = line_no;
return tmp;
}
}
Token LexicalAnalyzer::ScanIdOrKeyword()
{
char c;
input.GetChar(c);
if (isalpha(c)) {
tmp.lexeme = "";
while (!input.EndOfInput() && isalnum(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.line_no = line_no;
int keywordIndex = FindKeywordIndex(tmp.lexeme);
if (keywordIndex != -1)
tmp.token_type = (TokenType) keywordIndex;
else
tmp.token_type = ID;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
// GetToken() accesses tokens from the tokenList that is populated when a
// lexer object is instantiated
Token LexicalAnalyzer::GetToken()
{
Token token;
if (index == tokenList.size()){ // return end of file if
token.lexeme = ""; // index is too large
token.line_no = line_no;
token.token_type = END_OF_FILE;
}
else{
token = tokenList[index];
index = index + 1;
}
return token;
}
// peek requires that the argument "howFar" be positive.
Token LexicalAnalyzer::peek(int howFar)
{
if (howFar <= 0) { // peeking backward or in place is not allowed
cout << "LexicalAnalyzer:peek:Error: non positive argument\n";
exit(-1);
}
int peekIndex = index + howFar - 1;
if (peekIndex > (int)(tokenList.size())-1) { // if peeking too far
Token token; // return END_OF_FILE
token.lexeme = "";
token.line_no = line_no;
token.token_type = END_OF_FILE;
return token;
} else
return tokenList[peekIndex];
}
Token LexicalAnalyzer::GetTokenMain()
{
char c;
SkipSpace();
tmp.lexeme = "";
tmp.line_no = line_no;
tmp.token_type = END_OF_FILE;
if (!input.EndOfInput())
input.GetChar(c);
else
return tmp;
switch (c) {
case '+': tmp.token_type = PLUS; return tmp;
case '-': tmp.token_type = MINUS; return tmp;
case '/': tmp.token_type = DIV; return tmp;
case '*': tmp.token_type = MULT; return tmp;
case '=': tmp.token_type = EQUAL; return tmp;
case ':': tmp.token_type = COLON; return tmp;
case ',': tmp.token_type = COMMA; return tmp;
case ';': tmp.token_type = SEMICOLON; return tmp;
case '[': tmp.token_type = LBRAC; return tmp;
case ']': tmp.token_type = RBRAC; return tmp;
case '(': tmp.token_type = LPAREN; return tmp;
case ')': tmp.token_type = RPAREN; return tmp;
case '{': tmp.token_type = LBRACE; return tmp;
case '}': tmp.token_type = RBRACE; return tmp;
case '>': tmp.token_type = GREATER; return tmp;
case '<':
input.GetChar(c);
if (c == '>') {
tmp.token_type = NOTEQUAL;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.token_type = LESS;
}
return tmp;
default:
if (isdigit(c)) {
input.UngetChar(c);
return ScanNumber();
} else if (isalpha(c)) {
input.UngetChar(c);
return ScanIdOrKeyword();
} else if (input.EndOfInput())
tmp.token_type = END_OF_FILE;
else
tmp.token_type = ERROR;
return tmp;
}
}