-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
155 lines (128 loc) · 3.73 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "lexer.h"
void print_token(Token* token){
printf("TOKEN: %s\n", token->value);
printf("TOKEN TYPE: ");
switch(token->type){
case BEGINNING:
printf("BEGINNING\n");
break;
case INT:
printf("INT\n");
break;
case KEYWORD:
printf("KEYWORD\n");
break;
case SEPARATOR:
printf("SEPARATOR\n");
break;
}
printf("\n");
}
void print_all_tokens(Token* tokens, int len){
printf("Lexer found %d tokens:\n", len);
for(int i = 0; i < len; i++){
print_token(&tokens[i]);
}
}
void cleanup_token(Token* token){
if(token != NULL){
free(token->value);
free(token);
}
}
/*
void cleanup_all_tokens(Token* tokens, int len){
for(int i = 0; i < len; i++){
cleanup_token(&tokens[i]);
}
printf("Deleted all tokens\n"); // !? something wrong - this is not showing up in output
}
*/
Token* get_number(char current, FILE *file){
Token* number_token = malloc(sizeof(Token));
number_token->type = INT;
char* value = malloc(sizeof(char) * 8);
int index = 0;
while(isdigit(current) && current != EOF){
value[index++] = current;
current = fgetc(file);
}
value[index] = '\0';
number_token->value = value;
ungetc(current, file); // push back the non-digit character
return number_token;
}
Token *get_keyword(char current, FILE * file){
Token* keyword_token = malloc(sizeof(Token));
keyword_token->type = KEYWORD;
char* keyword = malloc(sizeof(char) * 8);
int index = 0;
while(isalpha(current) && current != EOF){
keyword[index++] = current;
current = fgetc(file);
}
keyword[index] = '\0';
if(strcmp(keyword, "exit") == 0){
keyword_token->value = strdup("EXIT");
}
else{
keyword_token->value = keyword; // directly assign the dynamically allocated keyword
}
ungetc(current, file);
return keyword_token;
}
void lexer(FILE *file, Token *tokens, int *len){
size_t tokens_i = 0;
char current;
//* fgetc() returns int (ASCII val or EOF) + forwards internal file position indicator
while((current = fgetc(file)) != EOF){
if(current == '('){
Token* open_bracket_token = malloc(sizeof(Token));
open_bracket_token->type = SEPARATOR;
open_bracket_token->value = strdup("("); //! DO I need to malloc() for the values as well or does strdup() cover it?
//print_token(open_bracket_token);
tokens[tokens_i++] = *open_bracket_token;
//cleanup_token(open_bracket_token);
}
else if(current == ')'){
Token* close_bracket_token = malloc(sizeof(Token));
close_bracket_token->type = SEPARATOR;
close_bracket_token->value = strdup(")");
//print_token(close_bracket_token);
tokens[tokens_i++] = *close_bracket_token;
//cleanup_token(close_bracket_token);
}
else if(current == ';'){
Token* semicolon_token = malloc(sizeof(Token));
semicolon_token->type = SEPARATOR;
semicolon_token->value = strdup(";");
//print_token(semicolon_token);
tokens[tokens_i++] = *semicolon_token;
//cleanup_token(semicolon_token);
}
else if(isdigit(current)){
Token* int_literal = get_number(current, file);
//print_token(int_literal);
tokens[tokens_i++] = *int_literal;
//cleanup_token(int_literal);
}
else if(isalpha(current)){
Token* keyword = get_keyword(current, file);
//print_token(keyword);
tokens[tokens_i++] = *keyword;
//cleanup_token(keyword);
}
else{
printf("ERROR: UNRECOGNIZED CHARACTER of -- %c --\n", current);
exit(1);
}
}
*len = tokens_i; // final index is the length of the 'tokens' array
//return tokens;
}