Browse Source

Makefile finished

EyeXion 3 years ago
parent
commit
ec422eaa3d
4 changed files with 496 additions and 1 deletions
  1. 12
    0
      Makefile
  2. 9
    0
      grammar.txt
  3. 1
    1
      main.py
  4. 474
    0
      parser.c

+ 12
- 0
Makefile View File

@@ -0,0 +1,12 @@
1
+default : parser
2
+
3
+parser : parser.c
4
+	gcc -o parser.exe parser.c
5
+
6
+
7
+parser.c : main.py
8
+	chmod +x main.py
9
+	python3 main.py > parser.c
10
+	
11
+clean : 
12
+	rm -f parser.c parser.exe

+ 9
- 0
grammar.txt View File

@@ -0,0 +1,9 @@
1
+S : a S b
2
+S : int
3
+S : " string "
4
+S : { Assoc }
5
+Assoc : KeyVal AssocBis
6
+Assoc : 
7
+AssocBis : , KeyVal AssocBis
8
+AssocBis :
9
+KeyVal : id = S

+ 1
- 1
main.py View File

@@ -162,5 +162,5 @@ int parse_{code}(char* word, int pos) {{
162 162
 
163 163
 
164 164
 if __name__ == '__main__':
165
-    with open("sample.txt", "r") as f:
165
+    with open("grammar.txt", "r") as f:
166 166
         parse(f.readlines())

+ 474
- 0
parser.c View File

@@ -0,0 +1,474 @@
1
+#include <stdio.h>
2
+#include <string.h>
3
+
4
+void print_with_indent(int indent, char * string) {
5
+    printf("%*s%s", indent, "", string);
6
+}
7
+
8
+int parse_S(char* word, int pos);
9
+int parse_Assoc(char* word, int pos);
10
+int parse_AssocBis(char* word, int pos);
11
+int parse_KeyVal(char* word, int pos);
12
+
13
+// ---- Functions to parse terminals ----
14
+int parse_string(char* word, int pos) {
15
+    // Extract the next 6 chars of the word
16
+    char substr[7];
17
+    substr[0] = '\0';
18
+    strncat(substr, &word[pos], 6);
19
+    
20
+    // Compare this extracted string to the terminal, 
21
+    // and check if the next char is a space or the end of the string
22
+    if (strcmp(substr, "string") == 0 && (word[pos+6] == ' ' || word[pos+6] == '\0')) {
23
+        print_with_indent(pos, "string\n");
24
+        return 7;
25
+    } else {
26
+        return -1;
27
+    }
28
+}
29
+
30
+int parse_a(char* word, int pos) {
31
+    // Extract the next 1 chars of the word
32
+    char substr[2];
33
+    substr[0] = '\0';
34
+    strncat(substr, &word[pos], 1);
35
+    
36
+    // Compare this extracted string to the terminal, 
37
+    // and check if the next char is a space or the end of the string
38
+    if (strcmp(substr, "a") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
39
+        print_with_indent(pos, "a\n");
40
+        return 2;
41
+    } else {
42
+        return -1;
43
+    }
44
+}
45
+
46
+int parse_44(char* word, int pos) {
47
+    // Extract the next 1 chars of the word
48
+    char substr[2];
49
+    substr[0] = '\0';
50
+    strncat(substr, &word[pos], 1);
51
+    
52
+    // Compare this extracted string to the terminal, 
53
+    // and check if the next char is a space or the end of the string
54
+    if (strcmp(substr, ",") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
55
+        print_with_indent(pos, ",\n");
56
+        return 2;
57
+    } else {
58
+        return -1;
59
+    }
60
+}
61
+
62
+int parse_b(char* word, int pos) {
63
+    // Extract the next 1 chars of the word
64
+    char substr[2];
65
+    substr[0] = '\0';
66
+    strncat(substr, &word[pos], 1);
67
+    
68
+    // Compare this extracted string to the terminal, 
69
+    // and check if the next char is a space or the end of the string
70
+    if (strcmp(substr, "b") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
71
+        print_with_indent(pos, "b\n");
72
+        return 2;
73
+    } else {
74
+        return -1;
75
+    }
76
+}
77
+
78
+int parse_125(char* word, int pos) {
79
+    // Extract the next 1 chars of the word
80
+    char substr[2];
81
+    substr[0] = '\0';
82
+    strncat(substr, &word[pos], 1);
83
+    
84
+    // Compare this extracted string to the terminal, 
85
+    // and check if the next char is a space or the end of the string
86
+    if (strcmp(substr, "}") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
87
+        print_with_indent(pos, "}\n");
88
+        return 2;
89
+    } else {
90
+        return -1;
91
+    }
92
+}
93
+
94
+int parse_id(char* word, int pos) {
95
+    // Extract the next 2 chars of the word
96
+    char substr[3];
97
+    substr[0] = '\0';
98
+    strncat(substr, &word[pos], 2);
99
+    
100
+    // Compare this extracted string to the terminal, 
101
+    // and check if the next char is a space or the end of the string
102
+    if (strcmp(substr, "id") == 0 && (word[pos+2] == ' ' || word[pos+2] == '\0')) {
103
+        print_with_indent(pos, "id\n");
104
+        return 3;
105
+    } else {
106
+        return -1;
107
+    }
108
+}
109
+
110
+int parse_123(char* word, int pos) {
111
+    // Extract the next 1 chars of the word
112
+    char substr[2];
113
+    substr[0] = '\0';
114
+    strncat(substr, &word[pos], 1);
115
+    
116
+    // Compare this extracted string to the terminal, 
117
+    // and check if the next char is a space or the end of the string
118
+    if (strcmp(substr, "{") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
119
+        print_with_indent(pos, "{\n");
120
+        return 2;
121
+    } else {
122
+        return -1;
123
+    }
124
+}
125
+
126
+int parse_34(char* word, int pos) {
127
+    // Extract the next 1 chars of the word
128
+    char substr[2];
129
+    substr[0] = '\0';
130
+    strncat(substr, &word[pos], 1);
131
+    
132
+    // Compare this extracted string to the terminal, 
133
+    // and check if the next char is a space or the end of the string
134
+    if (strcmp(substr, "\"") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
135
+        print_with_indent(pos, "\"\n");
136
+        return 2;
137
+    } else {
138
+        return -1;
139
+    }
140
+}
141
+
142
+int parse_int(char* word, int pos) {
143
+    // Extract the next 3 chars of the word
144
+    char substr[4];
145
+    substr[0] = '\0';
146
+    strncat(substr, &word[pos], 3);
147
+    
148
+    // Compare this extracted string to the terminal, 
149
+    // and check if the next char is a space or the end of the string
150
+    if (strcmp(substr, "int") == 0 && (word[pos+3] == ' ' || word[pos+3] == '\0')) {
151
+        print_with_indent(pos, "int\n");
152
+        return 4;
153
+    } else {
154
+        return -1;
155
+    }
156
+}
157
+
158
+int parse_61(char* word, int pos) {
159
+    // Extract the next 1 chars of the word
160
+    char substr[2];
161
+    substr[0] = '\0';
162
+    strncat(substr, &word[pos], 1);
163
+    
164
+    // Compare this extracted string to the terminal, 
165
+    // and check if the next char is a space or the end of the string
166
+    if (strcmp(substr, "=") == 0 && (word[pos+1] == ' ' || word[pos+1] == '\0')) {
167
+        print_with_indent(pos, "=\n");
168
+        return 2;
169
+    } else {
170
+        return -1;
171
+    }
172
+}
173
+
174
+// ---- Functions to parse a non-terminal according to a rule ----
175
+int parse_S1(char* word, int pos) {
176
+    int totalCharParsed = 0;
177
+    int nbCharParsed;
178
+    printf("Entering S1\n");
179
+
180
+    nbCharParsed = parse_a(word, pos + totalCharParsed);
181
+    if (nbCharParsed == -1) {
182
+        printf("Fail a in S1\n");
183
+        return -1;    
184
+    }
185
+    totalCharParsed += nbCharParsed;
186
+
187
+    nbCharParsed = parse_S(word, pos + totalCharParsed);
188
+    if (nbCharParsed == -1) {
189
+        printf("Fail S in S1\n");
190
+        return -1;    
191
+    }
192
+    totalCharParsed += nbCharParsed;
193
+
194
+    nbCharParsed = parse_b(word, pos + totalCharParsed);
195
+    if (nbCharParsed == -1) {
196
+        printf("Fail b in S1\n");
197
+        return -1;    
198
+    }
199
+    totalCharParsed += nbCharParsed;
200
+
201
+    printf("Success S1\n");
202
+    return totalCharParsed;
203
+}
204
+
205
+int parse_S2(char* word, int pos) {
206
+    int totalCharParsed = 0;
207
+    int nbCharParsed;
208
+    printf("Entering S2\n");
209
+
210
+    nbCharParsed = parse_int(word, pos + totalCharParsed);
211
+    if (nbCharParsed == -1) {
212
+        printf("Fail int in S2\n");
213
+        return -1;    
214
+    }
215
+    totalCharParsed += nbCharParsed;
216
+
217
+    printf("Success S2\n");
218
+    return totalCharParsed;
219
+}
220
+
221
+int parse_S3(char* word, int pos) {
222
+    int totalCharParsed = 0;
223
+    int nbCharParsed;
224
+    printf("Entering S3\n");
225
+
226
+    nbCharParsed = parse_34(word, pos + totalCharParsed);
227
+    if (nbCharParsed == -1) {
228
+        printf("Fail 34 in S3\n");
229
+        return -1;    
230
+    }
231
+    totalCharParsed += nbCharParsed;
232
+
233
+    nbCharParsed = parse_string(word, pos + totalCharParsed);
234
+    if (nbCharParsed == -1) {
235
+        printf("Fail string in S3\n");
236
+        return -1;    
237
+    }
238
+    totalCharParsed += nbCharParsed;
239
+
240
+    nbCharParsed = parse_34(word, pos + totalCharParsed);
241
+    if (nbCharParsed == -1) {
242
+        printf("Fail 34 in S3\n");
243
+        return -1;    
244
+    }
245
+    totalCharParsed += nbCharParsed;
246
+
247
+    printf("Success S3\n");
248
+    return totalCharParsed;
249
+}
250
+
251
+int parse_S4(char* word, int pos) {
252
+    int totalCharParsed = 0;
253
+    int nbCharParsed;
254
+    printf("Entering S4\n");
255
+
256
+    nbCharParsed = parse_123(word, pos + totalCharParsed);
257
+    if (nbCharParsed == -1) {
258
+        printf("Fail 123 in S4\n");
259
+        return -1;    
260
+    }
261
+    totalCharParsed += nbCharParsed;
262
+
263
+    nbCharParsed = parse_Assoc(word, pos + totalCharParsed);
264
+    if (nbCharParsed == -1) {
265
+        printf("Fail Assoc in S4\n");
266
+        return -1;    
267
+    }
268
+    totalCharParsed += nbCharParsed;
269
+
270
+    nbCharParsed = parse_125(word, pos + totalCharParsed);
271
+    if (nbCharParsed == -1) {
272
+        printf("Fail 125 in S4\n");
273
+        return -1;    
274
+    }
275
+    totalCharParsed += nbCharParsed;
276
+
277
+    printf("Success S4\n");
278
+    return totalCharParsed;
279
+}
280
+
281
+int parse_Assoc1(char* word, int pos) {
282
+    int totalCharParsed = 0;
283
+    int nbCharParsed;
284
+    printf("Entering Assoc1\n");
285
+
286
+    nbCharParsed = parse_KeyVal(word, pos + totalCharParsed);
287
+    if (nbCharParsed == -1) {
288
+        printf("Fail KeyVal in Assoc1\n");
289
+        return -1;    
290
+    }
291
+    totalCharParsed += nbCharParsed;
292
+
293
+    nbCharParsed = parse_AssocBis(word, pos + totalCharParsed);
294
+    if (nbCharParsed == -1) {
295
+        printf("Fail AssocBis in Assoc1\n");
296
+        return -1;    
297
+    }
298
+    totalCharParsed += nbCharParsed;
299
+
300
+    printf("Success Assoc1\n");
301
+    return totalCharParsed;
302
+}
303
+
304
+int parse_Assoc2(char* word, int pos) {
305
+    int totalCharParsed = 0;
306
+    int nbCharParsed;
307
+    printf("Entering Assoc2\n");
308
+
309
+    printf("Epsilon! -> Success\n");
310
+
311
+    printf("Success Assoc2\n");
312
+    return totalCharParsed;
313
+}
314
+
315
+int parse_AssocBis1(char* word, int pos) {
316
+    int totalCharParsed = 0;
317
+    int nbCharParsed;
318
+    printf("Entering AssocBis1\n");
319
+
320
+    nbCharParsed = parse_44(word, pos + totalCharParsed);
321
+    if (nbCharParsed == -1) {
322
+        printf("Fail 44 in AssocBis1\n");
323
+        return -1;    
324
+    }
325
+    totalCharParsed += nbCharParsed;
326
+
327
+    nbCharParsed = parse_KeyVal(word, pos + totalCharParsed);
328
+    if (nbCharParsed == -1) {
329
+        printf("Fail KeyVal in AssocBis1\n");
330
+        return -1;    
331
+    }
332
+    totalCharParsed += nbCharParsed;
333
+
334
+    nbCharParsed = parse_AssocBis(word, pos + totalCharParsed);
335
+    if (nbCharParsed == -1) {
336
+        printf("Fail AssocBis in AssocBis1\n");
337
+        return -1;    
338
+    }
339
+    totalCharParsed += nbCharParsed;
340
+
341
+    printf("Success AssocBis1\n");
342
+    return totalCharParsed;
343
+}
344
+
345
+int parse_AssocBis2(char* word, int pos) {
346
+    int totalCharParsed = 0;
347
+    int nbCharParsed;
348
+    printf("Entering AssocBis2\n");
349
+
350
+    printf("Epsilon! -> Success\n");
351
+
352
+    printf("Success AssocBis2\n");
353
+    return totalCharParsed;
354
+}
355
+
356
+int parse_KeyVal1(char* word, int pos) {
357
+    int totalCharParsed = 0;
358
+    int nbCharParsed;
359
+    printf("Entering KeyVal1\n");
360
+
361
+    nbCharParsed = parse_id(word, pos + totalCharParsed);
362
+    if (nbCharParsed == -1) {
363
+        printf("Fail id in KeyVal1\n");
364
+        return -1;    
365
+    }
366
+    totalCharParsed += nbCharParsed;
367
+
368
+    nbCharParsed = parse_61(word, pos + totalCharParsed);
369
+    if (nbCharParsed == -1) {
370
+        printf("Fail 61 in KeyVal1\n");
371
+        return -1;    
372
+    }
373
+    totalCharParsed += nbCharParsed;
374
+
375
+    nbCharParsed = parse_S(word, pos + totalCharParsed);
376
+    if (nbCharParsed == -1) {
377
+        printf("Fail S in KeyVal1\n");
378
+        return -1;    
379
+    }
380
+    totalCharParsed += nbCharParsed;
381
+
382
+    printf("Success KeyVal1\n");
383
+    return totalCharParsed;
384
+}
385
+
386
+// ---- Functions to parse a non-terminal by testing all rules ----
387
+int parse_S(char* word, int pos) {
388
+    int nbCharParsed;
389
+    printf("Entering S\n");
390
+
391
+    nbCharParsed = parse_S1(word, pos);
392
+    if (nbCharParsed != -1) {
393
+        return nbCharParsed;    
394
+    }
395
+
396
+    nbCharParsed = parse_S2(word, pos);
397
+    if (nbCharParsed != -1) {
398
+        return nbCharParsed;    
399
+    }
400
+
401
+    nbCharParsed = parse_S3(word, pos);
402
+    if (nbCharParsed != -1) {
403
+        return nbCharParsed;    
404
+    }
405
+
406
+    nbCharParsed = parse_S4(word, pos);
407
+    if (nbCharParsed != -1) {
408
+        return nbCharParsed;    
409
+    }
410
+
411
+    return -1;
412
+}
413
+
414
+int parse_Assoc(char* word, int pos) {
415
+    int nbCharParsed;
416
+    printf("Entering Assoc\n");
417
+
418
+    nbCharParsed = parse_Assoc1(word, pos);
419
+    if (nbCharParsed != -1) {
420
+        return nbCharParsed;    
421
+    }
422
+
423
+    nbCharParsed = parse_Assoc2(word, pos);
424
+    if (nbCharParsed != -1) {
425
+        return nbCharParsed;    
426
+    }
427
+
428
+    return -1;
429
+}
430
+
431
+int parse_AssocBis(char* word, int pos) {
432
+    int nbCharParsed;
433
+    printf("Entering AssocBis\n");
434
+
435
+    nbCharParsed = parse_AssocBis1(word, pos);
436
+    if (nbCharParsed != -1) {
437
+        return nbCharParsed;    
438
+    }
439
+
440
+    nbCharParsed = parse_AssocBis2(word, pos);
441
+    if (nbCharParsed != -1) {
442
+        return nbCharParsed;    
443
+    }
444
+
445
+    return -1;
446
+}
447
+
448
+int parse_KeyVal(char* word, int pos) {
449
+    int nbCharParsed;
450
+    printf("Entering KeyVal\n");
451
+
452
+    nbCharParsed = parse_KeyVal1(word, pos);
453
+    if (nbCharParsed != -1) {
454
+        return nbCharParsed;    
455
+    }
456
+
457
+    return -1;
458
+}
459
+
460
+int main(int argc, char* argv[]) {
461
+    char* word;
462
+    if (argc >= 2)
463
+        word = argv[1];
464
+    else
465
+        word = "";
466
+    int value = parse_S(word, 0);
467
+    printf("%d\n", value);
468
+    if (value == strlen(word) + 1) {
469
+        printf("OK\n");
470
+    } else {
471
+        printf("KO\n");
472
+    }
473
+}
474
+

Loading…
Cancel
Save