@@ -51,18 +51,17 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
51
51
buffer << " Token: " << ch << " \n " ;
52
52
return state0 (inputStream, lineCount, buffer);
53
53
} else if (isdigit (ch)) {
54
+ // state12(inputStream, lineCount, buffer);
55
+ // ************************************************** wasnt working plz help
54
56
std::string number;
55
57
number += ch;
56
- // **********************************************************************************************
57
- // This needs to be its own state. There should not be any nested if statements.
58
-
59
58
// Keep reading until a non-digit character is found.
60
59
while (inputStream.get (ch) && ch != ' ' && ch != ' ;' && ch != ' )' && ch != ' ]' ) {
61
60
number += ch;
62
61
}
63
62
64
63
inputStream.putback (ch); // Put back the last non-digit character.
65
-
64
+
66
65
// Check if the number is valid.
67
66
if (!isValidInteger (number)) {
68
67
std::cerr << " Syntax error on line " << lineCount << " : invalid integer\n " ;
@@ -71,26 +70,15 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
71
70
72
71
buffer << " \n Token type: INTEGER\n " ;
73
72
buffer << " Token: " << number << " \n " ;
74
- // ***************************************** *****************************************************
73
+ // *****************************************************
75
74
return state0 (inputStream, lineCount, buffer);
76
75
} else if (ch == ' ,' ) {
77
76
buffer << " \n Token type: COMMA\n " ;
78
77
buffer << " Token: " << ch << " \n " ;
79
78
return state0 (inputStream, lineCount, buffer);
80
79
} else if (ch == ' =' ) {
81
- // **********************************************************************************************
82
- // This needs to be its own state. There should not be any nested if statements.
83
- inputStream.get (ch);
84
- if (ch == ' =' ) {
85
- buffer << " \n Token type: BOOLEAN_EQUAL\n " ;
86
- buffer << " Token: ==\n " ;
87
- } else {
88
- inputStream.putback (ch);
89
- buffer << " \n Token type: ASSIGNMENT_OPERATOR\n " ;
90
- buffer << " Token: =\n " ;
91
- }
80
+ state13 (inputStream, lineCount, buffer);
92
81
return state0 (inputStream, lineCount, buffer);
93
- // **********************************************************************************************
94
82
} else if (ch == ' +' ) {
95
83
buffer << " \n Token type: PLUS\n " ;
96
84
buffer << " Token: " << ch << " \n " ;
@@ -117,19 +105,7 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
117
105
buffer << " Token: " << ch << " \n " ;
118
106
return state0 (inputStream, lineCount, buffer);
119
107
} else if (ch == ' &' ) {
120
- // **********************************************************************************************
121
- // This needs to be its own state. There should not be any nested if statements.
122
-
123
- inputStream.get (ch);
124
- if (ch == ' &' ) {
125
- buffer << " \n Token type: BOOLEAN_AND\n " ;
126
- buffer << " Token: &&\n " ;
127
- } else {
128
- inputStream.putback (ch);
129
- buffer << " \n Token type: AMPERSAND\n " ;
130
- buffer << " Token: &\n " ;
131
- }
132
- // **********************************************************************************************
108
+ state10 (inputStream, lineCount, buffer);
133
109
return state0 (inputStream, lineCount, buffer);
134
110
} else if (ch == ' "' ) {
135
111
buffer << " \n Token type: DOUBLE_QUOTE\n " ;
@@ -177,17 +153,8 @@ void Tokenizer::state1(std::istringstream &inputStream, int &lineCount, std::ost
177
153
buffer << " Token: " << ch << " \n " ;
178
154
return ;
179
155
} else if (ch == ' \\ ' ) { // Handle escape characters in strings
180
- // **********************************************************************************************
181
- // This needs to be its own state. There should not be any nested if statements.
182
-
183
156
buffer << ch; // Add the backslash
184
- inputStream.get (ch);
185
- if (ch == ' n' ) { // Case to add specifically 'n' to buffer
186
- buffer << " n" ;
187
- } else {
188
- buffer << ch; // Add other escape character other than /n
189
- }
190
- // **********************************************************************************************
157
+ state9 (inputStream, lineCount, buffer);
191
158
} else if (ch == ' \n ' ) { // Real newline in input
192
159
lineCount++;
193
160
std::cerr << " Error: Unterminated string on line " << lineCount << " \n " ;
@@ -274,18 +241,7 @@ void Tokenizer::state5(std::istringstream &inputStream, int &lineCount, std::ost
274
241
buffer << " \n Token type: CHAR_LITERAL\n " ;
275
242
buffer << " Token: " << ch << " \n " ; // Handle the literal inside the single quotes
276
243
}
277
-
278
- // Check for the closing single quote (to the non empty single quotes)
279
- inputStream.get (ch); // Why are we getting another character on the same iteration and checking for '\' again? Shouldn't we be calling state5() again instead? Repeated code?
280
- if (ch == ' \' ' ) {
281
- buffer << " \n Token type: SINGLE_QUOTE\n " ;
282
- buffer << " Token: " << ch << " \n " ; // Handle the final character
283
- return state0 (inputStream, lineCount, buffer);
284
- } else {
285
- std::cerr << " Error: Invalid character literal\n " ;
286
- exit (1 );
287
- }
288
-
244
+ state11 (inputStream, lineCount, buffer); // Found the char or escape character, now check for the closing quote
289
245
}
290
246
291
247
void Tokenizer::state6 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
@@ -326,12 +282,85 @@ void Tokenizer::state8(std::istringstream &inputStream, int &lineCount, std::ost
326
282
if (nextCh == ' n' || nextCh == ' t' || nextCh == ' \\ ' || nextCh == ' 0' ) { // Can add more if needed...
327
283
buffer << " \n Token type: STRING\n " ;
328
284
buffer << " Token: \\ " << nextCh << " \n " ; // Add the escaped character to the buffer
329
- } else { // Else, not a valid esc char
285
+ } else { // Else, was not a valid esc character
330
286
std::cerr << " Error: Invalid escape sequence '\\ " << nextCh << " \n " ;
331
287
exit (1 );
332
288
}
333
289
}
334
290
291
+ void Tokenizer::state9 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
292
+ char ch;
293
+ inputStream.get (ch);
294
+ if (ch == ' n' ) { // Case to add specifically 'n' to buffer
295
+ buffer << " n" ;
296
+ } else {
297
+ buffer << ch; // Add other escape character other than /n
298
+ }
299
+ }
300
+
301
+ void Tokenizer::state10 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
302
+ char ch;
303
+ inputStream.get (ch);
304
+ if (ch == ' &' ) {
305
+ buffer << " \n Token type: BOOLEAN_AND\n " ;
306
+ buffer << " Token: &&\n " ;
307
+ } else {
308
+ inputStream.putback (ch);
309
+ buffer << " \n Token type: AMPERSAND\n " ;
310
+ buffer << " Token: &\n " ;
311
+ }
312
+ }
313
+
314
+ // Check for the closing single quote (to the non empty single quotes) of the char or escape character
315
+ void Tokenizer::state11 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
316
+ char ch;
317
+ inputStream.get (ch);
318
+ if (ch == ' \' ' ) {
319
+ buffer << " \n Token type: SINGLE_QUOTE\n " ;
320
+ buffer << " Token: " << ch << " \n " ; // Handle the final character
321
+ return state0 (inputStream, lineCount, buffer);
322
+ } else {
323
+ std::cerr << " Error: Invalid character literal\n " ;
324
+ exit (1 );
325
+ }
326
+ }
327
+
328
+ void Tokenizer::state12 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
329
+ char ch;
330
+ inputStream.get (ch);
331
+ inputStream.putback (ch);
332
+ std::string number;
333
+ number += ch;
334
+ // Keep reading until a non-digit character is found.
335
+ while (inputStream.get (ch) && ch != ' ' && ch != ' ;' && ch != ' )' && ch != ' ]' ) {
336
+ number += ch;
337
+ }
338
+
339
+ inputStream.putback (ch); // Put back the last non-digit character.
340
+
341
+ // Check if the number is valid.
342
+ if (!isValidInteger (number)) {
343
+ std::cerr << " Syntax error on line " << lineCount << " : invalid integer\n " ;
344
+ exit (1 );
345
+ }
346
+
347
+ buffer << " \n Token type: INTEGER\n " ;
348
+ buffer << " Token: " << number << " \n " ;
349
+ }
350
+
351
+ void Tokenizer::state13 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
352
+ char ch;
353
+ inputStream.get (ch);
354
+ if (ch == ' =' ) {
355
+ buffer << " \n Token type: BOOLEAN_EQUAL\n " ;
356
+ buffer << " Token: ==\n " ;
357
+ } else {
358
+ inputStream.putback (ch);
359
+ buffer << " \n Token type: ASSIGNMENT_OPERATOR\n " ;
360
+ buffer << " Token: =\n " ;
361
+ }
362
+ return state0 (inputStream, lineCount, buffer);
363
+ }
335
364
336
365
bool Tokenizer::isValidInteger (const std::string& token) {
337
366
try {
0 commit comments