@@ -51,26 +51,11 @@ 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
56
- std::string number;
57
- number += ch;
58
- // Keep reading until a non-digit character is found.
59
- while (inputStream.get (ch) && ch != ' ' && ch != ' ;' && ch != ' )' && ch != ' ]' ) {
60
- number += ch;
61
- }
62
-
63
- inputStream.putback (ch); // Put back the last non-digit character.
64
-
65
- // Check if the number is valid.
66
- if (!isValidInteger (number)) {
67
- std::cerr << " Syntax error on line " << lineCount << " : invalid integer\n " ;
68
- exit (1 );
69
- }
70
-
54
+ inputStream.putback (ch);
71
55
buffer << " \n Token type: INTEGER\n " ;
72
- buffer << " Token: " << number << " \n " ;
73
- // *****************************************************
56
+ buffer << " Token: " ;
57
+ state12 (inputStream, lineCount, buffer);
58
+ buffer << " \n " ;
74
59
return state0 (inputStream, lineCount, buffer);
75
60
} else if (ch == ' ,' ) {
76
61
buffer << " \n Token type: COMMA\n " ;
@@ -117,7 +102,8 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
117
102
} else if (ch == ' \' ' ) { // Found a single quote
118
103
buffer << " \n Token type: SINGLE_QUOTE\n " ;
119
104
buffer << " Token: " << ch << " \n " ; // Add single quote to buffer
120
- return state5 (inputStream, lineCount, buffer); // Check what the single quote is
105
+ state5 (inputStream, lineCount, buffer); // Check what the single quote is
106
+ return state0 (inputStream, lineCount, buffer);
121
107
} else if (ch == ' _' ) {
122
108
buffer << " \n Token type: UNDERSCORE\n " ;
123
109
buffer << " Token: " << ch << " \n " ;
@@ -233,7 +219,7 @@ void Tokenizer::state5(std::istringstream &inputStream, int &lineCount, std::ost
233
219
} else if (ch == ' \' ' ) { // Check for the closing single quote (for empty single quotes) ** not sure if this should be accepted but I am for now **
234
220
buffer << " \n Token type: SINGLE_QUOTE\n " ;
235
221
buffer << " Token: ''\n " ; // Handle empty character literal
236
- return state0 (inputStream, lineCount, buffer) ;
222
+ return ;
237
223
} else if (ch == ' \\ ' ) { // Check for escaped characters
238
224
state8 (inputStream, lineCount, buffer); // Handle the esc character
239
225
}
@@ -318,34 +304,26 @@ void Tokenizer::state11(std::istringstream &inputStream, int &lineCount, std::os
318
304
if (ch == ' \' ' ) {
319
305
buffer << " \n Token type: SINGLE_QUOTE\n " ;
320
306
buffer << " Token: " << ch << " \n " ; // Handle the final character
321
- return state0 (inputStream, lineCount, buffer) ;
307
+ return ;
322
308
} else {
323
309
std::cerr << " Error: Invalid character literal\n " ;
324
310
exit (1 );
325
311
}
312
+ return ;
326
313
}
327
314
328
315
void Tokenizer::state12 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
329
316
char ch;
330
317
inputStream.get (ch);
331
- inputStream.putback (ch);
332
- std::string number;
333
- number += ch;
334
318
// 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 );
319
+ if (!isdigit (ch)){
320
+ inputStream.putback (ch);
321
+ state14 (inputStream, lineCount, buffer);
322
+ return ;
345
323
}
346
-
347
- buffer << " \n Token type: INTEGER \n " ;
348
- buffer << " Token: " << number << " \n " ;
324
+ buffer << ch;
325
+ state12 (inputStream, lineCount, buffer) ;
326
+ return ;
349
327
}
350
328
351
329
void Tokenizer::state13 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
@@ -359,17 +337,16 @@ void Tokenizer::state13(std::istringstream &inputStream, int &lineCount, std::os
359
337
buffer << " \n Token type: ASSIGNMENT_OPERATOR\n " ;
360
338
buffer << " Token: =\n " ;
361
339
}
362
- return state0 (inputStream, lineCount, buffer) ;
340
+ return ;
363
341
}
364
342
365
- bool Tokenizer::isValidInteger (const std::string& token) {
366
- try {
367
- int value = std::stoi (token);
368
- return std::to_string (value) == token;
369
- } catch (const std::out_of_range& e) {
370
- return false ;
371
- } catch (const std::invalid_argument& e) {
372
- return false ;
343
+ void Tokenizer::state14 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
344
+ char ch;
345
+ inputStream.get (ch);
346
+ if (ch != ' ' && ch != ' ;' && ch != ' )' && ch != ' ]' ) {
347
+ std::cerr << " Syntax error on line " << lineCount << " : invalid integer" ;
348
+ exit (1 );
373
349
}
350
+ inputStream.putback (ch);
351
+ return ;
374
352
}
375
-
0 commit comments