-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.c
158 lines (147 loc) · 2.92 KB
/
token.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
/* Austin Hester
CS 4280 sp18
C.Z. Janikow */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "token.h"
#include "states.h"
// I know these first few aren't actually tokens.
// Just so the indices match with enum STATE
char* TOKENS[] = {
"errorTK",
"initialTK",
"s1TK",
"s2TK",
"s3TK",
"s4TK",
"s5TK",
"opTK",
"delimTK",
"startTK",
"stopTK",
"iterTK",
"voidTK",
"varTK",
"returnTK",
"readTK",
"printTK",
"programTK",
"iffTK",
"thenTK",
"letTK",
"idTK",
"intTK",
"=TK",
"==TK",
">TK",
">>TK",
"<TK",
"<<TK",
":TK",
"+TK",
"-TK",
"*TK",
"/TK",
"%TK",
".TK",
"(TK",
")TK",
",TK",
"{TK",
"}TK",
";TK",
"[TK",
"]TK",
"&TK",
"\"TK",
"EOFTK"
};
// get the token indexed by state
char*
gettoken(enum STATE state)
{
return TOKENS[state];
}
// is this the EOF token?
int
isEOFtoken(const token_t* token)
{
if (strcmp(token->id, "EOFTK") == 0)
return 1;
return 0;
}
// make a token
void
maketoken(token_t* token,
const enum STATE state,
char* string,
const int line)
{
/* int len = strlen(string);
if (len > 8)
string[8] = '\0'; */
token->instance = (char*) malloc(16*sizeof(char));
strcpy(token->instance, string);
token->id = gettoken(state);
token->line_num = line;
return;
}
token_t*
customtoken(char* id, char* string, int line)
{
/* int len = strlen(string);
if (len > 8)
string[8] = '\0'; */
token_t* token = (token_t*) malloc(sizeof(token_t));
token->instance = (char*) malloc(16*sizeof(char));
strcpy(token->instance, string);
token->id = (char*) malloc(16*sizeof(char));
strcpy(token->id, id);
token->line_num = line;
return token;
}
void
copytokenlist(token_t** dest, token_t** src, int n)
{
if (src == (token_t**)NULL)
return;
int i;
for (i = 0; i < n; i++) {
// Copy original token list
dest[i] = (token_t*) malloc(sizeof(token_t));
copytoken(dest[i], src[i]);
}
}
void
copytoken(token_t* dest, token_t* src)
{
if (dest == (token_t*)NULL)
return;
dest->id = (char*) malloc(16*sizeof(char));
strcpy(dest->id, src->id);
dest->instance = (char*) malloc(16*sizeof(char));
strcpy(dest->instance, src->instance);
dest->line_num = src->line_num;
}
// display a list of tokens
void
displaytokens(token_t** tokenlist, const int numtokens)
{
printf("\nFinal token list, in order:\n");
printf("========================\n");
int i;
for (i = 0; i < numtokens; i++) {
const token_t* t = tokenlist[i];
if (t == (token_t*)NULL)
break;
displaytoken(t);
}
printf("========================\n");
printf("\n");
}
void
displaytoken(const token_t* t)
{
printf("%s, \'%s\', %d\n", t->id, t->instance, t->line_num);
}