@@ -103,6 +103,7 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
103
103
} else if (ch == ' \' ' ) { // Handle single quotes (character literals).
104
104
buffer << " \n Token type: SINGLE_QUOTE\n " ;
105
105
buffer << " Token: " << ch << " \n " ;
106
+ // state1(inputStream, lineCount, buffer);
106
107
state5 (inputStream, lineCount, buffer); // Transition to state5 for single quotes.
107
108
return state0 (inputStream, lineCount, buffer);
108
109
} else if (ch == ' _' ) { // Handle underscores.
@@ -136,13 +137,9 @@ void Tokenizer::state1(std::istringstream &inputStream, int &lineCount, std::ost
136
137
std::cerr << " Error: Unterminated string\n " ;
137
138
exit (1 );
138
139
} else if (ch == ' "' ) { // End of the string literal.
139
- buffer << " \n " ;
140
- buffer << " \n Token type: DOUBLE_QUOTE\n " ;
140
+ buffer << " \n\n Token type: DOUBLE_QUOTE\n " ;
141
141
buffer << " Token: " << ch << " \n " ;
142
142
return ;
143
- } else if (ch == ' \\ ' ) { // Handle escape characters inside the string.
144
- buffer << ch; // Add the backslash to the buffer.
145
- state9 (inputStream, lineCount, buffer); // Transition to state9 to handle the escape sequence.
146
143
} else if (ch == ' \n ' ) { // Handle newlines within the string (error).
147
144
lineCount++;
148
145
std::cerr << " Error: Unterminated string on line " << lineCount << " \n " ;
@@ -209,7 +206,7 @@ void Tokenizer::state4(std::istringstream &inputStream, int &lineCount, std::ost
209
206
return state4 (inputStream, lineCount, buffer); // Continue processing the identifier.
210
207
}
211
208
212
- // Handle character literals enclosed by single quotes.
209
+ // Handle characters enclosed by single quotes.
213
210
void Tokenizer::state5 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
214
211
char ch;
215
212
inputStream.get (ch); // Get the next character.
@@ -222,9 +219,11 @@ void Tokenizer::state5(std::istringstream &inputStream, int &lineCount, std::ost
222
219
buffer << " Token: ''\n " ; // Empty literal.
223
220
return ;
224
221
} else if (ch == ' \\ ' ) { // Escape character inside the literal.
225
- state8 (inputStream, lineCount, buffer); // Handle escape sequences.
226
- } else { // Handle regular character literals.
227
- buffer << " \n Token type: STRING\n " ;
222
+ buffer << " \n Token type: CHARACTER\n " ;
223
+ inputStream.get (ch); // Found backslash so need to get escaped character as well
224
+ buffer << " Token: \\ " << ch << " \n " ; // Add escape sequence to buffer.
225
+ } else { // Handle regular single length character literals.
226
+ buffer << " \n Token type: CHARACTER\n " ;
228
227
buffer << " Token: " << ch << " \n " ;
229
228
}
230
229
state11 (inputStream, lineCount, buffer); // Check for the closing single quote.
@@ -263,7 +262,7 @@ void Tokenizer::state7(std::istringstream &inputStream, int &lineCount, std::ost
263
262
return ;
264
263
}
265
264
266
- // Handle escape sequences inside string literals (e.g., '\n', '\t').
265
+ // Handle escape sequences inside string literals (e.g., '\n', '\t') and character literals
267
266
void Tokenizer::state8 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
268
267
char ch;
269
268
inputStream.get (ch); // Get the character after the backslash.
@@ -281,7 +280,7 @@ void Tokenizer::state8(std::istringstream &inputStream, int &lineCount, std::ost
281
280
}
282
281
283
282
// Handle character escape sequences (like '\n').
284
- void Tokenizer::state9 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
283
+ /* void Tokenizer::state9(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
285
284
char ch;
286
285
inputStream.get(ch); // Get the character after the backslash.
287
286
@@ -290,7 +289,7 @@ void Tokenizer::state9(std::istringstream &inputStream, int &lineCount, std::ost
290
289
} else {
291
290
buffer << ch; // Handle other escape characters.
292
291
}
293
- }
292
+ }*/
294
293
295
294
// Handle boolean AND (&&) and bitwise AND (&).
296
295
void Tokenizer::state10 (std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
0 commit comments