aboutsummaryrefslogtreecommitdiff
path: root/src/bnf-parser.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:02:38 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:26:07 +0000
commit6ecea40c849effd171c1d27c961f5c1ee100481f (patch)
tree99a92dafddf7d2ef0e318d927ae70c3b642e4171 /src/bnf-parser.c
downloadlab-03-6ecea40c849effd171c1d27c961f5c1ee100481f.tar.gz
publishHEADmaster
Diffstat (limited to 'src/bnf-parser.c')
-rw-r--r--src/bnf-parser.c191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/bnf-parser.c b/src/bnf-parser.c
new file mode 100644
index 0000000..7aa26f0
--- /dev/null
+++ b/src/bnf-parser.c
@@ -0,0 +1,191 @@
1#include <stdarg.h>
2#include "bnf-parser.h"
3#include "dynamic-array.h"
4
5void
6print_error (struct bnf_lexer *lexer, const char *point,
7 const char *template, ...)
8{
9 int row = 1;
10 int col = 0;
11
12 for (const char *head = lexer->sv.beg; head < point; ++head) {
13 col += 1;
14 if (*head == '\n') {
15 row += 1;
16 col = 0;
17 }
18 };
19
20 fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col);
21
22 va_list ap;
23 va_start (ap, template);
24 vfprintf (stderr, template, ap);
25 va_end (ap);
26
27 putc ('\n', stderr);
28}
29
30bool
31is_bnf_lexeme_valid (struct bnf_lexeme lexeme)
32{
33 return lexeme.kind != BNF_LEXEME_KIND_INVALID;
34}
35
36bool
37bnf_parse_lexeme (struct bnf_lexer *lexer, enum bnf_lexeme_kind kind)
38{
39 const char *checkpoint = lexer->cursor;
40 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
41
42 if (lexeme.kind != kind) goto revert;
43
44 return true;
45
46revert:
47 lexer->cursor = checkpoint;
48 return false;
49}
50
51struct bnf_lexeme
52bnf_parse_nonterminal (struct bnf_lexer *lexer)
53{
54 const char *checkpoint = lexer->cursor;
55 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
56 if (lexeme.kind == BNF_LEXEME_KIND_NONTERMINAL)
57 return lexeme;
58
59revert:
60 lexer->cursor = checkpoint;
61 return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
62}
63
64struct bnf_lexeme
65bnf_parse_terminal (struct bnf_lexer *lexer)
66{
67 const char *checkpoint = lexer->cursor;
68
69 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
70 if (lexeme.kind == BNF_LEXEME_KIND_TERMINAL)
71 return lexeme;
72
73revert:
74 lexer->cursor = checkpoint;
75 return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
76}
77
78bool
79is_bnf_sequence_valid (struct bnf_sequence sequence)
80{
81 return sequence.term_count != 0;
82}
83
84struct bnf_sequence
85bnf_parse_sequence (struct bnf_lexer *lexer)
86{
87 struct bnf_sequence sequence = {0};
88 const char *checkpoint = lexer->cursor;
89
90 for (;;) {
91 struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer);
92 if (!is_bnf_lexeme_valid (lexeme))
93 lexeme = bnf_parse_terminal (lexer);
94 if (!is_bnf_lexeme_valid (lexeme))
95 break;
96
97 DYNAMIC_ARRAY_APPEND (sequence, lexeme);
98 } if (!is_bnf_sequence_valid (sequence)) goto revert;
99
100 if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) ||
101 bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE))
102 return sequence;
103
104revert:
105 DYNAMIC_ARRAY_FREE (sequence);
106 lexer->cursor = checkpoint;
107 return (struct bnf_sequence) {0};
108}
109
110bool
111is_bnf_rule_valid (struct bnf_rule rule)
112{
113 return rule.is_replaced_by.sequence_count != 0;
114}
115
116struct bnf_rule
117bnf_parse_rule (struct bnf_lexer *lexer)
118{
119 struct bnf_rule rule = {0};
120 const char *checkpoint = lexer->cursor;
121
122 rule.name = bnf_parse_nonterminal (lexer);
123 if (!is_bnf_lexeme_valid (rule.name))
124 goto revert;
125
126 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY))
127 goto revert;
128
129 for (;;) {
130 struct bnf_sequence sequence = bnf_parse_sequence (lexer);
131 if (!is_bnf_sequence_valid (sequence)) break;
132 DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence);
133 } if (is_bnf_rule_valid (rule)) return rule;
134
135revert:
136 DYNAMIC_ARRAY_FREE (rule.is_replaced_by);
137 lexer->cursor = checkpoint;
138 return (struct bnf_rule) {0};
139}
140
141struct bnf_syntax
142bnf_parse_syntax (struct bnf_lexer *lexer)
143{
144 struct bnf_syntax syntax = {0};
145
146 for (;;) {
147 while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE));
148 struct bnf_rule rule = bnf_parse_rule (lexer);
149 if (!is_bnf_rule_valid (rule)) {
150 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) {
151 print_error (lexer, lexer->cursor, "unable to parse rule");
152 }
153 break;
154 }
155
156 DYNAMIC_ARRAY_APPEND (syntax, rule);
157 }
158
159 return syntax;
160}
161
162bool
163bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax)
164{
165 bool no_errors = true;
166 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
167 DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) {
168 DYNAMIC_ARRAY_FOREACH (term, *sequence) {
169 if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue;
170 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) continue;
171 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) continue;
172
173 bool rule_found = false;
174 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
175 if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue;
176 rule_found = true;
177 break;
178 }
179
180 if (!rule_found) {
181 no_errors = false;
182 print_error (lexer, term->sv.beg,
183 "rule not found: %.*s",
184 (int) STRING_VIEW_LEN (term->sv), term->sv.beg);
185 }
186 }
187 }
188 }
189
190 return no_errors;
191}