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.h
More file actions
86 lines (71 loc) · 1.9 KB
/
token.h
File metadata and controls
86 lines (71 loc) · 1.9 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
#ifndef __TOKEN_H__
#define __TOKEN_H__
#include <stdio.h>
#define MAX_LEXEME_LENGTH 11
/**
* The struct to store token information
* */
typedef struct {
int id; // numerical representation of the token
char lexeme[MAX_LEXEME_LENGTH + 1]; // null terminated string
} Token;
/**
* The struct to store list of tokens and keep track
* of number of tokens included in the list
* */
typedef struct {
Token* tokens;
int numberOfTokens;
} TokenList;
/**
* The struct that helps to iterate on a TokenList
* */
typedef struct {
TokenList* tokenList;
int currentTokenInd;
} TokenListIterator;
/**
* Initializes the given TokenList
* */
void initTokenList(TokenList*);
/**
* Adds the given Token to the given TokenList
* */
void addToken(TokenList*, Token);
/**
* Creates and returns a copy of the given TokenList.
* TokenList dynamically allocates memory for its list.
* Therefore, shallow copying with assignment operator does not
* .. guarantee any lifetime of the included tokens list, which
* .. may cause dangling pointers.
* */
TokenList getCopy(TokenList);
/**
* Writes the given TokenList to the given FILE
* */
void printTokenList(TokenList, FILE*);
/**
* Reads a list of tokens from given file.
* The format of the list in the input file should be same as the printTokenList()
* func prints.
* */
TokenList readTokenList(FILE*);
/**
* Makes the necessary deallocations on the TokenList
* */
void deleteTokenList(TokenList*);
/**
* Creates and returns a token list iterator
* */
TokenListIterator getTokenListIterator(TokenList*);
/**
* Returns the current token from TokenListIterator.
* If the given iterator is invalid or all the tokens have already consumed,
* .. returns nulsym.
* */
Token getCurrentTokenFromIterator(TokenListIterator);
/**
* Advances the position of next token of TokenListIterator by one.
* */
void advanceTokenListIterator(TokenListIterator*);
#endif