-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexUtils.c
47 lines (37 loc) · 1.2 KB
/
lexUtils.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
#include "lexUtils.h"
void create_and_store_token(int kind, arrayList* list, char* string, int lineNum){
token* newToken = (token*)malloc(sizeof(token));
newToken->line = lineNum;
newToken->kind = kind;
newToken->lexeme = string;
addToken(list, newToken);
free(newToken);
}
token* next_token(arrayList* list){
token** nextToken = (token**)malloc(sizeof(token*));
if(fileIndex == list->index){
if (yylex()){
*nextToken = &list->tokens[fileIndex++];
}
else return NULL;
}
else{
*nextToken = &list->tokens[fileIndex++];
}
return *nextToken;
}
token* back_token(arrayList* list){
token** prevToken = (token**)malloc(sizeof(token*));
if(fileIndex >= 2){
*prevToken = &list->tokens[(--fileIndex) - 1];
}
return *prevToken;
}
void printToken(token* t, FILE* outputFile){
if (t != NULL){
fprintf(outputFile, "\tToken of kind '%s' was found at line: %d, lexeme: '%s' \n", toString(t->kind), t->line, t->lexeme);
}
}
void printError(char* lexeme, FILE* outputFile){
fprintf(outputFile, "\tThe character '%s' at line: %d does not begin any legal token in the language \n", lexeme, line_num);
}