/
tokenizer.c
294 lines (236 loc) · 8.16 KB
/
tokenizer.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* tokenizer.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tokenizer.h"
#define MAX_HEX_CHARS 2
#define MAX_OCT_CHARS 3
/*
* Tokenizer type. You need to fill in the type as part of your implementation.
*/
char is_escape_character(char character) {
/*
* Description: determines if a character is a member of the set of escaped characters
* Parameters: character to be evaluated
* Modifies: nothing
* Returns: appropriate escape character sequence if it is a member, 0 if it is not
*
*/
char* escape_sequence = "ntvbrfa\\?'\"";
char* escape_characters = "\n\t\v\b\r\f\a\\\?\'\"";
int offset = 0;
for(offset = 0; offset < strlen(escape_sequence); offset++) {
if(*(escape_sequence + offset) == character) {
return *(escape_characters + offset);
}
}
return 0;
}
int char_to_hex(char character) {
/*
* Description: converts a hex digit represented as a character into its actual integer value
* Parameters: character to be converted
* Modifies: nothing
* Returns: value of the hex digit character as an integer
*
*/
if(isdigit(character)){
return character - '0';
} else if (islower(character)){
return character - 'a' + 10;
} else {
return character - 'A' + 10;
}
}
int char_to_oct(char character) {
/*
* Description: converts a octal digit represented as a character into its actual integer value
* Parameters: character to be converted
* Modifies: nothing
* Returns: value of the octal digit character as an integer
*
*/
return character - '0';
}
int is_oct_digit(char oct_digit) {
/*
* Description: determines if a character represents a octal digit
* Parameters: character to be evaluated
* Modifies: nothing
* Returns: 1 if it is an octal character, 0 if it is not
*
*/
if(oct_digit >= '0' && oct_digit <= '7') {
return 1;
} else {
return 0;
}
}
char* unescape_string(char* string) {
/*
* Description: given an input stream converts escaped characters into their appropriate char representation and returns it as a new string
* Parameters: string to be converted
* Modifies: nothing
* Returns: resulting string with all the escaped characters with their appropriate values
*
*/
char* unescaped_string = (char*)malloc(strlen(string) * sizeof(char) + 1);
int current_position = 0;
int unescaped_string_position = 0;
unsigned char escape_character = 0;
for(current_position = 0; current_position < strlen(string); current_position++) {
escape_character = *(string + current_position);
if(*(string + current_position) == '\\') {
if(*(string + current_position + 1) == 'x') {
current_position++;
int hex_count;
escape_character = 0;
for(hex_count = 1; hex_count <= MAX_HEX_CHARS; hex_count++) {
if(!isxdigit(*(string + current_position + hex_count))) {
break;
}
escape_character = escape_character * 16 + char_to_hex(*(string + current_position + hex_count));
}
hex_count--;
current_position += hex_count;
} else if(is_oct_digit((*(string + current_position + 1))) == 1) {
int oct_count;
escape_character = 0;
for(oct_count = 1; oct_count <= MAX_OCT_CHARS; oct_count++) {
if(is_oct_digit(*(string + current_position + oct_count)) == 0) {
break;
}
escape_character = escape_character * 8 + char_to_oct(*(string + current_position + oct_count));
}
current_position += oct_count;
} else {
escape_character = is_escape_character(*(string + current_position + 1));
if(escape_character == 0) {
escape_character = *(string + current_position);
} else {
current_position++;
}
}
}
*(unescaped_string + unescaped_string_position) = escape_character;
unescaped_string_position++;
escape_character = 0;
}
*(unescaped_string + unescaped_string_position) = '\0';
return unescaped_string;
}
/*
* TKCreate creates a new TokenizerT object for a given set of separator
* characters (given as a string) and a token stream (given as a string).
*
* TKCreate should copy the two arguments so that it is not dependent on
* them staying immutable after returning. (In the future, this may change
* to increase efficiency.)
*
* If the function succeeds, it returns a non-NULL TokenizerT.
* Else it returns NULL.
*
* You need to fill in this function as part of your implementation.
*/
TokenizerT *TKCreate(char *separators, char *ts) {
/*
* Description: creates a new tokenizer struct from the token stream and delimiters
* Parameters: set of delimiters, token stream
* Modifies: nothing
* Returns: a pointer to a tokenizer struct on success, a null pointer on failure
*
*/
if(separators == NULL || ts == NULL){
return NULL;
}
TokenizerT* tokenizer = (TokenizerT*)malloc(sizeof(TokenizerT));
if(tokenizer == NULL){
return NULL;
}
tokenizer->delimiters = unescape_string(separators);
tokenizer->copied_string = unescape_string(ts);
tokenizer->current_position = tokenizer->copied_string;
return tokenizer;
}
/*
* TKDestroy destroys a TokenizerT object. It should free all dynamically
* allocated memory that is part of the object being destroyed.
*
* You need to fill in this function as part of your implementation.
*/
void TKDestroy(TokenizerT *tk) {
/*
* Description: destroys tokenizer struct and deallocates all memory
* Parameters: tokenizer to be destroyed
* Modifies: tokenizer struct: deallocates memory
* Returns: nothing
*/
free(tk->copied_string);
free(tk->delimiters);
free(tk);
return;
}
char is_delimiter(char character, char* delimiters) {
/*
* Description: determines if a particular character is a member of the set of delimiters
* Parameters: character to be compared, string of delimiters
* Modifies: Nothing
* Returns: 1 if character is a delimiter, 0 if it is not
*/
int x = (int) tolower(character);
if ((x>= 48 && x <= 57) || (x >= 97 && x <= 122)) {
return 0;
}
return 1;
}
/*
* TKGetNextToken returns the next token from the token stream as a
* character string. Space for the returned token should be dynamically
* allocated. The caller is responsible for freeing the space once it is
* no longer needed.
*
* If the function succeeds, it returns a C string (delimited by '\0')
* containing the token. Else it returns 0.
*
* You need to fill in this function as part of your implementation.
*/
char *TKGetNextToken(TokenizerT *tk) {
/*
* Description: returns the next token from the token stream specified within the tokenizer
* Parameters: tokenizer from which to extract token
* Modifies: tokenizer->current_position: identifies starting point of next token; creates a new string with
* Returns: token extracted as a char* on success, null on failure/end of string;
*/
char* token = NULL;
char* token_start = NULL;
while(tk->current_position - tk->copied_string < strlen(tk->copied_string)) {
if(!is_delimiter(*tk->current_position, tk->delimiters)) {
token_start = tk->current_position;
break;
}
tk->current_position++;
}
if(token_start == NULL) {
return NULL;
}
while(tk->current_position - tk->copied_string < strlen(tk->copied_string)) {
if(is_delimiter(*tk->current_position, tk->delimiters)) {
break;
}
tk->current_position++;
}
token = (char*)malloc(sizeof(char) * (tk->current_position - token_start + 1));
strncpy(token, token_start, tk->current_position - token_start);
token[(tk->current_position - token_start)] = '\0';
return token;
}
/*
* main will have two string arguments (in argv[1] and argv[2]).
* The first string contains the separator characters.
* The second string contains the tokens.
* Print out the tokens in the second string in left-to-right order.
* Each token should be printed on a separate line.
*/