forked from coolsidd/Compiler-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_grammar.c
108 lines (93 loc) · 2.72 KB
/
read_grammar.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
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grammar.h"
#include "grammar_structs.h"
#define MAXLINELEN 2048
char* replace_char(char* str, char find, char replace){
char *current_pos = strchr(str,find);
while (current_pos){
*current_pos = replace;
current_pos = strchr(current_pos,find);
}
return str;
}
void printGrammar(Grammar* g) {
printf("Rules(%d): \n", g->num_rules);
printf("-------------\n");
for (int i=0;i<g->num_rules;i++) {
printf("%s -> ",toStringSymbol((g->rules)[i].lhs));
RuleNode* rnptr = (g->rules)[i].rhs;
while (rnptr != NULL) {
if ( (rnptr->s).is_terminal ) {
printf("T<%s> ", toStringSymbol(rnptr->s));
} else {
printf("NT<%s> ", toStringSymbol(rnptr->s));
}
rnptr = rnptr->next;
}
printf("\n");
}
}
int readGrammar(char *filename, Grammar* g){
char* sep = " ";
FILE* file = fopen(filename, "r");
if (!file) {
printf("Grammar: File error!\n");
return 0;
}
Rule *rptr = NULL;
char line[MAXLINELEN];
while (fgets(line, MAXLINELEN, file)) {
bool first = true;
Rule rule_new;
RuleNode *rn_head = NULL, *rn_ptr = NULL;
char* tmp_line = strdup(line); // strtok destroys a string
tmp_line = replace_char(tmp_line, '\n', '\0');
tmp_line = replace_char(tmp_line, '\r', '\0');
char* tok = strtok(tmp_line, sep); // read first token from the line
while (tok) {
if (first) {
rule_new.lhs = toSymbol(tok);
first = false;
}
else {
RuleNode *rn_new = (RuleNode*)malloc(sizeof(RuleNode));
rn_new->next = NULL;
rn_new->s = toSymbol(tok);
if (rn_ptr == NULL) {
rn_head = rn_new;
rn_ptr = rn_head;
} else {
rn_ptr->next = rn_new;
rn_ptr = rn_ptr->next;
}
}
/* printf("%s|", tok); */
tok = strtok(NULL, sep);
} /* printf("\n"); */
rule_new.rhs = rn_head;
(g->rules)[(g->num_rules)++] = rule_new;
free(tmp_line);
}
return 1;
}
void freeGrammar(Grammar *g){
for(int i = 0; i<g->num_rules; i++){
RuleNode * temp = g->rules[i].rhs;
while(temp){
g->rules[i].rhs = g->rules[i].rhs->next;
free(temp);
temp = g->rules[i].rhs;
}
free(g->rules);
}
free(g);
}