-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsadriver.c
153 lines (136 loc) · 4.71 KB
/
fsadriver.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
/* Austin Hester
CS 4280 sp18
C.Z. Janikow */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "fsadriver.h"
#include "fsatable.h"
#include "wordlist.h"
#include "token.h"
#include "states.h"
#include "keywords.h"
// for tracking place in the filter
static int line = 0;
static int column = 0;
// I apologize for how ridiculous this function is.
// I'll split it up in the future
token_t*
fsadriver(const wordlist_t* filter)
{
int i = 0;
enum STATE state = INITIAL;
enum STATE nextstate = ERROR;
token_t* token = (token_t*) malloc(sizeof(token_t));
char nextchar;
char string[256] = "";
int numlines = filter->length;
char buf[256];
for (line = line; line < numlines; line++) {
// first for loop for each line
//fprintf(stderr, "line = %s\n", filter->list[line]);
if (filter->list[line] == (char*)NULL) {
// If next line is NULL,
// \t is a special char here, signifies
// EOF to FSA
nextchar = 26;
} else {
// copy new line to buffer
strcpy(buf, filter->list[line]);
nextchar = buf[column];
}
// loop until FINAL state, breaks on end of line
// which allows for multi-line tokens
while (state < IDENTIFIER)
{
nextstate = fsatable(state, nextchar);
//fprintf(stderr, "state\t = %d\n", state);
//fprintf(stderr, "string\t = .%s.\n", string);
//fprintf(stderr, "next\t = %c,\n", nextchar);
// If there is an error
if (nextstate == ERROR) {
fprintf(stderr, "Error while parsing @ line ");
fprintf(stderr, "%d\n", line);
fprintf(stderr, "Check last state and next for info.\n");
return (token_t*)NULL;
}
// If we have reached a final state
if (nextstate >= START) {
state = nextstate;
// If we have a single character token,
// add the current char to string
if (i == 0) {
if (nextchar != '\n' && nextchar != ' ') {
string[i] = nextchar;
}
}
if (nextstate == EQUALSEQUALS ||
nextstate == GREATEREQUALS ||
nextstate == LESSEQUALS) {
string[i] = nextchar;
column++;
}
// the following corrects error of line number
// when the lookahead requires going to the
// next line
int lastline = line;
if (column == 0 && i != 0)
lastline--;
// Identifiers cannot begin with capital
if (nextstate == IDENTIFIER && isupper(string[0])) {
fprintf(stderr, "Error while parsing @ line ");
fprintf(stderr, "%d\n", line);
fprintf(stderr, "Identifiers cannot begin with Upper\n");
return (token_t*)NULL;
}
// Check if keyword
if (iskeyword(string)) {
state = whichkeyword(string);
}
char* cutstring = (char*) malloc(16*sizeof(char));
strncpy(cutstring, string, 8);
cutstring[8] = '\0';
maketoken(token, state, cutstring, lastline);
// this is needed for avoiding infinite loops in table
if (i < 1)
column++;
// is the next char the end?
// if so let the position vars know
nextchar = buf[column];
if (nextchar == '\0' || nextchar == '\n') {
column = 0;
line++;
}
// Return that token
return token;
// If we're still not done
} else {
state = nextstate;
if (nextchar != '\n' && nextchar != ' ') {
string[i] = nextchar;
i++;
}
column++;
nextchar = buf[column];
if (nextchar == '\0') {
column = 0;
break;
}
}
// inside while
}
// outside of while
// token is still processing.
// new lines do not end scanning
}
// we did not encounter an error, but no token was made
// must be END OF FILE
maketoken(token, EOFILE, "EOF", line);
return token;
}
void
resetfsadriver() {
line = 0;
column = 0;
}