-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.cpp
190 lines (160 loc) · 5.26 KB
/
set.cpp
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
#include <cstdio>
#include <cstring>
#include <set>
#include "set.h"
#include "hash.h"
#define MAX_STR_LEN 100
int sym_num = 0;
void read_grammar(FILE *fp,
struct Grammar *grammar,
Symbol_List *hash_table[],
Symbol *symbol_table[])
{
int lhs_id = -1;
int sym_id;
unsigned int hash_value;
char buf[MAX_STR_LEN+1], str[MAX_STR_LEN+1];
Symbol *sym;
struct Rule *rule;
while(fgets(buf, MAX_STR_LEN + 1, fp)){
switch(buf[0]){
case ' ': // RHS
case '\t': // RHS
{
if(lhs_id == -1){
printf("No LHS found\n");
return;
}
struct Product *product;
struct Product_List *pl = NULL;
char *tok;
tok = strtok(buf, " \t\n\r");
// if there's something on the RHS, create a new
// product list
if(tok){
pl = create_productlist();
}
while(tok){
sym_id = symbol_id(hash_table, tok);
// new symbol found, insert it to hash_table
if(sym_id == -1){
sym = insert_symbol(hash_table, tok, sym_num);
sym_id = sym->id;
symbol_table[sym_num++] = sym;
}
// set nullable if find an epsilon
if(!strcmp("epsilon", tok)){
symbol_table[lhs_id]->nullable = 1;
symbol_table[sym_id]->nullable = 1;
}
push_product(pl, sym_id);
tok = strtok(NULL, " \t\n\r");
}
add_productlist(rule, pl);
} break;
case '\n': // new line
break;
default: // LHS (nonterminal)
{
sscanf(buf, "%s", str);
lhs_id = symbol_id(hash_table, str);
if(lhs_id == -1){
sym = insert_symbol(hash_table, str, sym_num);
lhs_id = sym_num;
symbol_table[sym_num++] = sym;
}
rule = find_rule(grammar, lhs_id);
// a new nonterminal
if(rule == NULL){
rule = create_rule(lhs_id);
grammar_addrule(grammar, rule);
}
// nonterminal already existed
else{
}
if(lhs_id != -1)
symbol_table[lhs_id]->terminal = 0;
} break;
}
buf[0] = 0;
}
}
void write_nullable(FILE *fp, struct Grammar *grammar)
{
if(!fp)
return;
Symbol *sym;
Symbol **symbol_table = grammar->symbol_table;
fprintf(fp, "Nullable\n");
for(int i = 0; i < sym_num; i++){
sym = symbol_table[i];
// output only terminal
if(!sym->terminal){
fprintf(fp, "%-25s : ", sym->name);
if(sym->nullable)
fprintf(fp, "true\n");
else
fprintf(fp, "false\n");
}
}
}
void write_first(FILE *fp, struct Grammar *grammar)
{
if(!fp)
return;
Symbol *sym;
Symbol **symbol_table = grammar->symbol_table;
std::set<int>::iterator it;
fprintf(fp, "First\n");
for(int i = 0; i < sym_num; i++){
sym = symbol_table[i];
// output only terminal
if(!sym->terminal){
fprintf(fp, "%-25s : ", symbol_table[i]->name);
for(it = sym->first.begin(); it != sym->first.end(); it++)
fprintf(fp, "%s ", symbol_table[*it]->name);
fprintf(fp, "\n");
}
}
}
void write_follow(FILE *fp, struct Grammar *grammar)
{
if(!fp)
return;
Symbol *sym;
Symbol **symbol_table = grammar->symbol_table;
std::set<int>::iterator it;
fprintf(fp, "Follow\n");
for(int i = 0; i < sym_num; i++){
sym = symbol_table[i];
// output only terminal
if(!sym->terminal){
fprintf(fp, "%-25s : ", sym->name);
for(it = sym->follow.begin(); it != sym->follow.end(); it++)
fprintf(fp, "%s ", symbol_table[*it]->name);
fprintf(fp, "\n");
}
}
}
void parser_gen(struct Grammar *grammar, char *path)
{
FILE *fp = fopen(path, "r");
FILE *set_txt = fopen("set.txt", "w");
grammar_init(grammar);
Symbol **symbol_table = grammar->symbol_table;
Symbol_List **hash_table = grammar->hash_table;
// read the grammar from file
read_grammar(fp, grammar, hash_table, symbol_table);
nullable(grammar, symbol_table);
write_nullable(set_txt, grammar);
fprintf(set_txt, "\n");
for(int i = 0; i < sym_num; i++)
first(grammar, symbol_table, i);
write_first(set_txt, grammar);
fprintf(set_txt, "\n");
follow(grammar, symbol_table);
write_follow(set_txt, grammar);
fprintf(set_txt, "\n");
fclose(fp);
fclose(set_txt);
}