aboutsummaryrefslogtreecommitdiff
path: root/src/bnf-parser.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 08:19:44 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 08:23:19 +0000
commit5be0e39d0ce97849d7722bd1ac98563c7014faf3 (patch)
tree9dba821664337a6662a6da88dfe86291d269c36b /src/bnf-parser.c
downloadlab-05-5be0e39d0ce97849d7722bd1ac98563c7014faf3.tar.gz
publishHEADmaster
Diffstat (limited to 'src/bnf-parser.c')
-rw-r--r--src/bnf-parser.c213
1 files changed, 213 insertions, 0 deletions
diff --git a/src/bnf-parser.c b/src/bnf-parser.c
new file mode 100644
index 0000000..ce14e43
--- /dev/null
+++ b/src/bnf-parser.c
@@ -0,0 +1,213 @@
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
78uint32_t
79bnf_parse_probability (struct bnf_lexer *lexer)
80{
81 const char *checkpoint = lexer->cursor;
82
83 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
84 if (lexeme.kind == BNF_LEXEME_KIND_PROBABILITY) {
85 uint32_t probability = 0;
86 while (lexeme.sv.beg != lexeme.sv.end) {
87 probability *= 10;
88 probability += *lexeme.sv.beg++ - '0';
89 }
90 return probability;
91 }
92
93revert:
94 lexer->cursor = checkpoint;
95 return 1;
96}
97
98bool
99is_bnf_sequence_valid (struct bnf_sequence sequence)
100{
101 return sequence.term_count != 0;
102}
103
104struct bnf_sequence
105bnf_parse_sequence (struct bnf_lexer *lexer)
106{
107 struct bnf_sequence sequence = {0};
108 const char *checkpoint = lexer->cursor;
109
110 sequence.probability = bnf_parse_probability (lexer);
111
112 for (;;) {
113 struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer);
114 if (!is_bnf_lexeme_valid (lexeme))
115 lexeme = bnf_parse_terminal (lexer);
116 if (!is_bnf_lexeme_valid (lexeme))
117 break;
118
119 DYNAMIC_ARRAY_APPEND (sequence, lexeme);
120 } if (!is_bnf_sequence_valid (sequence)) goto revert;
121
122 if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) ||
123 bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE))
124 return sequence;
125
126revert:
127 DYNAMIC_ARRAY_FREE (sequence);
128 lexer->cursor = checkpoint;
129 return (struct bnf_sequence) {0};
130}
131
132bool
133is_bnf_rule_valid (struct bnf_rule rule)
134{
135 return rule.is_replaced_by.sequence_count != 0;
136}
137
138struct bnf_rule
139bnf_parse_rule (struct bnf_lexer *lexer)
140{
141 struct bnf_rule rule = {0};
142 const char *checkpoint = lexer->cursor;
143
144 rule.name = bnf_parse_nonterminal (lexer);
145 if (!is_bnf_lexeme_valid (rule.name))
146 goto revert;
147
148 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY))
149 goto revert;
150
151 for (;;) {
152 struct bnf_sequence sequence = bnf_parse_sequence (lexer);
153 if (!is_bnf_sequence_valid (sequence)) break;
154 DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence);
155 } if (is_bnf_rule_valid (rule)) return rule;
156
157revert:
158 DYNAMIC_ARRAY_FREE (rule.is_replaced_by);
159 lexer->cursor = checkpoint;
160 return (struct bnf_rule) {0};
161}
162
163struct bnf_syntax
164bnf_parse_syntax (struct bnf_lexer *lexer)
165{
166 struct bnf_syntax syntax = {0};
167
168 for (;;) {
169 while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE));
170 struct bnf_rule rule = bnf_parse_rule (lexer);
171 if (!is_bnf_rule_valid (rule)) {
172 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) {
173 print_error (lexer, lexer->cursor, "unable to parse rule");
174 }
175 break;
176 }
177
178 DYNAMIC_ARRAY_APPEND (syntax, rule);
179 }
180
181 return syntax;
182}
183
184bool
185bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax)
186{
187 bool no_errors = true;
188 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
189 DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) {
190 DYNAMIC_ARRAY_FOREACH (term, *sequence) {
191 if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue;
192 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) continue;
193 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) continue;
194
195 bool rule_found = false;
196 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
197 if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue;
198 rule_found = true;
199 break;
200 }
201
202 if (!rule_found) {
203 no_errors = false;
204 print_error (lexer, term->sv.beg,
205 "rule not found: %.*s",
206 (int) STRING_VIEW_LEN (term->sv), term->sv.beg);
207 }
208 }
209 }
210 }
211
212 return no_errors;
213}