-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.c
More file actions
204 lines (158 loc) · 4.37 KB
/
grammar.c
File metadata and controls
204 lines (158 loc) · 4.37 KB
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
/*
* grammar.c
*/
#include "grammar.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void grammar_rule_free (struct GrammarRule *rule);
struct Grammar *
grammar_new (void)
{
struct Grammar *grammar;
grammar = malloc (sizeof (struct Grammar));
grammar->start = 'S';
grammar->n_terminals = 0;
grammar->terminals = NULL;
grammar->n_rules = 0;
grammar->rules = NULL;
return grammar;
}
static struct GrammarRule *
grammar_new_single_production (char symbol,
char *production)
{
struct GrammarRule *rule;
rule = malloc (sizeof (struct GrammarRule));
rule->symbol = symbol;
rule->n_productions = 0;
rule->productions = malloc (sizeof (char *));
rule->productions[rule->n_productions++] =
strdup (production); /* Always a good idea to own a string */
return rule;
}
void
grammar_create_rule (struct Grammar *grammar,
char symbol,
char *production)
{
grammar->rules = realloc (grammar->rules,
sizeof (struct GrammarRule)
* (grammar->n_rules + 1));
grammar->rules[grammar->n_rules++] =
grammar_new_single_production (symbol, production);
}
static char
grammar_get_unit_non_terminal (struct Grammar *grammar,
char terminal)
{
char prod[2] = { '\0' };
prod[0] = terminal;
for (int i = 0; i < grammar->n_rules; i++)
{
struct GrammarRule *rule;
rule = grammar->rules[i];
if (rule->n_productions > 0
&& (strncmp (rule->productions[0], prod, 2) == 0))
return rule->symbol;
}
utils_throw_error ("grammar_get_unit_non_terminal: unreachable code");
return '\0';
}
char
grammar_add_terminal (struct Grammar *grammar,
char c)
{
char symbol;
char prod[2] = { '\0' };
for (int i = 0; i < grammar->n_terminals; i++)
{
/* check if terminal already exists */
if (grammar->terminals[i] == c)
return grammar_get_unit_non_terminal (grammar, c);
}
/* we don't have the terminal */
grammar->terminals = realloc (grammar->terminals,
sizeof (char) * (grammar->n_terminals + 1));
grammar->terminals[grammar->n_terminals++] = c;
symbol = utils_get_next_symbol ();
prod[0] = c;
grammar_create_rule (grammar, symbol, prod);
return symbol;
}
static void
grammar_rule_add_prod (struct GrammarRule *rule,
char *production)
{
rule->productions = realloc (rule->productions,
sizeof (char) * (rule->n_productions + 1));
rule->productions[rule->n_productions++] = strdup (production);
}
void
grammar_optimize (struct Grammar *grammar)
{
for (int i = 0; i < grammar->n_rules; i++)
{
struct GrammarRule *rule1;
rule1 = grammar->rules[i];
if (rule1 == NULL)
continue;
for (int j = i + 1; j < grammar->n_rules; j++)
{
struct GrammarRule *rule2;
rule2 = grammar->rules[j];
if (rule2 != NULL
&& rule1->symbol == rule2->symbol)
{
grammar_rule_add_prod (rule1,
rule2->productions[0]);
grammar_rule_free (rule2);
grammar->rules[j] = NULL;
}
}
}
}
static void
grammar_rule_print (struct GrammarRule *rule)
{
printf ("%c -> ", rule->symbol);
for (int i = 0; i < rule->n_productions; i++)
{
if (i == rule->n_productions - 1)
printf ("%s\n", rule->productions[i]);
else
printf ("%s | ", rule->productions[i]);
}
}
void
grammar_print (struct Grammar *grammar)
{
int i = grammar->n_rules;
for (; --i >= 0; )
{
if (grammar->rules[i])
grammar_rule_print (grammar->rules[i]);
}
}
static void
grammar_rule_free (struct GrammarRule *rule)
{
for (; --rule->n_productions >= 0; )
{
if (rule->productions[rule->n_productions] != NULL)
free (rule->productions[rule->n_productions]);
}
rule->n_productions = 0;
}
void
grammar_free (struct Grammar *grammar)
{
if (grammar->terminals)
free (grammar->terminals);
for (; --grammar->n_rules >= 0; )
{
if (grammar->rules[grammar->n_rules] != NULL)
grammar_rule_free (grammar->rules[grammar->n_rules]);
}
}