#include #include "bnf-parser.h" #include "dynamic-array.h" void print_error (struct bnf_lexer *lexer, const char *point, const char *template, ...) { int row = 1; int col = 0; for (const char *head = lexer->sv.beg; head < point; ++head) { col += 1; if (*head == '\n') { row += 1; col = 0; } }; fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col); va_list ap; va_start (ap, template); vfprintf (stderr, template, ap); va_end (ap); putc ('\n', stderr); } bool is_bnf_lexeme_valid (struct bnf_lexeme lexeme) { return lexeme.kind != BNF_LEXEME_KIND_INVALID; } bool bnf_parse_lexeme (struct bnf_lexer *lexer, enum bnf_lexeme_kind kind) { const char *checkpoint = lexer->cursor; struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); if (lexeme.kind != kind) goto revert; return true; revert: lexer->cursor = checkpoint; return false; } struct bnf_lexeme bnf_parse_nonterminal (struct bnf_lexer *lexer) { const char *checkpoint = lexer->cursor; struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); if (lexeme.kind == BNF_LEXEME_KIND_NONTERMINAL) return lexeme; revert: lexer->cursor = checkpoint; return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; } struct bnf_lexeme bnf_parse_terminal (struct bnf_lexer *lexer) { const char *checkpoint = lexer->cursor; struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); if (lexeme.kind == BNF_LEXEME_KIND_TERMINAL) return lexeme; revert: lexer->cursor = checkpoint; return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; } uint32_t bnf_parse_probability (struct bnf_lexer *lexer) { const char *checkpoint = lexer->cursor; struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); if (lexeme.kind == BNF_LEXEME_KIND_PROBABILITY) { uint32_t probability = 0; while (lexeme.sv.beg != lexeme.sv.end) { probability *= 10; probability += *lexeme.sv.beg++ - '0'; } return probability; } revert: lexer->cursor = checkpoint; return 1; } bool is_bnf_sequence_valid (struct bnf_sequence sequence) { return sequence.term_count != 0; } struct bnf_sequence bnf_parse_sequence (struct bnf_lexer *lexer) { struct bnf_sequence sequence = {0}; const char *checkpoint = lexer->cursor; sequence.probability = bnf_parse_probability (lexer); for (;;) { struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer); if (!is_bnf_lexeme_valid (lexeme)) lexeme = bnf_parse_terminal (lexer); if (!is_bnf_lexeme_valid (lexeme)) break; DYNAMIC_ARRAY_APPEND (sequence, lexeme); } if (!is_bnf_sequence_valid (sequence)) goto revert; if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) || bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE)) return sequence; revert: DYNAMIC_ARRAY_FREE (sequence); lexer->cursor = checkpoint; return (struct bnf_sequence) {0}; } bool is_bnf_rule_valid (struct bnf_rule rule) { return rule.is_replaced_by.sequence_count != 0; } struct bnf_rule bnf_parse_rule (struct bnf_lexer *lexer) { struct bnf_rule rule = {0}; const char *checkpoint = lexer->cursor; rule.name = bnf_parse_nonterminal (lexer); if (!is_bnf_lexeme_valid (rule.name)) goto revert; if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY)) goto revert; for (;;) { struct bnf_sequence sequence = bnf_parse_sequence (lexer); if (!is_bnf_sequence_valid (sequence)) break; DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence); } if (is_bnf_rule_valid (rule)) return rule; revert: DYNAMIC_ARRAY_FREE (rule.is_replaced_by); lexer->cursor = checkpoint; return (struct bnf_rule) {0}; } struct bnf_syntax bnf_parse_syntax (struct bnf_lexer *lexer) { struct bnf_syntax syntax = {0}; for (;;) { while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE)); struct bnf_rule rule = bnf_parse_rule (lexer); if (!is_bnf_rule_valid (rule)) { if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) { print_error (lexer, lexer->cursor, "unable to parse rule"); } break; } DYNAMIC_ARRAY_APPEND (syntax, rule); } return syntax; } bool bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax) { bool no_errors = true; DYNAMIC_ARRAY_FOREACH (rule, syntax) { DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) { DYNAMIC_ARRAY_FOREACH (term, *sequence) { if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue; if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR (""))) continue; if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR (""))) continue; bool rule_found = false; DYNAMIC_ARRAY_FOREACH (rule, syntax) { if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue; rule_found = true; break; } if (!rule_found) { no_errors = false; print_error (lexer, term->sv.beg, "rule not found: %.*s", (int) STRING_VIEW_LEN (term->sv), term->sv.beg); } } } } return no_errors; }