-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
440 lines (386 loc) · 11.7 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define ALLOCCONST 10000
#define REALLOCCONST 2
#define READLENGTH 32
/**
* Transition
*/
typedef struct {
int begin;
char read;
char onStack;
char write[READLENGTH];
int end;
struct transition *nextT;
} transition;
/**
* State and transitionList
*/
typedef struct {
int id;
transition *transitionList;
} state;
/**
* Global constant for number of states currently defined
*/
int numOfStates = 0;
/**
* Global constant for number of accepting states currently defined
*/
int numOfAcceptingStates = 0;
/**
* Current state, always start from 0
*/
int currentState = 0;
/**
* Current position on stack
*/
int top = 0;
char onTopStack = 'Z';
/**
* Used to activate verbose mode
*/
int verboseMode = false;
/**
* Add transition with specified parameters to transitionList
*/
void addTransition(int begin, int end, char read, char onStack, char write[READLENGTH], state states[]) {
transition *newT = (transition *) malloc(sizeof(transition));
newT->begin = begin;
newT->end = end;
newT->read = read;
newT->onStack = onStack;
for (int i = 0; i < strlen(write); i++)
newT->write[i] = write[i];
newT->write[strlen(write)] = '\0';
newT->nextT = NULL;
transition *handle = NULL;
int currState = 0;
for (int i = 0; i < numOfStates; i++) {
if (states[i].id == begin) {
handle = states[i].transitionList;
currState = i;
break;
}
}
if (states[currState].transitionList == NULL) {
states[currState].transitionList = newT;
} else {
bool tailNotFound = true;
while (tailNotFound) {
if (handle->nextT == NULL) {
handle->nextT = (struct transition *) newT;
tailNotFound = false;
} else
handle = (transition *) handle->nextT;
}
}
}
/**
* Find transition corresponding to specified parameters
* @return found transition
*/
transition *findTransition(state state, char read) {
transition *handle = state.transitionList;
while (handle != NULL) {
if (handle->read == read && onTopStack == handle->onStack)
return handle;
else
handle = (transition *) handle->nextT;
}
return NULL;
}
/**
* Function that creates a new state and insert it in states' array
* @param id is id of the state
* @param state is states' array
* @return 1 if transitions list is over, 0 otherwise
*/
int createState(state states[]) {
/**
* Two states are created in case both two begin and end state are new
*/
state *newS1 = (state *) malloc(sizeof(state));
newS1->id = 0;
newS1->transitionList = NULL;
state *newS2 = (state *) malloc(sizeof(state));
newS2->id = 0;
newS2->transitionList = NULL;
char buffer[READLENGTH];
char stateBuffer[READLENGTH];
int readingState = 0;
bool alreadyPresent = false;
int beginState = 0;
char read = '0';
char onStack = '0';
char write[READLENGTH];
int endState = 0;
/**
* 0 -> Begin state
* 1 -> Read
* 2 -> Write on stack
* 3 -> End state
*/
int phase = 0;
/**
* Reallocate state array
*/
if (numOfStates > ALLOCCONST - 1)
states = realloc(states, REALLOCCONST * sizeof(&states));
if (fgets(buffer, READLENGTH, stdin) == NULL)
exit(-1);
if ((buffer[0] == 'e' && buffer[1] == 'n' && buffer[2] == 'd'))
return 1;
int innerWrite = 0;
for (int i = 0; buffer[i] != '\n'; i++) {
if (buffer[i] != ' ') {
switch (phase) {
case 0:
stateBuffer[readingState] = buffer[i];
readingState++;
break;
case 1:
read = buffer[i];
break;
case 2:
onStack = buffer[i];
break;
case 3:
write[innerWrite] = buffer[i];
innerWrite++;
break;
case 4:
write[innerWrite] = '\0';
stateBuffer[readingState] = buffer[i];
readingState++;
break;
default:
printf("Unknown state");
}
} else {
if (phase == 0) {
stateBuffer[readingState] = '\0';
readingState = 0;
beginState = atoi(stateBuffer);
stateBuffer[0] = '\0';
for (int j = 0; j < numOfStates; j++) {
if (states[j].id == beginState) {
alreadyPresent = true;
}
}
if (!alreadyPresent) {
newS1->id = beginState;
states[numOfStates] = *newS1;
numOfStates++;
} else {
free(newS1);
newS1 = NULL;
}
alreadyPresent = false;
}
phase++;
}
}
stateBuffer[readingState] = '\0';
readingState = 0;
endState = atoi(stateBuffer);
stateBuffer[0] = '\0';
for (int j = 0; j < numOfStates; j++) {
if (states[j].id == endState) {
alreadyPresent = true;
}
}
if (!alreadyPresent) {
newS2->id = endState;
states[numOfStates] = *newS2;
numOfStates++;
} else {
free(newS2);
newS2 = NULL;
}
alreadyPresent = false;
addTransition(beginState, endState, read, onStack, write, states);
return 0;
}
/**
* Function that insert a new accepting state in accepting states' array
* @param id is id of the state
* @param state is accepting states' array
*/
int insertAcceptingState(int accStates[]) {
char buffer[READLENGTH];
if (fgets(buffer, READLENGTH, stdin) == NULL)
exit(-1);
if (numOfAcceptingStates > ALLOCCONST - 1)
accStates = realloc(accStates, REALLOCCONST * sizeof(&accStates));
if ((buffer[0] == 'e' && buffer[1] == 'n' && buffer[2] == 'd'))
return 1;
else {
accStates[numOfAcceptingStates] = atoi(buffer);
numOfAcceptingStates++;
return 0;
}
}
/**
* For details about syntax please check README
*/
void parsingMenu(state states[], int acceptingStates[]) {
char buffer[READLENGTH];
if (fgets(buffer, READLENGTH, stdin) == NULL)
exit(-1);
while (createState(states) != 1);
buffer[0] = '\0';
if (fgets(buffer, READLENGTH, stdin) == NULL)
exit(-1);
while (insertAcceptingState(acceptingStates) != 1);
}
/**
* PDA operating function
*/
void routine(state states[], int acceptingStates[], char stack[]) {
char buffer;
bool end = false;
bool notAccepted = false;
transition *newT = NULL;
while (!end) {
buffer = getc(stdin);
switch (buffer) {
case '$':
end = true;
break;
case '\r':
break;
case '\n':
for (int i = 0; i < numOfAcceptingStates && !notAccepted; i++) {
if (acceptingStates[i] == currentState) {
printf("String accepted\n");
break;
} else if (i == numOfAcceptingStates - 1)
printf("String not accepted\n");
}
/**
* Reset
*/
currentState = 0;
memset(stack, '_', sizeof(char) * ALLOCCONST);
stack[0] = 'Z';
onTopStack = 'Z';
top = 0;
notAccepted = false;
break;
default:
for (int i = 0; i < numOfStates && !notAccepted; i++) {
if (states[i].id == currentState) {
newT = findTransition(states[i], buffer);
if (newT != NULL) {
currentState = newT->end;
if (verboseMode) {
printf("------------------------------------\n");
printf("Current state %d\n", currentState);
printf("Going to %d\n", newT->begin);
}
for (int j = 0; j < strlen(newT->write); j++) {
if (newT->write[j] == '|') {
stack[top] = '_';
top--;
onTopStack = stack[top];
break;
} else {
stack[top] = newT->write[j];
onTopStack = stack[top];
if (j != strlen(newT->write) - 1)
top++;
}
}
if (verboseMode) {
printf("------------------------------------\n");
}
} else {
printf("String not accepted\n");
if (verboseMode) {
printf("------------------------------------\n");
}
notAccepted = true;
break;
}
break;
}
}
}
}
}
/**
* Print current states and transitions read, usable for debugging
*/
void printStateAndTransitions(state states[], int acceptingStates[]) {
printf("------------------------------------\n");
for (int i = 0; i < numOfStates; i++) {
printf("State: %d\n", states[i].id);
transition *handle = states[i].transitionList;
int j = 0;
while (handle != NULL) {
printf("Transition found %c %c %s -> %d\n", handle->read, handle->onStack, handle->write, handle->end);
handle = (transition *) handle->nextT;
j++;
}
}
printf("Accepting states:\n");
for (int i = 0; i < numOfAcceptingStates; i++) {
printf("%d\n", acceptingStates[i]);
}
printf("------------------------------------\n");
}
/**
* Command validation
*/
void validateArguments(int argc, char **argv) {
switch (argc) {
case 1:
break;
case 2:
if (strcmp(argv[1], "-v") == 0)
verboseMode = true;
else {
printf("Unexpected command, exiting...\n");
exit(-1);
}
break;
default:
printf("Unexpected command, exiting...\n");
exit(-1);
}
}
int main(int argc, char **argv) {
/**
* Command validation
*/
validateArguments(argc, argv);
/**
* PDA stack
*/
char *stack = (char *) malloc(sizeof(char) * ALLOCCONST);
memset(stack, '_', sizeof(char) * ALLOCCONST);
stack[0] = 'Z';
/**
* States array (every cell points to a list of possible transitions)
*/
state *states = (state *) malloc(sizeof(state) * ALLOCCONST);
/**
* Accepting states array (contains only ID)
*/
int *acceptingStates = (int *) malloc(sizeof(int) * ALLOCCONST);
/**
* Setup of PDA
*/
parsingMenu(states, acceptingStates);
if (verboseMode)
printStateAndTransitions(states, acceptingStates);
/**
* Begin operating
*/
routine(states, acceptingStates, stack);
return 0;
}