-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cc
268 lines (230 loc) · 6.17 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (C) Rida Bazzi, 2019
* (C) Raha Moraffah 2018
* Do not share this file with anyone
*/
/*
* Chase Brown
*
* 1212869071
*
* Project 2
*/
#include <iostream>
#include <istream>
#include <vector>
#include <string>
#include <cctype>
#include "lexer.h"
#include "inputbuf.h"
using namespace std;
string reserved[] = { "END_OF_FILE",
"LPAREN", "RPAREN", "HASH", "ID", "COMMA", "DOT", "STAR",
"OR", "UNDERSCORE", "SYMBOL", "CHAR", "INPUT_TEXT", "ERROR"};
void Token::Print()
{
cout << "{" << this->lexeme << " , "
<< reserved[(int) this->token_type] << " , "
<< this->line_no << "}\n";
}
// The constructor function will get all token in the input and stores them in an
// internal vector. This faciliates the implementation of peek()
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;
}
Token LexicalAnalyzer::ScanIdOrChar()
{
char c;
input.GetChar(c);
if (isalpha(c)) {
tmp.lexeme = "";
int no = 0;
while (!input.EndOfInput() && isalnum(c)) {
tmp.lexeme += c;
input.GetChar(c);
no++;
}
tmp.line_no = line_no;
if(no == 1)
tmp.token_type = CHAR;
else
tmp.token_type = ID;
if (!input.EndOfInput()) {
input.UngetChar(c);
}
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
Token LexicalAnalyzer::ScanInput()
{
char c;
input.GetChar(c);
string lexeme = "";
if (c == '"') {
tmp.lexeme = "";
lexeme += '"';
Token symbol = ScanSymbol();
while (symbol.token_type == SYMBOL) {
lexeme += symbol.lexeme;
symbol = ScanSymbol();
}
if (!input.EndOfInput()) {
input.GetChar(c);
if (c == '"') {
lexeme += c;
tmp.lexeme += lexeme;
tmp.token_type = INPUT_TEXT;
}
else{
tmp.lexeme = "";
tmp.token_type = ERROR;
}
}
else{
tmp.lexeme = "";
tmp.token_type = ERROR;
}
tmp.line_no = line_no;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
Token LexicalAnalyzer::ScanSymbol()
{
char c;
input.GetChar(c);
tmp.lexeme = "";
if (isspace(c) || isalnum(c)) {
//tmp.lexeme += c;
while (!input.EndOfInput() && (isspace(c) || isalnum(c))) {
line_no += (c == '\n');
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.line_no = line_no;
tmp.token_type = SYMBOL;
} 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 > (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 = LPAREN; return tmp;
case ')': tmp.token_type = RPAREN; return tmp;
case ',': tmp.token_type = COMMA; return tmp;
case '.': tmp.token_type = DOT; return tmp;
case '*': tmp.token_type = STAR; return tmp;
case '|': tmp.token_type = OR; return tmp;
case '#': tmp.token_type = HASH; return tmp;
case '_': tmp.token_type = UNDERSCORE; return tmp;
case '"':
input.UngetChar(c);
return ScanInput();
default:
if (isdigit(c)) {
//CHAR
//input.UngetChar(c);
//return ScanNumber();
tmp.token_type = CHAR;
tmp.lexeme = c;
} else if (isalpha(c)) {
input.UngetChar(c);
return ScanIdOrChar();
}
else if (input.EndOfInput())
tmp.token_type = END_OF_FILE;
else
tmp.token_type = ERROR;
return tmp;
}
}