-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.c
333 lines (275 loc) · 9.32 KB
/
lexer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/********************************************
2017B4A70696P Abdul Kadir Khimani
********************************************/
#include "lexer.h"
/*This function reads data from file.
Parameters :
fp : file pointer of file from which we have to read
s : head pointer of linked list of tokens
Returns :
tkn : the token pointer if unsuccessful(error detected),
and NULL if parsed successfully
*/
TokenDetails* tokeniseSourceCode(char* fileName, tokenStream *s){
FILE* fp = fopen(fileName, "r");
if(fp == NULL){
printf(RED"\nFile not found..\n"RESET);
return NULL;
}
//Caculate file size
fseek(fp, 0, SEEK_END);
size_t f_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
//Create an input buffer for the entire code size
char input_buffer[f_size + 1];
int no_of_chars_read = 0;
//Read source file into input_buffer
if((no_of_chars_read=(fread(input_buffer, sizeof(char), f_size, fp)))>0){
input_buffer[no_of_chars_read]='\0';
printf(GREEN"\nNUMBER OF BYTES READ : %d Bytes"RESET,no_of_chars_read);
}
int line_no = 1; //Current line number
char curr_lexeme[21] = {'\0'}; //Current lexeme
for(int i = 0; strlen(input_buffer); i++){
//Current Lexeme pointer
int j = 0;
//Remove all extra spaces and tabs before a lexeme
while(input_buffer[i] != '\0' && (input_buffer[i] == ' '
|| input_buffer[i] == '\t'|| input_buffer[i] == '\n')){
if(input_buffer[i] == '\n')
line_no++;
i++;
}
//Break if you reach the end of input_buffer(eof)
if(input_buffer[i] == '\0') break;
//Read the lexeme
while(j <= 20 && input_buffer[i] != '\0' && input_buffer[i] != ' '
&& input_buffer[i] != '\n' && input_buffer[i] != '\t')
curr_lexeme[j++] = input_buffer[i++];
//if lexeme has length > 20, raise error
if(j == 21){
curr_lexeme[j-1] = '\0';
TokenDetails* tkn = createToken(curr_lexeme, line_no);
tkn->error = "Lexical Error : Identifier has length greater than 20";
tkn->is_error = true;
return tkn;
}
//if you are here, then lexeme has been read successfully
curr_lexeme[j] = '\0';
//Create the token for the lexeme
TokenDetails* tkn = createToken(curr_lexeme, line_no);
//Check if an error was detected in token creation
//Bad token: Raise error
if(tkn->error != NULL)
return tkn;
//Add good token to linkedlist
addToken(tkn, s);
//increase line_number if newline encountered
if(input_buffer[i] == '\n') line_no++;
//break if you reach end of input_buffer
if(input_buffer[i] == '\0') break;
}
//successfully created all tokens
return NULL;
}
/*This function adds token to the linked list.
Parameters :-
node : pointer to the token
s : head pointer of linked list of tokens
*/
void addToken(TokenDetails *node, tokenStream *s)
{
if(s->start == NULL)
{
s->start = node;
s->end = node;
}
else
{
s->end->next = node;
node->next = NULL;
node->prev = s->end;
s->end = node;
}
}
/*This function adds prints all tokens in the linked list.
Parameters :-
s : head pointer of linked list of tokens
*/
void printTokens(tokenStream *s)
{
printf(RED"\n--------------PRINTING TOKEN DETAILS--------------\n"RESET);
TokenDetails *ptr = s->start;
while(ptr != NULL)
{
printf("Token : "CYAN"%12s"RESET" | Lexeme : "GREEN"%12s "RESET"| Line no. : "YELLOW"%3d"RESET" | Terminal no. : "RED"%3d"RESET, ptr->token, ptr->lexeme, ptr->line_no, ptr->terminal_no);
printf("\n");
ptr = ptr->next;
}
}
/*This function checks if the lexeme is a number
Parameters :-
str : lexeme to be checked
*/
bool isNumeric(char* str) {
for (int i = 0; str[i]; i++)
if (isdigit(str[i]) == false)
return false; //when one non numeric value is found, return false
return true;
}
TokenDetails* createToken(char lexeme[], int line_no)
{
//create token for the lexeme with metadata
TokenDetails *tkn = (TokenDetails*) malloc(sizeof(TokenDetails));
tkn->line_no = line_no;
strcpy(tkn->lexeme, lexeme);
tkn->error = NULL;
tkn->is_error = false;
tkn->is_num = false;
tkn->next = NULL;
tkn->prev = NULL;
tkn->terminal_no = -1;
//Check one by one for each token and map it to lexeme
if(strcmp(lexeme, "+") == 0){
strcpy(tkn->token, "PLUS");
tkn->terminal_no = findTerminal("PLUS");
}
else if(strcmp(lexeme, "-") == 0){
strcpy(tkn->token, "MINUS");
tkn->terminal_no = findTerminal("MINUS");
}
else if(strcmp(lexeme, "*") == 0){
strcpy(tkn->token, "MUL");
tkn->terminal_no = findTerminal("MUL");
}
else if(strcmp(lexeme, "/") == 0){
strcpy(tkn->token, "DIV");
tkn->terminal_no = findTerminal("DIV");
}
else if(strcmp(lexeme, "]") == 0){
strcpy(tkn->token, "SQBC");
tkn->terminal_no = findTerminal("SQBC");
}
else if(strcmp(lexeme, "[") == 0){
strcpy(tkn->token, "SQBO");
tkn->terminal_no = findTerminal("SQBO");
}
else if(strcmp(lexeme, "}") == 0){
strcpy(tkn->token, "CYBC");
tkn->terminal_no = findTerminal("CYBC");
}
else if(strcmp(lexeme, "{") == 0){
strcpy(tkn->token, "CYBO");
tkn->terminal_no = findTerminal("CYBO");
}
else if(strcmp(lexeme, "(") == 0){
strcpy(tkn->token, "RBO");
tkn->terminal_no = findTerminal("RBO");
}
else if(strcmp(lexeme, ")") == 0){
strcpy(tkn->token, "RBC");
tkn->terminal_no = findTerminal("RBC");
}
else if(strcmp(lexeme, "program") == 0){
strcpy(tkn->token, "PROGRAM");
tkn->terminal_no = findTerminal("PROGRAM");
}
else if(strcmp(lexeme, "&&&") == 0){
strcpy(tkn->token, "AND");
tkn->terminal_no = findTerminal("AND");
}
else if(strcmp(lexeme, "|||") == 0){
strcpy(tkn->token, "OR");
tkn->terminal_no = findTerminal("OR");
}
else if(strcmp(lexeme, ":") == 0){
strcpy(tkn->token, "COLON");
tkn->terminal_no = findTerminal("COLON");
}
else if(strcmp(lexeme, ";") == 0){
strcpy(tkn->token, "SEMICOLON");
tkn->terminal_no = findTerminal("SEMICOLON");
}
else if(strcmp(lexeme, "..") == 0){
strcpy(tkn->token, "ELLIPSIS");
tkn->terminal_no = findTerminal("ELLIPSIS");
}
else if(strcmp(lexeme, "boolean") == 0){
strcpy(tkn->token, "BOOLEAN");
tkn->terminal_no = findTerminal("BOOLEAN");
}
else if(strcmp(lexeme, "declare") == 0){
strcpy(tkn->token, "DECLARE");
tkn->terminal_no = findTerminal("DECLARE");
}
else if(strcmp(lexeme, "integer") == 0){
strcpy(tkn->token, "INTEGER");
tkn->terminal_no = findTerminal("INTEGER");
}
else if(strcmp(lexeme, "jagged") == 0){
strcpy(tkn->token, "JAGGED");
tkn->terminal_no = findTerminal("JAGGED");
}
else if(strcmp(lexeme, "array") == 0){
strcpy(tkn->token, "ARRAY");
tkn->terminal_no = findTerminal("ARRAY");
}
else if(strcmp(lexeme, "list") == 0){
strcpy(tkn->token, "LIST");
tkn->terminal_no = findTerminal("LIST");
}
else if(strcmp(lexeme, "of") == 0){
strcpy(tkn->token, "OF");
tkn->terminal_no = findTerminal("OF");
}
else if(strcmp(lexeme, "#") == 0){
strcpy(tkn->token, "EPSILON");
tkn->terminal_no = findTerminal("#");
}
else if(strcmp(lexeme, "R1") == 0){
strcpy(tkn->token, "R1");
tkn->terminal_no = findTerminal("R1");
}
else if(strcmp(lexeme, "real") == 0){
strcpy(tkn->token, "REAL");
tkn->terminal_no = findTerminal("REAL");
}
else if(strcmp(lexeme, "size") == 0){
strcpy(tkn->token, "SIZE");
tkn->terminal_no = findTerminal("SIZE");
}
else if(strcmp(lexeme, "values") == 0){
strcpy(tkn->token, "VALUES");
tkn->terminal_no = findTerminal("VALUES");
}
else if(strcmp(lexeme, "variables") == 0){
strcpy(tkn->token, "VARIABLES");
tkn->terminal_no = findTerminal("VARIABLES");
}
else if(strcmp(lexeme, "=") == 0){
strcpy(tkn->token, "ASSSIGNOP");
tkn->terminal_no = findTerminal("ASSIGNOP");
}
else if(isNumeric(lexeme)){
strcpy(tkn->token, "NUM");
tkn->is_num = true;
tkn->terminal_no = findTerminal("NUM");
tkn->value = atoi(lexeme);
}
else{ //if it is an identifier
if(isdigit(lexeme[0])){
tkn->is_error = true;
tkn->error = "Lexical Error : Identifier contains first character as number";
}
for(int i = 0; lexeme[i]; i++){
if(!isdigit(lexeme[i]) && !isalpha(lexeme[i]) && lexeme[i] != '_'){
tkn->is_error = true;
tkn->error = "Lexical Error : Identifier contains an invalid character";
break;
}
}
strcpy(tkn->token, "ID");
tkn->terminal_no = findTerminal("ID");
}
return tkn;
}