-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.c
54 lines (51 loc) · 1.38 KB
/
features.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bnf.h"
void fprint_grammar_repr(bnf_grammar gr, FILE *fp)
{
size_t rule, list, term;
if (!gr.rule_number)
{
fputs("[[[ ILLEGAL BNF SPECIFICATION! ]]]", fp);
return;
}
for (rule = 0; rule < gr.rule_number; ++rule)
{
bnf_rule r = gr.rules[rule];
fprintf(fp, "<%s> ::=", r.name);
for (list = 0; list < r.list_number; ++list)
{
bnf_list l = r.lists[list];
if (list)
fputs(" |", fp);
for (term = 0; term < l.term_number; ++term)
{
bnf_term t = l.terms[term];
fputc(' ', fp);
fprintf(fp,
t.type == TRM_LIT
? strchr(t.value.literal, '"') ? "'%s'" : "\"%s\""
: "<%s>",
t.type == TRM_RULE ? t.value.rule->name : t.value.literal);
}
}
fputc('\n', fp);
}
}
void print_grammar_repr(bnf_grammar gr)
{
fprint_grammar_repr(gr, stdout);
}
bnf_rule find_rule_by_name(bnf_grammar gr, char *name)
{
size_t rule;
bnf_rule not_found = {name, 0, NULL}, r;
for(rule = 0; rule < gr.rule_number; ++rule)
{
r = gr.rules[rule];
if (!strcmp(name, r.name))
return r;
}
return not_found;
}