-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtokenizer_structs.c
109 lines (103 loc) · 2.58 KB
/
tokenizer_structs.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
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include "tokenizer_structs.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
void freeTokenStream(TokenStream *s){
Token * temp = s->head;
while(s->head){
s->head = s->head->next;
free(temp);
temp= s->head;
}
free(s);
}
TokenStream *insertIntoStream(TokenStream *s, int line_num, char *token_str) {
// printf("inserting \"%s\"\n", token_str);
Token *newToken = (Token *)malloc(sizeof(Token));
newToken->line_no = line_num;
newToken->token_name = token_str;
newToken->lexeme = toSymbol(token_str);
if (newToken->lexeme.s == UNKNOWN) {
char *temp = token_str;
bool flag = false;
while (*temp) {
//printf("flag : %d\n", flag);
if (isdigit(*temp)) {
temp++;
continue;
} else {
flag = true;
break;
}
}
if (flag) {
char *temp = token_str;
bool flag = false;
if (isalpha((char)*temp) && strlen(temp) <= 20) {
newToken->lexeme.s = ID;
newToken->lexeme.is_terminal = true;
} else {
printf("ERROR parsed an invalid token \"%s\"\n", token_str);
}
} else {
newToken->lexeme.s = CONST;
newToken->lexeme.is_terminal = true;
}
}
// printf("inserted \"%s\"",toStringSymbol(newToken->lexeme));
newToken->next = NULL;
if (s->tail) {
s->tail->next = newToken;
s->tail = s->tail->next;
s->length++;
return s;
}
s->head = newToken;
s->tail = newToken;
s->length++;
return s;
}
TokenStream *newTokenStream() {
TokenStream *s = (TokenStream *)malloc(sizeof(TokenStream));
s->head = NULL;
s->tail = NULL;
s->length = 0;
return s;
}
void deleteHead(TokenStream *s) {
if (!s->head) {
printf("ERROR DELETING HEAD OF SIZE 0");
return;
}
Token *temp = s->head->next;
free(s->head);
s->head = temp;
s->length--;
}
void deleteTail(TokenStream *s) {
if (!s->head) {
printf("ERROR DELETING TAIL OF SIZE 0");
return;
}
s->length--;
Token *temp = s->head;
if (!temp->next) {
free(temp);
s->head = NULL;
s->tail = NULL;
return;
}
while (temp->next->next) {
temp = temp->next;
}
s->tail = temp;
free(temp->next);
s->tail->next = NULL;
}