This repository was archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
112 lines (82 loc) · 2.33 KB
/
token.c
File metadata and controls
112 lines (82 loc) · 2.33 KB
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
#include "token.h"
#include <stdio.h>
#include <stdlib.h>
void initTokenList(TokenList* tokenList)
{
tokenList->tokens = NULL;
tokenList->numberOfTokens = 0;
}
void addToken(TokenList* tokenList, Token token)
{
// Increase number of tokens
tokenList->numberOfTokens++;
// Allocate space for new token
tokenList->tokens = (Token*)realloc(tokenList->tokens, (tokenList->numberOfTokens) * sizeof(Token));
// Add token to the end of the list
tokenList->tokens[tokenList->numberOfTokens - 1] = token;
}
TokenList getCopy(TokenList src)
{
TokenList copy;
copy.numberOfTokens = src.numberOfTokens;
if(src.tokens)
{
copy.tokens = (Token*)malloc(src.numberOfTokens * sizeof(Token));
for(int i = 0; i < src.numberOfTokens; i++)
copy.tokens[i] = src.tokens[i];
}
return copy;
}
void printTokenList(TokenList tokenList, FILE* out)
{
if(out == NULL || tokenList.tokens == NULL)
return;
fprintf(out, "%10s %12s\n", "Token Type", "Lexeme");
for(int i = 0; i < tokenList.numberOfTokens; i++)
{
fprintf(out, "%10d %12s\n", tokenList.tokens[i].id, tokenList.tokens[i].lexeme);
}
}
TokenList readTokenList(FILE* in)
{
TokenList tokenList;
tokenList.tokens = NULL;
tokenList.numberOfTokens = 0;
if(!in) return tokenList;
// Skip header, which is 26 characters
fseek(in, 26, SEEK_CUR);
Token token;
while( fscanf(in, "%10d %12s\n", &token.id, token.lexeme) == 2 )
{
addToken(&tokenList, token);
}
return tokenList;
}
void deleteTokenList(TokenList* tokenList)
{
if(!tokenList) return;
if(tokenList->tokens)
free(tokenList->tokens);
tokenList->tokens = NULL;
}
TokenListIterator getTokenListIterator(TokenList* tokenList)
{
TokenListIterator it;
it.currentTokenInd = 0;
if(tokenList) it.tokenList = tokenList;
else it.tokenList = NULL;
return it;
}
Token getCurrentTokenFromIterator(TokenListIterator it)
{
if(!it.tokenList || !it.tokenList->tokens || it.currentTokenInd >= it.tokenList->numberOfTokens)
{
Token nulsymToken = { .id=0, .lexeme="" };
return nulsymToken;
}
return it.tokenList->tokens[it.currentTokenInd];
}
void advanceTokenListIterator(TokenListIterator* it)
{
if(it) it->currentTokenInd++;
}