diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bnf-lexeme.c | 10 | ||||
| -rw-r--r-- | src/bnf-lexeme.h | 35 | ||||
| -rw-r--r-- | src/bnf-lexer.c | 101 | ||||
| -rw-r--r-- | src/bnf-lexer.h | 18 | ||||
| -rw-r--r-- | src/bnf-parser.c | 191 | ||||
| -rw-r--r-- | src/bnf-parser.h | 51 | ||||
| -rw-r--r-- | src/bnf-preprocessor.c | 76 | ||||
| -rw-r--r-- | src/bnf-preprocessor.h | 8 | ||||
| -rw-r--r-- | src/dynamic-array.h | 75 | ||||
| -rw-r--r-- | src/generate-grammar.c | 391 | ||||
| -rw-r--r-- | src/main.c | 1225 | ||||
| -rw-r--r-- | src/slovak-adjective.bnf | 97 | ||||
| -rw-r--r-- | src/slovak-adverb.bnf | 3 | ||||
| -rw-r--r-- | src/slovak-noun.bnf | 321 | ||||
| -rw-r--r-- | src/slovak-object.bnf | 56 | ||||
| -rw-r--r-- | src/slovak-sentence.bnf | 43 | ||||
| -rw-r--r-- | src/slovak-verb.bnf | 113 | ||||
| -rw-r--r-- | src/string-builder.c | 41 | ||||
| -rw-r--r-- | src/string-builder.h | 48 | ||||
| -rw-r--r-- | src/string-view.h | 17 | ||||
| -rw-r--r-- | src/utils.h | 37 |
21 files changed, 2957 insertions, 0 deletions
diff --git a/src/bnf-lexeme.c b/src/bnf-lexeme.c new file mode 100644 index 0000000..22ab39d --- /dev/null +++ b/src/bnf-lexeme.c | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #include "bnf-lexeme.h" | ||
| 2 | |||
| 3 | #define X(SYM, ...) [BNF_LEXEME_KIND_##SYM] = #SYM, | ||
| 4 | const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT] = { | ||
| 5 | BNF_LEXEME_KINDS () | ||
| 6 | [BNF_LEXEME_KIND_END_OF_LINE] = "END_OF_LINE", | ||
| 7 | [BNF_LEXEME_KIND_END_OF_FILE] = "END_OF_FILE", | ||
| 8 | [BNF_LEXEME_KIND_INVALID ] = "INVALID", | ||
| 9 | }; | ||
| 10 | #undef X | ||
diff --git a/src/bnf-lexeme.h b/src/bnf-lexeme.h new file mode 100644 index 0000000..20c532c --- /dev/null +++ b/src/bnf-lexeme.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #ifndef INCLUDED_BNF_LEXEME_H | ||
| 2 | #define INCLUDED_BNF_LEXEME_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "utils.h" | ||
| 7 | #include "string-view.h" | ||
| 8 | |||
| 9 | #define BNF_LEXEME_KINDS() \ | ||
| 10 | X (TERMINAL, "\"[^\"]*\"|'[^']*'") \ | ||
| 11 | X (NONTERMINAL, "<[^>]*>" ) \ | ||
| 12 | X (SYMBOL_ALTERNATIVE, "\\|" ) \ | ||
| 13 | X (SYMBOL_IS_REPLACED_BY, "::=" ) \ | ||
| 14 | X (COMMENT, "#[^\n]*" ) | ||
| 15 | |||
| 16 | #define X(SYM, ...) BNF_LEXEME_KIND_##SYM, | ||
| 17 | enum bnf_lexeme_kind { | ||
| 18 | BNF_LEXEME_KINDS () | ||
| 19 | BNF_LEXEME_KIND_END_OF_LINE, | ||
| 20 | BNF_LEXEME_KIND_END_OF_FILE, | ||
| 21 | BNF_LEXEME_KIND_INVALID, | ||
| 22 | BNF_LEXEME_KIND_COUNT, | ||
| 23 | }; | ||
| 24 | #undef X | ||
| 25 | |||
| 26 | #define BNF_LEXEME_REGEX_COUNT (BNF_LEXEME_KIND_COUNT - 3) | ||
| 27 | |||
| 28 | struct bnf_lexeme { | ||
| 29 | struct string_view sv; | ||
| 30 | enum bnf_lexeme_kind kind; | ||
| 31 | }; | ||
| 32 | |||
| 33 | extern const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT]; | ||
| 34 | |||
| 35 | #endif // INCLUDED_BNF_LEXEME_H | ||
diff --git a/src/bnf-lexer.c b/src/bnf-lexer.c new file mode 100644 index 0000000..f859af9 --- /dev/null +++ b/src/bnf-lexer.c | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #include "bnf-lexer.h" | ||
| 2 | #include <regex.h> | ||
| 3 | #include <assert.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <ctype.h> | ||
| 7 | #include "dynamic-array.h" | ||
| 8 | |||
| 9 | #define X(SYM, REG, ...) [BNF_LEXEME_KIND_##SYM] = "^(" REG ")", | ||
| 10 | static const char *lexeme_regex_strings[BNF_LEXEME_REGEX_COUNT] = { BNF_LEXEME_KINDS () }; | ||
| 11 | #undef X | ||
| 12 | |||
| 13 | static regex_t lexeme_regexes[BNF_LEXEME_REGEX_COUNT]; | ||
| 14 | |||
| 15 | static void | ||
| 16 | print_regex_error (const regex_t *comp, const char *regex, int errcode) | ||
| 17 | { | ||
| 18 | size_t length = regerror (errcode, comp, NULL, 0); | ||
| 19 | char *buffer = malloc (length); | ||
| 20 | regerror (errcode, comp, buffer, length); | ||
| 21 | fprintf (stderr, "error: regcomp (\"%s\"): %s\n", | ||
| 22 | regex, buffer); | ||
| 23 | free (buffer); | ||
| 24 | } | ||
| 25 | |||
| 26 | static void __attribute__((constructor)) | ||
| 27 | compile_regexes (void) | ||
| 28 | { | ||
| 29 | for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) { | ||
| 30 | regex_t *compiled = &lexeme_regexes[kind]; | ||
| 31 | const char *pattern = lexeme_regex_strings[kind]; | ||
| 32 | int errcode = regcomp (compiled, pattern, REG_EXTENDED); | ||
| 33 | if (errcode) { | ||
| 34 | print_regex_error (compiled, pattern, errcode); | ||
| 35 | exit (1); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | struct bnf_lexer | ||
| 41 | bnf_lexer_from_string_view (struct string_view sv, const char *filename) | ||
| 42 | { | ||
| 43 | return (struct bnf_lexer) { | ||
| 44 | .sv = sv, | ||
| 45 | .cursor = sv.beg, | ||
| 46 | .filename = filename, | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | static void | ||
| 51 | lexer_skip_whitespace (struct bnf_lexer *lexer) | ||
| 52 | { | ||
| 53 | while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' || | ||
| 54 | *lexer->cursor == '\t')) | ||
| 55 | lexer->cursor += 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | struct bnf_lexeme | ||
| 59 | bnf_lexer_next_lexeme (struct bnf_lexer *lexer) | ||
| 60 | { | ||
| 61 | lexer_skip_whitespace (lexer); | ||
| 62 | struct bnf_lexeme lexeme = { | ||
| 63 | .sv.beg = lexer->cursor, | ||
| 64 | .kind = BNF_LEXEME_KIND_INVALID, | ||
| 65 | }; | ||
| 66 | |||
| 67 | if (lexer->cursor >= lexer->sv.end) { | ||
| 68 | lexeme.kind = BNF_LEXEME_KIND_END_OF_FILE; | ||
| 69 | lexeme.sv.end = lexer->sv.end; | ||
| 70 | return lexeme; | ||
| 71 | } | ||
| 72 | |||
| 73 | if (*lexer->cursor == '\n') { | ||
| 74 | lexeme.kind = BNF_LEXEME_KIND_END_OF_LINE; | ||
| 75 | lexeme.sv.end = ++lexer->cursor; | ||
| 76 | return lexeme; | ||
| 77 | } | ||
| 78 | |||
| 79 | regmatch_t regmatch; | ||
| 80 | enum bnf_lexeme_kind longest_matching_lexeme_kind = BNF_LEXEME_KIND_INVALID; | ||
| 81 | int longest_matching_lexeme_kind_length = 0; | ||
| 82 | for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) { | ||
| 83 | if (regexec (&lexeme_regexes[kind], lexer->cursor, 1, ®match, 0)) continue; | ||
| 84 | if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue; | ||
| 85 | longest_matching_lexeme_kind_length = regmatch.rm_eo; | ||
| 86 | longest_matching_lexeme_kind = kind; | ||
| 87 | } | ||
| 88 | |||
| 89 | if (longest_matching_lexeme_kind != BNF_LEXEME_KIND_INVALID) { | ||
| 90 | lexeme.kind = longest_matching_lexeme_kind; | ||
| 91 | lexeme.sv.end = lexer->cursor += longest_matching_lexeme_kind_length; | ||
| 92 | |||
| 93 | switch (lexeme.kind) { | ||
| 94 | case BNF_LEXEME_KIND_COMMENT: return bnf_lexer_next_lexeme (lexer); | ||
| 95 | default: return lexeme; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | lexeme.sv.end = ++lexer->cursor; | ||
| 100 | return lexeme; | ||
| 101 | } | ||
diff --git a/src/bnf-lexer.h b/src/bnf-lexer.h new file mode 100644 index 0000000..5863219 --- /dev/null +++ b/src/bnf-lexer.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef INCLUDED_BNF_LEXER_H | ||
| 2 | #define INCLUDED_BNF_LEXER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include "utils.h" | ||
| 6 | #include "bnf-lexeme.h" | ||
| 7 | |||
| 8 | struct bnf_lexer { | ||
| 9 | struct string_view sv; | ||
| 10 | const char *cursor; | ||
| 11 | const char *filename; | ||
| 12 | }; | ||
| 13 | |||
| 14 | extern struct bnf_lexer bnf_lexer_from_string_view (struct string_view, const char *filename); | ||
| 15 | extern struct bnf_lexer bnf_lexer_from_filename (const char *); | ||
| 16 | extern struct bnf_lexeme bnf_lexer_next_lexeme (struct bnf_lexer *); | ||
| 17 | |||
| 18 | #endif // INCLUDED_BNF_LEXER_H | ||
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 | |||
| 5 | void | ||
| 6 | print_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 | |||
| 30 | bool | ||
| 31 | is_bnf_lexeme_valid (struct bnf_lexeme lexeme) | ||
| 32 | { | ||
| 33 | return lexeme.kind != BNF_LEXEME_KIND_INVALID; | ||
| 34 | } | ||
| 35 | |||
| 36 | bool | ||
| 37 | bnf_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 | |||
| 46 | revert: | ||
| 47 | lexer->cursor = checkpoint; | ||
| 48 | return false; | ||
| 49 | } | ||
| 50 | |||
| 51 | struct bnf_lexeme | ||
| 52 | bnf_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 | |||
| 59 | revert: | ||
| 60 | lexer->cursor = checkpoint; | ||
| 61 | return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; | ||
| 62 | } | ||
| 63 | |||
| 64 | struct bnf_lexeme | ||
| 65 | bnf_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 | |||
| 73 | revert: | ||
| 74 | lexer->cursor = checkpoint; | ||
| 75 | return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; | ||
| 76 | } | ||
| 77 | |||
| 78 | bool | ||
| 79 | is_bnf_sequence_valid (struct bnf_sequence sequence) | ||
| 80 | { | ||
| 81 | return sequence.term_count != 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | struct bnf_sequence | ||
| 85 | bnf_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 | |||
| 104 | revert: | ||
| 105 | DYNAMIC_ARRAY_FREE (sequence); | ||
| 106 | lexer->cursor = checkpoint; | ||
| 107 | return (struct bnf_sequence) {0}; | ||
| 108 | } | ||
| 109 | |||
| 110 | bool | ||
| 111 | is_bnf_rule_valid (struct bnf_rule rule) | ||
| 112 | { | ||
| 113 | return rule.is_replaced_by.sequence_count != 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | struct bnf_rule | ||
| 117 | bnf_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 | |||
| 135 | revert: | ||
| 136 | DYNAMIC_ARRAY_FREE (rule.is_replaced_by); | ||
| 137 | lexer->cursor = checkpoint; | ||
| 138 | return (struct bnf_rule) {0}; | ||
| 139 | } | ||
| 140 | |||
| 141 | struct bnf_syntax | ||
| 142 | bnf_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 | |||
| 162 | bool | ||
| 163 | bnf_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 | } | ||
diff --git a/src/bnf-parser.h b/src/bnf-parser.h new file mode 100644 index 0000000..db9804d --- /dev/null +++ b/src/bnf-parser.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #ifndef INCLUDED_BNF_PARSER_H | ||
| 2 | #define INCLUDED_BNF_PARSER_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include "bnf-lexer.h" | ||
| 7 | |||
| 8 | struct bnf_sequence { | ||
| 9 | union { struct bnf_lexeme *items, *terms; }; | ||
| 10 | union { uint32_t count, term_count; }; | ||
| 11 | uint32_t capacity; | ||
| 12 | }; | ||
| 13 | |||
| 14 | struct bnf_rule { | ||
| 15 | struct bnf_lexeme name; | ||
| 16 | struct { | ||
| 17 | union { struct bnf_sequence *items, *sequences; }; | ||
| 18 | union { uint32_t count, sequence_count; }; | ||
| 19 | uint32_t capacity; | ||
| 20 | } is_replaced_by; | ||
| 21 | }; | ||
| 22 | |||
| 23 | struct bnf_syntax { | ||
| 24 | union { struct bnf_rule *items, *rules; }; | ||
| 25 | union { uint32_t count, rule_count; }; | ||
| 26 | uint32_t capacity; | ||
| 27 | }; | ||
| 28 | |||
| 29 | // struct term_table { | ||
| 30 | // union { struct bnf_lexeme *items, *terms; }; | ||
| 31 | // union { uint32_t count, term_count; }; | ||
| 32 | // uint32_t capacity; | ||
| 33 | // }; | ||
| 34 | |||
| 35 | struct bnf_parser { | ||
| 36 | struct bnf_lexer lexer; | ||
| 37 | }; | ||
| 38 | |||
| 39 | bool is_bnf_lexeme_valid (struct bnf_lexeme); | ||
| 40 | bool is_bnf_sequence_valid (struct bnf_sequence); | ||
| 41 | bool is_bnf_rule_valid (struct bnf_rule); | ||
| 42 | |||
| 43 | bool bnf_check_syntax (struct bnf_lexer *, struct bnf_syntax); | ||
| 44 | bool bnf_parse_lexeme (struct bnf_lexer *, enum bnf_lexeme_kind); | ||
| 45 | struct bnf_lexeme bnf_parse_nonterminal (struct bnf_lexer *); | ||
| 46 | struct bnf_lexeme bnf_parse_terminal (struct bnf_lexer *); | ||
| 47 | struct bnf_sequence bnf_parse_sequence (struct bnf_lexer *); | ||
| 48 | struct bnf_rule bnf_parse_rule (struct bnf_lexer *); | ||
| 49 | struct bnf_syntax bnf_parse_syntax (struct bnf_lexer *); | ||
| 50 | |||
| 51 | #endif // INCLUDED_BNF_PARSER_H | ||
diff --git a/src/bnf-preprocessor.c b/src/bnf-preprocessor.c new file mode 100644 index 0000000..67fafc7 --- /dev/null +++ b/src/bnf-preprocessor.c | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | #include "bnf-preprocessor.h" | ||
| 2 | #include "string-builder.h" | ||
| 3 | #include <errno.h> | ||
| 4 | #include <fcntl.h> | ||
| 5 | #include <unistd.h> | ||
| 6 | #include <sys/stat.h> | ||
| 7 | #include <sys/mman.h> | ||
| 8 | |||
| 9 | struct preprocessor { | ||
| 10 | struct { | ||
| 11 | struct open_file { | ||
| 12 | struct string_view sv; | ||
| 13 | const char *filename; | ||
| 14 | const char *cursor; | ||
| 15 | } *items; | ||
| 16 | uint32_t count; | ||
| 17 | uint32_t capacity; | ||
| 18 | } open_files; | ||
| 19 | struct string_builder sb; | ||
| 20 | }; | ||
| 21 | |||
| 22 | static const char import_cstr[] = "import"; | ||
| 23 | static struct string_view import_sv = { | ||
| 24 | .beg = import_cstr, | ||
| 25 | .end = import_cstr + sizeof (import_cstr) - 1, | ||
| 26 | }; | ||
| 27 | |||
| 28 | |||
| 29 | static void | ||
| 30 | preprocess_file (struct string_builder *sb, const char *filename) | ||
| 31 | { | ||
| 32 | struct string_view file_contents; | ||
| 33 | const char *cursor; | ||
| 34 | |||
| 35 | int fd = open (filename, O_RDONLY); | ||
| 36 | if (fd == -1) DIE ("error: %s: %s", strerror (errno), filename); | ||
| 37 | |||
| 38 | struct stat st; | ||
| 39 | fstat (fd, &st); | ||
| 40 | cursor = file_contents.beg = | ||
| 41 | mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| 42 | close (fd); | ||
| 43 | file_contents.end = file_contents.beg + st.st_size; | ||
| 44 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, STRING_VIEW_LEN (file_contents)); | ||
| 45 | |||
| 46 | do { | ||
| 47 | if (*cursor == '!') { | ||
| 48 | cursor += 1; | ||
| 49 | while (*cursor == ' ') cursor += 1; | ||
| 50 | if () | ||
| 51 | while (*cursor != '\n') cursor += 1; | ||
| 52 | cursor += 1; | ||
| 53 | preprocess_file (sb, import_filename); | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | |||
| 57 | DYNAMIC_ARRAY_APPEND_FAST (*sb, *cursor++); | ||
| 58 | } while (1); | ||
| 59 | |||
| 60 | munmap ((char *) file_contents.beg, STRING_VIEW_LEN (file_contents)); | ||
| 61 | } | ||
| 62 | |||
| 63 | struct string_view | ||
| 64 | bnf_preprocess_file (const char *filename) | ||
| 65 | { | ||
| 66 | struct string_builder sb = {0}; | ||
| 67 | |||
| 68 | preprocess_file (&sb, filename); | ||
| 69 | |||
| 70 | struct string_view output = { | ||
| 71 | .beg = sb.string, | ||
| 72 | .end = sb.string + sb.count, | ||
| 73 | }; | ||
| 74 | |||
| 75 | return output; | ||
| 76 | } | ||
diff --git a/src/bnf-preprocessor.h b/src/bnf-preprocessor.h new file mode 100644 index 0000000..6181137 --- /dev/null +++ b/src/bnf-preprocessor.h | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #ifndef INCLUDED_BNF_PREPROCESSOR_H | ||
| 2 | #define INCLUDED_BNF_PREPROCESSOR_H | ||
| 3 | |||
| 4 | #include "string-view.h" | ||
| 5 | |||
| 6 | struct string_view bnf_preprocess_file (const char *filename); | ||
| 7 | |||
| 8 | #endif // INCLUDED_BNF_PREPROCESSOR_H | ||
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..1329995 --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DA, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DA).count + (COUNT) > (DA).capacity) { \ | ||
| 13 | if ((DA).capacity == 0) \ | ||
| 14 | (DA).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DA).count + (COUNT) > (DA).capacity) \ | ||
| 16 | (DA).capacity *= 2; \ | ||
| 17 | (DA).items = REALLOC((DA).items, \ | ||
| 18 | (DA).capacity * \ | ||
| 19 | sizeof (*(DA).items)); \ | ||
| 20 | ASSERT((DA).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DA) \ | ||
| 25 | ({ \ | ||
| 26 | (DA).capacity = (DA).count; \ | ||
| 27 | (DA).items = REALLOC((DA).items, \ | ||
| 28 | (DA).capacity * \ | ||
| 29 | sizeof (*(DA).items)); \ | ||
| 30 | ASSERT((DA).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DA, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), 1); \ | ||
| 37 | (DA).items[(DA).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_FAST(DA, ITEM) \ | ||
| 41 | (DA).items[(DA).count++] = (ITEM); | ||
| 42 | |||
| 43 | #define DYNAMIC_ARRAY_APPEND_MANY(DA, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 44 | ({ \ | ||
| 45 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), (NEW_ITEMS_COUNT)); \ | ||
| 46 | memcpy ((DA).items + (DA).count, (NEW_ITEMS), \ | ||
| 47 | (NEW_ITEMS_COUNT)*sizeof (*(DA).items)); \ | ||
| 48 | (DA).count += (NEW_ITEMS_COUNT); \ | ||
| 49 | }) | ||
| 50 | |||
| 51 | #define DYNAMIC_ARRAY_APPEND_DYNAMIC_ARRAY(TO, FROM) \ | ||
| 52 | DYNAMIC_ARRAY_APPEND_MANY ((TO), \ | ||
| 53 | (FROM).items, \ | ||
| 54 | (FROM).count) | ||
| 55 | |||
| 56 | #define DYNAMIC_ARRAY_LAST(DA) \ | ||
| 57 | (DA).items[(DA).count - 1] | ||
| 58 | |||
| 59 | #define DYNAMIC_ARRAY_POP(DA) \ | ||
| 60 | (DA).items[--(DA).count] | ||
| 61 | |||
| 62 | #define DYNAMIC_ARRAY_RESET(DA) ({ (DA).count = 0; }) | ||
| 63 | |||
| 64 | #define DYNAMIC_ARRAY_FREE(DA) FREE((DA).items) | ||
| 65 | |||
| 66 | // NOTE(cmmm): resizing will invalidate! | ||
| 67 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DA) for ( \ | ||
| 68 | typeof ((DA).items) ITEM = (DA).items; \ | ||
| 69 | ITEM < (DA).items + (DA).count; ++ITEM) | ||
| 70 | |||
| 71 | #define DYNAMIC_ARRAY_FOREACH_REVERSED(ITEM, DA) for ( \ | ||
| 72 | typeof ((DA).items) ITEM = &DYNAMIC_ARRAY_LAST (DA); \ | ||
| 73 | ITEM >= (DA).items; --ITEM) | ||
| 74 | |||
| 75 | #endif // INCLUDED_DYNAMIC_ARRAY_H | ||
diff --git a/src/generate-grammar.c b/src/generate-grammar.c new file mode 100644 index 0000000..d4d0132 --- /dev/null +++ b/src/generate-grammar.c | |||
| @@ -0,0 +1,391 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | int | ||
| 5 | main (int argc, char *argv[]) | ||
| 6 | { | ||
| 7 | FILE *fp = stdout; | ||
| 8 | |||
| 9 | verb { | ||
| 10 | imperfective { | ||
| 11 | indicative { | ||
| 12 | present { | ||
| 13 | sg { | ||
| 14 | p1; | ||
| 15 | p2; | ||
| 16 | p3; | ||
| 17 | }; | ||
| 18 | pl { | ||
| 19 | p1; | ||
| 20 | p2; | ||
| 21 | p3; | ||
| 22 | }; | ||
| 23 | }; | ||
| 24 | past { | ||
| 25 | masculine { | ||
| 26 | sg { | ||
| 27 | p1; | ||
| 28 | p2; | ||
| 29 | p3; | ||
| 30 | }; | ||
| 31 | pl { | ||
| 32 | p1 { | ||
| 33 | animate; | ||
| 34 | inanimate; | ||
| 35 | }; | ||
| 36 | p2 { | ||
| 37 | animate; | ||
| 38 | inanimate; | ||
| 39 | }; | ||
| 40 | p3 { | ||
| 41 | animate; | ||
| 42 | inanimate; | ||
| 43 | }; | ||
| 44 | }; | ||
| 45 | }; | ||
| 46 | feminine { | ||
| 47 | sg { | ||
| 48 | p1; | ||
| 49 | p2; | ||
| 50 | p3; | ||
| 51 | }; | ||
| 52 | pl { | ||
| 53 | p1; | ||
| 54 | p2; | ||
| 55 | p3; | ||
| 56 | }; | ||
| 57 | }; | ||
| 58 | neuter { | ||
| 59 | sg { | ||
| 60 | p1; | ||
| 61 | p2; | ||
| 62 | p3; | ||
| 63 | }; | ||
| 64 | pl { | ||
| 65 | p1; | ||
| 66 | p2; | ||
| 67 | p3; | ||
| 68 | }; | ||
| 69 | }; | ||
| 70 | }; | ||
| 71 | future { | ||
| 72 | sg { | ||
| 73 | p1; | ||
| 74 | p2; | ||
| 75 | p3; | ||
| 76 | }; | ||
| 77 | pl { | ||
| 78 | p1; | ||
| 79 | p2; | ||
| 80 | p3; | ||
| 81 | }; | ||
| 82 | }; | ||
| 83 | }; | ||
| 84 | conditional { | ||
| 85 | present { | ||
| 86 | masculine { | ||
| 87 | sg { | ||
| 88 | p1; | ||
| 89 | p2; | ||
| 90 | p3; | ||
| 91 | }; | ||
| 92 | pl { | ||
| 93 | p1 { | ||
| 94 | animate; | ||
| 95 | inanimate; | ||
| 96 | }; | ||
| 97 | p2 { | ||
| 98 | animate; | ||
| 99 | inanimate; | ||
| 100 | }; | ||
| 101 | p3 { | ||
| 102 | animate; | ||
| 103 | inanimate; | ||
| 104 | }; | ||
| 105 | }; | ||
| 106 | }; | ||
| 107 | feminine { | ||
| 108 | sg { | ||
| 109 | p1; | ||
| 110 | p2; | ||
| 111 | p3; | ||
| 112 | }; | ||
| 113 | pl { | ||
| 114 | p1; | ||
| 115 | p2; | ||
| 116 | p3; | ||
| 117 | }; | ||
| 118 | }; | ||
| 119 | neuter { | ||
| 120 | sg { | ||
| 121 | p1; | ||
| 122 | p2; | ||
| 123 | p3; | ||
| 124 | }; | ||
| 125 | pl { | ||
| 126 | p1; | ||
| 127 | p2; | ||
| 128 | p3; | ||
| 129 | }; | ||
| 130 | }; | ||
| 131 | }; | ||
| 132 | past { | ||
| 133 | masculine { | ||
| 134 | sg { | ||
| 135 | p1; | ||
| 136 | p2; | ||
| 137 | p3; | ||
| 138 | }; | ||
| 139 | pl { | ||
| 140 | p1 { | ||
| 141 | animate; | ||
| 142 | inanimate; | ||
| 143 | }; | ||
| 144 | p2 { | ||
| 145 | animate; | ||
| 146 | inanimate; | ||
| 147 | }; | ||
| 148 | p3 { | ||
| 149 | animate; | ||
| 150 | inanimate; | ||
| 151 | }; | ||
| 152 | }; | ||
| 153 | }; | ||
| 154 | feminine { | ||
| 155 | sg { | ||
| 156 | p1; | ||
| 157 | p2; | ||
| 158 | p3; | ||
| 159 | }; | ||
| 160 | pl { | ||
| 161 | p1; | ||
| 162 | p2; | ||
| 163 | p3; | ||
| 164 | }; | ||
| 165 | }; | ||
| 166 | neuter { | ||
| 167 | sg { | ||
| 168 | p1; | ||
| 169 | p2; | ||
| 170 | p3; | ||
| 171 | }; | ||
| 172 | pl { | ||
| 173 | p1; | ||
| 174 | p2; | ||
| 175 | p3; | ||
| 176 | }; | ||
| 177 | }; | ||
| 178 | }; | ||
| 179 | }; | ||
| 180 | imperative { | ||
| 181 | sg { | ||
| 182 | p2; | ||
| 183 | }; | ||
| 184 | pl { | ||
| 185 | p1; | ||
| 186 | p2; | ||
| 187 | }; | ||
| 188 | }; | ||
| 189 | participle { | ||
| 190 | sg { | ||
| 191 | masculine; | ||
| 192 | feminine; | ||
| 193 | neuter; | ||
| 194 | }; | ||
| 195 | pl { | ||
| 196 | masculine { | ||
| 197 | animate; | ||
| 198 | inanimate; | ||
| 199 | }; | ||
| 200 | feminine; | ||
| 201 | neuter; | ||
| 202 | }; | ||
| 203 | }; | ||
| 204 | }; | ||
| 205 | perfective { | ||
| 206 | indicative { | ||
| 207 | present { | ||
| 208 | sg { | ||
| 209 | p1; | ||
| 210 | p2; | ||
| 211 | p3; | ||
| 212 | }; | ||
| 213 | pl { | ||
| 214 | p1; | ||
| 215 | p2; | ||
| 216 | p3; | ||
| 217 | }; | ||
| 218 | }; | ||
| 219 | past { | ||
| 220 | masculine { | ||
| 221 | sg { | ||
| 222 | p1; | ||
| 223 | p2; | ||
| 224 | p3; | ||
| 225 | }; | ||
| 226 | pl { | ||
| 227 | p1 { | ||
| 228 | animate; | ||
| 229 | inanimate; | ||
| 230 | }; | ||
| 231 | p2 { | ||
| 232 | animate; | ||
| 233 | inanimate; | ||
| 234 | }; | ||
| 235 | p3 { | ||
| 236 | animate; | ||
| 237 | inanimate; | ||
| 238 | }; | ||
| 239 | }; | ||
| 240 | }; | ||
| 241 | feminine { | ||
| 242 | sg { | ||
| 243 | p1; | ||
| 244 | p2; | ||
| 245 | p3; | ||
| 246 | }; | ||
| 247 | pl { | ||
| 248 | p1; | ||
| 249 | p2; | ||
| 250 | p3; | ||
| 251 | }; | ||
| 252 | }; | ||
| 253 | neuter { | ||
| 254 | sg { | ||
| 255 | p1; | ||
| 256 | p2; | ||
| 257 | p3; | ||
| 258 | }; | ||
| 259 | pl { | ||
| 260 | p1; | ||
| 261 | p2; | ||
| 262 | p3; | ||
| 263 | }; | ||
| 264 | }; | ||
| 265 | }; | ||
| 266 | }; | ||
| 267 | conditional { | ||
| 268 | present { | ||
| 269 | masculine { | ||
| 270 | sg { | ||
| 271 | p1; | ||
| 272 | p2; | ||
| 273 | p3; | ||
| 274 | }; | ||
| 275 | pl { | ||
| 276 | p1 { | ||
| 277 | animate; | ||
| 278 | inanimate; | ||
| 279 | }; | ||
| 280 | p2 { | ||
| 281 | animate; | ||
| 282 | inanimate; | ||
| 283 | }; | ||
| 284 | p3 { | ||
| 285 | animate; | ||
| 286 | inanimate; | ||
| 287 | }; | ||
| 288 | }; | ||
| 289 | }; | ||
| 290 | feminine { | ||
| 291 | sg { | ||
| 292 | p1; | ||
| 293 | p2; | ||
| 294 | p3; | ||
| 295 | }; | ||
| 296 | pl { | ||
| 297 | p1; | ||
| 298 | p2; | ||
| 299 | p3; | ||
| 300 | }; | ||
| 301 | }; | ||
| 302 | neuter { | ||
| 303 | sg { | ||
| 304 | p1; | ||
| 305 | p2; | ||
| 306 | p3; | ||
| 307 | }; | ||
| 308 | pl { | ||
| 309 | p1; | ||
| 310 | p2; | ||
| 311 | p3; | ||
| 312 | }; | ||
| 313 | }; | ||
| 314 | }; | ||
| 315 | past { | ||
| 316 | masculine { | ||
| 317 | sg { | ||
| 318 | p1; | ||
| 319 | p2; | ||
| 320 | p3; | ||
| 321 | }; | ||
| 322 | pl { | ||
| 323 | p1 { | ||
| 324 | animate; | ||
| 325 | inanimate; | ||
| 326 | }; | ||
| 327 | p2 { | ||
| 328 | animate; | ||
| 329 | inanimate; | ||
| 330 | }; | ||
| 331 | p3 { | ||
| 332 | animate; | ||
| 333 | inanimate; | ||
| 334 | }; | ||
| 335 | }; | ||
| 336 | }; | ||
| 337 | feminine { | ||
| 338 | sg { | ||
| 339 | p1; | ||
| 340 | p2; | ||
| 341 | p3; | ||
| 342 | }; | ||
| 343 | pl { | ||
| 344 | p1; | ||
| 345 | p2; | ||
| 346 | p3; | ||
| 347 | }; | ||
| 348 | }; | ||
| 349 | neuter { | ||
| 350 | sg { | ||
| 351 | p1; | ||
| 352 | p2; | ||
| 353 | p3; | ||
| 354 | }; | ||
| 355 | pl { | ||
| 356 | p1; | ||
| 357 | p2; | ||
| 358 | p3; | ||
| 359 | }; | ||
| 360 | }; | ||
| 361 | }; | ||
| 362 | }; | ||
| 363 | imperative { | ||
| 364 | sg { | ||
| 365 | p2; | ||
| 366 | }; | ||
| 367 | pl { | ||
| 368 | p1; | ||
| 369 | p2; | ||
| 370 | }; | ||
| 371 | }; | ||
| 372 | participle { | ||
| 373 | sg { | ||
| 374 | masculine; | ||
| 375 | feminine; | ||
| 376 | neuter; | ||
| 377 | }; | ||
| 378 | pl { | ||
| 379 | masculine { | ||
| 380 | animate; | ||
| 381 | inanimate; | ||
| 382 | }; | ||
| 383 | feminine; | ||
| 384 | neuter; | ||
| 385 | }; | ||
| 386 | }; | ||
| 387 | }; | ||
| 388 | }; | ||
| 389 | |||
| 390 | return EXIT_SUCCESS; | ||
| 391 | } | ||
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b17fbbc --- /dev/null +++ b/src/main.c | |||
| @@ -0,0 +1,1225 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <assert.h> | ||
| 3 | #include <errno.h> | ||
| 4 | #include <time.h> | ||
| 5 | #include <ctype.h> | ||
| 6 | #include <fcntl.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include <sys/stat.h> | ||
| 9 | #include <sys/mman.h> | ||
| 10 | #include "bnf-parser.h" | ||
| 11 | #include "utils.h" | ||
| 12 | #include "dynamic-array.h" | ||
| 13 | #include "string-builder.h" | ||
| 14 | #include "string-view.h" | ||
| 15 | |||
| 16 | #define TERMINAL_COUNT (256 + 2) // ASCII + <EOL> + <EOF> | ||
| 17 | |||
| 18 | struct string_builder sb = {0}; | ||
| 19 | |||
| 20 | static struct string_view | ||
| 21 | load_file (const char *filename) | ||
| 22 | { | ||
| 23 | struct string_view file_contents; | ||
| 24 | |||
| 25 | int fd = open (filename, O_RDONLY); | ||
| 26 | if (fd == -1) DIE ("error: %s: %s", strerror (errno), filename); | ||
| 27 | |||
| 28 | struct stat st; | ||
| 29 | fstat (fd, &st); | ||
| 30 | file_contents.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| 31 | close (fd); | ||
| 32 | file_contents.end = file_contents.beg + st.st_size; | ||
| 33 | |||
| 34 | return file_contents; | ||
| 35 | } | ||
| 36 | |||
| 37 | static void | ||
| 38 | unload_file (struct string_view sv) | ||
| 39 | { | ||
| 40 | munmap ((char *) sv.beg, STRING_VIEW_LEN (sv)); | ||
| 41 | } | ||
| 42 | |||
| 43 | static void print_term (struct bnf_syntax syntax, | ||
| 44 | struct string_builder *sb, | ||
| 45 | struct bnf_lexeme term); | ||
| 46 | |||
| 47 | static void | ||
| 48 | print_nonterminal (struct bnf_syntax syntax, | ||
| 49 | struct string_builder *sb, | ||
| 50 | struct string_view sv) | ||
| 51 | { | ||
| 52 | DYNAMIC_ARRAY_FOREACH (rule, syntax) { | ||
| 53 | if (!STRING_VIEW_EQUALS (rule->name.sv, sv)) continue; | ||
| 54 | |||
| 55 | struct bnf_sequence sequence = | ||
| 56 | rule->is_replaced_by.items[rand () % rule->is_replaced_by.count]; | ||
| 57 | DYNAMIC_ARRAY_FOREACH (term, sequence) { | ||
| 58 | print_term (syntax, sb, *term); | ||
| 59 | } | ||
| 60 | |||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | printf ("%.*s\n", (int) STRING_VIEW_LEN (sv), sv.beg); | ||
| 65 | UNREACHABLE (); | ||
| 66 | } | ||
| 67 | |||
| 68 | static void | ||
| 69 | print_terminal (struct string_view sv, struct string_builder *sb) | ||
| 70 | { | ||
| 71 | sv.beg += 1; | ||
| 72 | sv.end -= 1; | ||
| 73 | STRING_BUILDER_APPEND_STRING_VIEW (*sb, sv); | ||
| 74 | } | ||
| 75 | |||
| 76 | static void | ||
| 77 | print_term (struct bnf_syntax syntax, struct string_builder *sb, struct bnf_lexeme term) | ||
| 78 | { | ||
| 79 | switch (term.kind) { | ||
| 80 | case BNF_LEXEME_KIND_TERMINAL: | ||
| 81 | print_terminal (term.sv, sb); | ||
| 82 | break; | ||
| 83 | case BNF_LEXEME_KIND_NONTERMINAL: | ||
| 84 | if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) { | ||
| 85 | printf ("\n"); | ||
| 86 | } else if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) { | ||
| 87 | } else { | ||
| 88 | print_nonterminal (syntax, sb, term.sv); | ||
| 89 | } break; | ||
| 90 | default: UNREACHABLE (); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | static void | ||
| 95 | print_random_rule (struct bnf_syntax syntax, const char *rule) | ||
| 96 | { | ||
| 97 | STRING_BUILDER_RESET (sb); | ||
| 98 | print_nonterminal (syntax, &sb, STRING_VIEW_FROM_CSTR (rule)); | ||
| 99 | sb.string[0] = toupper (sb.string[0]); | ||
| 100 | STRING_BUILDER_APPEND_NULL (sb); | ||
| 101 | printf ("%s", sb.string); | ||
| 102 | } | ||
| 103 | |||
| 104 | static void | ||
| 105 | save_dot (struct bnf_syntax syntax) | ||
| 106 | { | ||
| 107 | FILE *fp = fopen ("./tmp/slovak.dot", "w"); | ||
| 108 | |||
| 109 | fprintf (fp, "digraph slovak {" "\n"); { | ||
| 110 | fprintf (fp, "\t" "fontname=\"Aporetic Serif,serif\"" "\n"); | ||
| 111 | fprintf (fp, "\t" "node [fontname=\"Aporetic Sans,sans-serif\"]" "\n"); | ||
| 112 | fprintf (fp, "\t" "edge [fontname=\"Aporetic Serif Mono,mono\"]" "\n"); | ||
| 113 | fprintf (fp, "\t" "rankdir=TD" "\n"); | ||
| 114 | DYNAMIC_ARRAY_FOREACH (rule, syntax) { | ||
| 115 | fprintf (fp, "\t" "rule_%ld [label=\"%.*s\"]" "\n", | ||
| 116 | rule - syntax.items, | ||
| 117 | (int) STRING_VIEW_LEN (rule->name.sv), rule->name.sv.beg); | ||
| 118 | DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) { | ||
| 119 | fprintf (fp, "\t" "sequence_%ld_%ld [label=\"%ld\"]" "\n", | ||
| 120 | rule - syntax.items, | ||
| 121 | sequence - rule->is_replaced_by.items, | ||
| 122 | sequence - rule->is_replaced_by.items); | ||
| 123 | fprintf (fp, "\t" "rule_%ld -> sequence_%ld_%ld" "\n", | ||
| 124 | rule - syntax.items, | ||
| 125 | rule - syntax.items, | ||
| 126 | sequence - rule->is_replaced_by.items); | ||
| 127 | DYNAMIC_ARRAY_FOREACH (term, *sequence) { | ||
| 128 | fprintf (fp, "\t" "sequence_%ld_%ld -> term_%ld_%ld_%ld" "\n", | ||
| 129 | rule - syntax.items, | ||
| 130 | sequence - rule->is_replaced_by.items, | ||
| 131 | rule - syntax.items, | ||
| 132 | sequence - rule->is_replaced_by.items, | ||
| 133 | term - sequence->items); | ||
| 134 | STRING_BUILDER_RESET (sb); | ||
| 135 | switch (term->kind) { | ||
| 136 | case BNF_LEXEME_KIND_TERMINAL: | ||
| 137 | STRING_BUILDER_APPEND_CSTR (sb, "\\\""); | ||
| 138 | STRING_BUILDER_APPEND_STRING_VIEW ( | ||
| 139 | sb, ((struct string_view) { | ||
| 140 | .beg = term->sv.beg + 1, | ||
| 141 | .end = term->sv.end - 1, | ||
| 142 | })); | ||
| 143 | STRING_BUILDER_APPEND_CSTR (sb, "\\\""); | ||
| 144 | break; | ||
| 145 | case BNF_LEXEME_KIND_NONTERMINAL: | ||
| 146 | STRING_BUILDER_APPEND_STRING_VIEW (sb, term->sv); | ||
| 147 | break; | ||
| 148 | default: UNREACHABLE (); | ||
| 149 | } | ||
| 150 | STRING_BUILDER_APPEND_NULL (sb); | ||
| 151 | fprintf (fp, "\t" "term_%ld_%ld_%ld [label=\"%s\"]" "\n", | ||
| 152 | rule - syntax.items, | ||
| 153 | sequence - rule->is_replaced_by.items, | ||
| 154 | term - sequence->items, | ||
| 155 | sb.string); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } fprintf (fp, "}\n"); | ||
| 160 | |||
| 161 | fclose (fp); | ||
| 162 | } | ||
| 163 | |||
| 164 | static | ||
| 165 | void print_bnf (struct bnf_syntax syntax) | ||
| 166 | { | ||
| 167 | DYNAMIC_ARRAY_FOREACH (rule, syntax) { | ||
| 168 | printf ("%.*s ::=", | ||
| 169 | (int) STRING_VIEW_LEN (rule->name.sv), rule->name.sv.beg); | ||
| 170 | DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) { | ||
| 171 | if (sequence != rule->is_replaced_by.items) | ||
| 172 | printf (" |"); | ||
| 173 | DYNAMIC_ARRAY_FOREACH (term, *sequence) { | ||
| 174 | printf (" %.*s", (int) STRING_VIEW_LEN (term->sv), term->sv.beg); | ||
| 175 | } | ||
| 176 | } printf ("\n"); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | typedef uint32_t symbol; | ||
| 181 | |||
| 182 | struct symbols { | ||
| 183 | symbol *items; | ||
| 184 | uint32_t count; | ||
| 185 | uint32_t capacity; | ||
| 186 | }; | ||
| 187 | |||
| 188 | struct production { | ||
| 189 | symbol from; | ||
| 190 | struct symbols to; | ||
| 191 | }; | ||
| 192 | |||
| 193 | struct productions { | ||
| 194 | struct production *items; | ||
| 195 | uint32_t count; | ||
| 196 | uint32_t capacity; | ||
| 197 | }; | ||
| 198 | |||
| 199 | struct selections { | ||
| 200 | struct selection { | ||
| 201 | symbol input; | ||
| 202 | uint32_t production; | ||
| 203 | } *items; | ||
| 204 | uint32_t count; | ||
| 205 | uint32_t capacity; | ||
| 206 | }; | ||
| 207 | |||
| 208 | struct nonterminal { | ||
| 209 | const char *name; | ||
| 210 | struct selections selections; | ||
| 211 | }; | ||
| 212 | |||
| 213 | struct nonterminals { | ||
| 214 | struct nonterminal *items; | ||
| 215 | uint32_t count; | ||
| 216 | uint32_t capacity; | ||
| 217 | }; | ||
| 218 | |||
| 219 | // struct terminals { | ||
| 220 | // const char **items; | ||
| 221 | // uint32_t count; | ||
| 222 | // uint32_t capacity; | ||
| 223 | // }; | ||
| 224 | |||
| 225 | struct grammar { | ||
| 226 | symbol initial_nonterminal; | ||
| 227 | struct nonterminals nonterminals; | ||
| 228 | // struct terminals terminals; | ||
| 229 | struct productions productions; | ||
| 230 | }; | ||
| 231 | |||
| 232 | static struct grammar | ||
| 233 | build_grammar (struct bnf_syntax syntax) | ||
| 234 | { | ||
| 235 | struct grammar grammar = { .initial_nonterminal = TERMINAL_COUNT }; | ||
| 236 | |||
| 237 | DYNAMIC_ARRAY_FOREACH (rule, syntax) { | ||
| 238 | size_t nonterminal_name_len = STRING_VIEW_LEN (rule->name.sv) - 2; | ||
| 239 | char *nonterminal_name = malloc (nonterminal_name_len + 1); | ||
| 240 | nonterminal_name[nonterminal_name_len] = '\0'; | ||
| 241 | memcpy (nonterminal_name, rule->name.sv.beg + 1, nonterminal_name_len); | ||
| 242 | DYNAMIC_ARRAY_APPEND (grammar.nonterminals, | ||
| 243 | ((struct nonterminal) { .name = nonterminal_name })); | ||
| 244 | } | ||
| 245 | |||
| 246 | DYNAMIC_ARRAY_FOREACH (rule, syntax) { | ||
| 247 | symbol from = TERMINAL_COUNT + (rule - syntax.items); | ||
| 248 | DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) { | ||
| 249 | struct production production = { .from = from }; | ||
| 250 | DYNAMIC_ARRAY_FOREACH (term, *sequence) { | ||
| 251 | switch (term->kind) { | ||
| 252 | case BNF_LEXEME_KIND_TERMINAL: | ||
| 253 | for (const char *p = term->sv.beg + 1; p < term->sv.end - 1; ++p) | ||
| 254 | DYNAMIC_ARRAY_APPEND (production.to, (uint8_t) *p); | ||
| 255 | break; | ||
| 256 | case BNF_LEXEME_KIND_NONTERMINAL: | ||
| 257 | if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) { | ||
| 258 | DYNAMIC_ARRAY_APPEND (production.to, 256); | ||
| 259 | } else if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) { | ||
| 260 | DYNAMIC_ARRAY_APPEND (production.to, 257); | ||
| 261 | } else { | ||
| 262 | size_t term_len = STRING_VIEW_LEN (term->sv) - 2; | ||
| 263 | DYNAMIC_ARRAY_FOREACH (nonterminal, grammar.nonterminals) { | ||
| 264 | size_t nonterminal_name_len = strlen (nonterminal->name); | ||
| 265 | if (nonterminal_name_len != term_len) continue; | ||
| 266 | if (memcmp (nonterminal->name, term->sv.beg + 1, term_len)) continue; | ||
| 267 | DYNAMIC_ARRAY_APPEND (production.to, TERMINAL_COUNT + (nonterminal - grammar.nonterminals.items)); | ||
| 268 | break; | ||
| 269 | } | ||
| 270 | } break; | ||
| 271 | default: UNREACHABLE (); | ||
| 272 | }; | ||
| 273 | } | ||
| 274 | DYNAMIC_ARRAY_APPEND (grammar.productions, production); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return grammar; | ||
| 279 | } | ||
| 280 | |||
| 281 | static void | ||
| 282 | fprint_symbol (FILE *fp, struct grammar grammar, symbol symbol) | ||
| 283 | { | ||
| 284 | if (symbol == ' ') { | ||
| 285 | fprintf (fp, "‿"); | ||
| 286 | } else if (symbol < 256) { | ||
| 287 | // printf ("0x%02X", symbol); | ||
| 288 | putc (symbol, fp); | ||
| 289 | } else if (symbol == 256) { | ||
| 290 | fprintf (fp, "<EOL>"); | ||
| 291 | } else if (symbol == 257) { | ||
| 292 | fprintf (fp, "<EOF>"); | ||
| 293 | } else { | ||
| 294 | fprintf (fp, "<%s>", grammar.nonterminals.items[symbol - TERMINAL_COUNT].name); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | static void | ||
| 299 | print_symbol (struct grammar grammar, symbol symbol) | ||
| 300 | { | ||
| 301 | fprint_symbol (stdout, grammar, symbol); | ||
| 302 | } | ||
| 303 | |||
| 304 | static void | ||
| 305 | print_symbols (struct grammar grammar, struct symbols symbols) | ||
| 306 | { | ||
| 307 | bool printing_nonterminal = true; | ||
| 308 | if (symbols.count == 0) | ||
| 309 | printf ("\"\""); | ||
| 310 | DYNAMIC_ARRAY_FOREACH (symbol, symbols) { | ||
| 311 | if (printing_nonterminal && *symbol < 256) { | ||
| 312 | putchar ('"'); | ||
| 313 | printing_nonterminal = false; | ||
| 314 | } else if (!printing_nonterminal && *symbol >= 256) { | ||
| 315 | putchar ('"'); | ||
| 316 | printing_nonterminal = true; | ||
| 317 | } | ||
| 318 | print_symbol (grammar, *symbol); | ||
| 319 | } | ||
| 320 | if (!printing_nonterminal) | ||
| 321 | putchar ('"'); | ||
| 322 | printf ("\n"); | ||
| 323 | } | ||
| 324 | |||
| 325 | static void | ||
| 326 | print_production (struct grammar grammar, struct production production) | ||
| 327 | { | ||
| 328 | print_symbol (grammar, production.from); | ||
| 329 | printf (" -> "); | ||
| 330 | print_symbols (grammar, production.to); | ||
| 331 | } | ||
| 332 | |||
| 333 | static void | ||
| 334 | print_grammar (struct grammar grammar) | ||
| 335 | { | ||
| 336 | printf ("Initial nonterminal: "); | ||
| 337 | print_symbol (grammar, grammar.initial_nonterminal); | ||
| 338 | printf ("\n"); | ||
| 339 | DYNAMIC_ARRAY_FOREACH (production, grammar.productions) { | ||
| 340 | printf ("%lu. ", production - grammar.productions.items); | ||
| 341 | print_production (grammar, *production); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | static int | ||
| 346 | compare_production_pointers (const void *a, const void *b) | ||
| 347 | { | ||
| 348 | const struct production *production_a = *(const struct production **) a; | ||
| 349 | const struct production *production_b = *(const struct production **) b; | ||
| 350 | |||
| 351 | if (production_a->from < production_b->from) return +1; | ||
| 352 | if (production_a->from > production_b->from) return -1; | ||
| 353 | |||
| 354 | struct symbols symbols_a = production_a->to; | ||
| 355 | struct symbols symbols_b = production_b->to; | ||
| 356 | |||
| 357 | for (uint32_t i = 0; i < MIN (symbols_a.count, symbols_b.count); ++i) { | ||
| 358 | symbol symbol_a = symbols_a.items[i]; | ||
| 359 | symbol symbol_b = symbols_b.items[i]; | ||
| 360 | if (symbol_a == symbol_b) continue; | ||
| 361 | return (int) symbol_b - symbol_a; | ||
| 362 | } | ||
| 363 | |||
| 364 | if (symbols_a.count < symbols_b.count) return +1; | ||
| 365 | if (symbols_a.count > symbols_b.count) return -1; | ||
| 366 | |||
| 367 | return 0; | ||
| 368 | } | ||
| 369 | |||
| 370 | static int | ||
| 371 | compare_symbols (const void *a, const void *b) | ||
| 372 | { | ||
| 373 | const symbol symbol_a = *(const symbol *) a; | ||
| 374 | const symbol symbol_b = *(const symbol *) b; | ||
| 375 | |||
| 376 | return symbol_a - symbol_b; | ||
| 377 | } | ||
| 378 | |||
| 379 | static void | ||
| 380 | grammar_left_factor_common_prefixes (struct grammar *grammar) | ||
| 381 | { | ||
| 382 | struct symbols handled = {0}; | ||
| 383 | |||
| 384 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 385 | DYNAMIC_ARRAY_FOREACH (symbol, handled) { | ||
| 386 | if (*symbol == production->from) goto next_production; | ||
| 387 | } | ||
| 388 | |||
| 389 | symbol from = production->from; | ||
| 390 | |||
| 391 | size_t factored = 0; | ||
| 392 | factor_single: | ||
| 393 | struct { | ||
| 394 | struct production **items; | ||
| 395 | uint32_t count; | ||
| 396 | uint32_t capacity; | ||
| 397 | } same = {0}; | ||
| 398 | DYNAMIC_ARRAY_FOREACH (other, grammar->productions) { | ||
| 399 | if (other->from != from) continue; | ||
| 400 | DYNAMIC_ARRAY_APPEND (same, other); | ||
| 401 | } | ||
| 402 | |||
| 403 | qsort (same.items, same.count, sizeof (same.items[0]), compare_production_pointers); | ||
| 404 | // puts ("same:"); | ||
| 405 | // DYNAMIC_ARRAY_FOREACH (prod, same) { | ||
| 406 | // print_production (*grammar, **prod); | ||
| 407 | // } | ||
| 408 | |||
| 409 | DYNAMIC_ARRAY_FOREACH (production, same) { | ||
| 410 | if (production + 1 == same.items + same.count) break; | ||
| 411 | struct production *prod = *(production + 0); | ||
| 412 | struct production *next = *(production + 1); | ||
| 413 | |||
| 414 | struct symbols prefix = {0}; | ||
| 415 | for (uint32_t i = 0; i < MIN (prod->to.count, next->to.count); ++i) { | ||
| 416 | if (prod->to.items[i] != next->to.items[i]) break; | ||
| 417 | DYNAMIC_ARRAY_APPEND (prefix, prod->to.items[i]); | ||
| 418 | } if (prefix.count == 0) continue; | ||
| 419 | |||
| 420 | // printf ("prefix: "); | ||
| 421 | // print_symbols (*grammar, prefix); | ||
| 422 | |||
| 423 | const char *nonterminal_name = grammar->nonterminals.items[prod->from - TERMINAL_COUNT].name; | ||
| 424 | size_t nonterminal_name_len = strlen (nonterminal_name); | ||
| 425 | char *nonterminal_name_new = malloc (nonterminal_name_len + 2 + factored); | ||
| 426 | symbol from_new = TERMINAL_COUNT + grammar->nonterminals.count; | ||
| 427 | memcpy (nonterminal_name_new, nonterminal_name, nonterminal_name_len); | ||
| 428 | for (size_t i = nonterminal_name_len; i < nonterminal_name_len + factored + 1; ++i) | ||
| 429 | nonterminal_name_new[i] = '\''; | ||
| 430 | nonterminal_name_new[nonterminal_name_len + factored + 1] = '\0'; | ||
| 431 | DYNAMIC_ARRAY_APPEND (grammar->nonterminals, | ||
| 432 | ((struct nonterminal) { .name = nonterminal_name_new })); | ||
| 433 | |||
| 434 | struct production prod_new = { .from = prod->from }; | ||
| 435 | DYNAMIC_ARRAY_APPEND_MANY (prod_new.to, prefix.items, prefix.count); | ||
| 436 | DYNAMIC_ARRAY_APPEND (prod_new.to, from_new); | ||
| 437 | DYNAMIC_ARRAY_APPEND (grammar->productions, prod_new); | ||
| 438 | |||
| 439 | struct { | ||
| 440 | struct production **items; | ||
| 441 | uint32_t count; | ||
| 442 | uint32_t capacity; | ||
| 443 | } rewrite = {0}; | ||
| 444 | DYNAMIC_ARRAY_APPEND (rewrite, prod); | ||
| 445 | DYNAMIC_ARRAY_APPEND (rewrite, next); | ||
| 446 | for (uint32_t i = production + 2 - same.items; i < same.count; ++i) { | ||
| 447 | struct production *p = same.items[i]; | ||
| 448 | if (p->to.count < prefix.count) continue; | ||
| 449 | if (memcmp (p->to.items, prefix.items, prefix.count*sizeof (*prefix.items))) continue; | ||
| 450 | DYNAMIC_ARRAY_APPEND (rewrite, p); | ||
| 451 | } | ||
| 452 | |||
| 453 | DYNAMIC_ARRAY_FOREACH (p, rewrite) { | ||
| 454 | struct production *prod = *p; | ||
| 455 | prod->from = from_new; | ||
| 456 | memmove (prod->to.items, | ||
| 457 | prod->to.items + prefix.count, | ||
| 458 | (prod->to.count - prefix.count) * | ||
| 459 | sizeof (prod->to.items[0])); | ||
| 460 | prod->to.count -= prefix.count; | ||
| 461 | } | ||
| 462 | |||
| 463 | factored += 1; | ||
| 464 | goto factor_single; | ||
| 465 | } | ||
| 466 | |||
| 467 | DYNAMIC_ARRAY_APPEND (handled, production->from); | ||
| 468 | next_production: | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | static bool | ||
| 473 | first (struct grammar grammar, struct symbols syms, struct symbols *set) | ||
| 474 | { | ||
| 475 | if (syms.count == 0) return true; | ||
| 476 | |||
| 477 | symbol first_sym = syms.items[0]; | ||
| 478 | if (first_sym < TERMINAL_COUNT) { | ||
| 479 | DYNAMIC_ARRAY_FOREACH (sym, *set) | ||
| 480 | if (*sym == first_sym) return false; | ||
| 481 | DYNAMIC_ARRAY_APPEND (*set, first_sym); | ||
| 482 | return false; | ||
| 483 | } | ||
| 484 | |||
| 485 | bool has_epsilon = false; | ||
| 486 | DYNAMIC_ARRAY_FOREACH (production, grammar.productions) { | ||
| 487 | if (production->from != first_sym) continue; | ||
| 488 | has_epsilon = first (grammar, production->to, set); | ||
| 489 | } | ||
| 490 | |||
| 491 | if (has_epsilon) { | ||
| 492 | syms.items += 1; | ||
| 493 | syms.count -= 1; | ||
| 494 | |||
| 495 | return first (grammar, syms, set); | ||
| 496 | } | ||
| 497 | |||
| 498 | return false; | ||
| 499 | } | ||
| 500 | |||
| 501 | static void | ||
| 502 | follow (struct grammar grammar, symbol nonterm, struct symbols *set, struct symbols *stack) | ||
| 503 | { | ||
| 504 | DYNAMIC_ARRAY_FOREACH (nt, *stack) | ||
| 505 | if (*nt == nonterm) return; | ||
| 506 | |||
| 507 | DYNAMIC_ARRAY_APPEND (*stack, nonterm); | ||
| 508 | DYNAMIC_ARRAY_FOREACH (production, grammar.productions) { | ||
| 509 | DYNAMIC_ARRAY_FOREACH (sym, production->to) { | ||
| 510 | if (*sym != nonterm) continue; | ||
| 511 | if (sym == &DYNAMIC_ARRAY_LAST (production->to)) { | ||
| 512 | if (*sym == production->from) continue; | ||
| 513 | follow (grammar, production->from, set, stack); | ||
| 514 | continue; | ||
| 515 | } | ||
| 516 | |||
| 517 | struct symbols syms = { | ||
| 518 | .items = sym + 1, | ||
| 519 | .count = production->to.count - (sym - production->to.items), | ||
| 520 | }; | ||
| 521 | if (first (grammar, syms, set)) | ||
| 522 | follow (grammar, production->from, set, stack); | ||
| 523 | } | ||
| 524 | } | ||
| 525 | stack->count -= 1; | ||
| 526 | } | ||
| 527 | |||
| 528 | static void | ||
| 529 | build_lookup_table (struct grammar *grammar) | ||
| 530 | { | ||
| 531 | struct symbols selection = {0}; | ||
| 532 | struct symbols stack = {0}; | ||
| 533 | DYNAMIC_ARRAY_FOREACH (nonterminal, grammar->nonterminals) { | ||
| 534 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 535 | if (production->from != TERMINAL_COUNT + nonterminal - grammar->nonterminals.items) continue; | ||
| 536 | // print_production (*grammar, *production); | ||
| 537 | selection.count = 0; | ||
| 538 | stack.count = 0; | ||
| 539 | if (first (*grammar, production->to, &selection)) | ||
| 540 | follow (*grammar, production->from, &selection, &stack); | ||
| 541 | |||
| 542 | for (uint32_t i = 0; i < selection.count - 1; ++i) { | ||
| 543 | for (uint32_t j = i + 1; j < selection.count; ++j) { | ||
| 544 | if (selection.items[j] != selection.items[i]) continue; | ||
| 545 | selection.items[j] = selection.items[--selection.count]; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | // DYNAMIC_ARRAY_FOREACH (sym, selection) { | ||
| 550 | // if (*sym < 256) | ||
| 551 | // putchar (*sym); | ||
| 552 | // else if (*sym == 256) | ||
| 553 | // printf ("<EOL>"); | ||
| 554 | // else if (*sym == 257) | ||
| 555 | // printf ("<EOF>"); | ||
| 556 | // } printf ("\n"); | ||
| 557 | |||
| 558 | DYNAMIC_ARRAY_FOREACH (sym, selection) { | ||
| 559 | DYNAMIC_ARRAY_APPEND ( | ||
| 560 | nonterminal->selections, | ||
| 561 | ((struct selection) { | ||
| 562 | .input = *sym, | ||
| 563 | .production = production - grammar->productions.items, | ||
| 564 | })); | ||
| 565 | } | ||
| 566 | } | ||
| 567 | } | ||
| 568 | DYNAMIC_ARRAY_FREE (selection); | ||
| 569 | DYNAMIC_ARRAY_FREE (stack); | ||
| 570 | |||
| 571 | // DYNAMIC_ARRAY_FOREACH (nonterminal, grammar->nonterminals) { | ||
| 572 | // printf ("<%s> |", nonterminal->name); | ||
| 573 | // DYNAMIC_ARRAY_FOREACH (selection, nonterminal->selections) { | ||
| 574 | // if (selection->input == 256) | ||
| 575 | // printf (" <EOL> -> %u |", selection->production); | ||
| 576 | // else if (selection->input == 257) | ||
| 577 | // printf (" <EOF> -> %u |", selection->production); | ||
| 578 | // else | ||
| 579 | // printf (" %c -> %u |", selection->input, selection->production); | ||
| 580 | // } printf ("\n"); | ||
| 581 | // } | ||
| 582 | } | ||
| 583 | |||
| 584 | struct tree { | ||
| 585 | struct children { | ||
| 586 | struct tree *items; | ||
| 587 | uint32_t count; | ||
| 588 | uint32_t capacity; | ||
| 589 | } children; | ||
| 590 | struct tree *parent; | ||
| 591 | const char *begin; | ||
| 592 | symbol symbol; | ||
| 593 | }; | ||
| 594 | |||
| 595 | static struct tree * | ||
| 596 | tree_root (struct tree *tree) | ||
| 597 | { | ||
| 598 | ASSERT (tree); | ||
| 599 | while (tree->parent) | ||
| 600 | tree = tree->parent; | ||
| 601 | return tree; | ||
| 602 | } | ||
| 603 | |||
| 604 | static size_t | ||
| 605 | tree_count_terminals_within (struct tree *node) | ||
| 606 | { | ||
| 607 | ASSERT (node); | ||
| 608 | |||
| 609 | if (!node->begin) return 0; | ||
| 610 | |||
| 611 | if (node->children.count == 0) | ||
| 612 | return node->symbol < TERMINAL_COUNT; // TODO: handle special symbols | ||
| 613 | |||
| 614 | size_t sum = 0; | ||
| 615 | DYNAMIC_ARRAY_FOREACH (child, node->children) | ||
| 616 | sum += tree_count_terminals_within (child); | ||
| 617 | return sum; | ||
| 618 | } | ||
| 619 | |||
| 620 | static size_t | ||
| 621 | tree_count_terminals_from_to (struct tree *from, struct tree *to) | ||
| 622 | { | ||
| 623 | ASSERT (from); | ||
| 624 | ASSERT (to); | ||
| 625 | |||
| 626 | if (from >= to) | ||
| 627 | return 0; | ||
| 628 | |||
| 629 | if (from->children.count == 0) | ||
| 630 | return from->symbol < TERMINAL_COUNT; // TODO: handle special symbols | ||
| 631 | |||
| 632 | size_t sum = 0; | ||
| 633 | DYNAMIC_ARRAY_FOREACH (child, from->children) | ||
| 634 | sum += tree_count_terminals_within (from); | ||
| 635 | return sum; | ||
| 636 | } | ||
| 637 | |||
| 638 | static size_t | ||
| 639 | tree_count_terminals_up_to (struct tree *to) | ||
| 640 | { | ||
| 641 | ASSERT (to); | ||
| 642 | return tree_count_terminals_from_to (tree_root (to), to); | ||
| 643 | } | ||
| 644 | |||
| 645 | static void | ||
| 646 | print_tree_highlight (struct grammar grammar, | ||
| 647 | struct tree *tree, | ||
| 648 | struct tree *highlight, | ||
| 649 | int depth) | ||
| 650 | { | ||
| 651 | ASSERT (tree); | ||
| 652 | |||
| 653 | for (int i = 0; i < depth; ++i) | ||
| 654 | printf (" "); | ||
| 655 | printf (tree == highlight ? "+ " : " "); | ||
| 656 | if (tree->symbol < 256) putchar ('"'); | ||
| 657 | print_symbol (grammar, tree->symbol); | ||
| 658 | if (tree->symbol < 256) putchar ('"'); | ||
| 659 | if (tree->begin) | ||
| 660 | printf (" -> \"%.*s\"", | ||
| 661 | (int) tree_count_terminals_within (tree), tree->begin); | ||
| 662 | printf ("\n"); | ||
| 663 | DYNAMIC_ARRAY_FOREACH (child, tree->children) | ||
| 664 | print_tree_highlight (grammar, child, highlight, depth + 1); | ||
| 665 | if (depth == 0) | ||
| 666 | printf ("\n"); | ||
| 667 | } | ||
| 668 | |||
| 669 | static void | ||
| 670 | print_tree (struct grammar grammar, struct tree *tree) | ||
| 671 | { | ||
| 672 | ASSERT (tree); | ||
| 673 | print_tree_highlight (grammar, tree_root (tree), tree, 0); | ||
| 674 | } | ||
| 675 | |||
| 676 | static struct tree * | ||
| 677 | tree_prev_leaf (struct tree *tree) | ||
| 678 | { | ||
| 679 | ASSERT (tree); | ||
| 680 | |||
| 681 | if (tree->children.count > 0) { | ||
| 682 | while (tree->parent && tree == tree->parent->children.items) | ||
| 683 | tree = tree->parent; | ||
| 684 | if (!tree->parent) return NULL; | ||
| 685 | tree -= 1; | ||
| 686 | while (tree->children.count > 0) | ||
| 687 | tree = &DYNAMIC_ARRAY_LAST (tree->children); | ||
| 688 | return tree; | ||
| 689 | } | ||
| 690 | |||
| 691 | if (!tree->parent) return NULL; | ||
| 692 | if (tree > tree->parent->children.items) { | ||
| 693 | tree -= 1; | ||
| 694 | while (tree->children.count > 0) | ||
| 695 | tree = &DYNAMIC_ARRAY_LAST (tree->children); | ||
| 696 | return tree; | ||
| 697 | } | ||
| 698 | |||
| 699 | return NULL; | ||
| 700 | } | ||
| 701 | |||
| 702 | static struct tree * | ||
| 703 | tree_next_leaf (struct tree *tree) | ||
| 704 | { | ||
| 705 | ASSERT (tree); | ||
| 706 | |||
| 707 | if (tree->children.count > 0) { | ||
| 708 | do tree = tree->children.items; | ||
| 709 | while (tree->children.count > 0); | ||
| 710 | return tree; | ||
| 711 | } | ||
| 712 | |||
| 713 | if (!tree->parent) return NULL; | ||
| 714 | |||
| 715 | if (tree < &DYNAMIC_ARRAY_LAST (tree->parent->children)) { | ||
| 716 | tree += 1; | ||
| 717 | while (tree->children.count > 0) | ||
| 718 | tree = tree->children.items; | ||
| 719 | return tree; | ||
| 720 | } | ||
| 721 | |||
| 722 | do tree = tree->parent; | ||
| 723 | while (tree->parent && tree == &DYNAMIC_ARRAY_LAST (tree->parent->children)); | ||
| 724 | if (!tree->parent) return NULL; | ||
| 725 | return tree + 1; | ||
| 726 | } | ||
| 727 | |||
| 728 | static void | ||
| 729 | tree_remove_child (struct tree *child) | ||
| 730 | { | ||
| 731 | while (child->parent && child->children.count == 0) { | ||
| 732 | struct tree *parent = child->parent; | ||
| 733 | if (child != &DYNAMIC_ARRAY_LAST (parent->children)) | ||
| 734 | memmove (child, child + 1, | ||
| 735 | (&DYNAMIC_ARRAY_LAST (parent->children) - child)*sizeof (*child)); | ||
| 736 | parent->children.count -= 1; | ||
| 737 | child = parent; | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | static bool | ||
| 742 | parse_impl (struct grammar grammar, struct tree *leaf) | ||
| 743 | { | ||
| 744 | do { | ||
| 745 | if (leaf->parent) { | ||
| 746 | leaf->begin = leaf->parent->begin; | ||
| 747 | DYNAMIC_ARRAY_FOREACH (child, leaf->parent->children) { | ||
| 748 | if (child == leaf) break; | ||
| 749 | leaf->begin += tree_count_terminals_within (child); | ||
| 750 | } | ||
| 751 | } | ||
| 752 | |||
| 753 | if (leaf->symbol < TERMINAL_COUNT) { | ||
| 754 | if (!(*leaf->begin == '\n' && leaf->symbol == 256 || | ||
| 755 | *leaf->begin == '\0' && leaf->symbol == 257) && | ||
| 756 | (uint8_t) *leaf->begin != leaf->symbol) { | ||
| 757 | // printf ("didn't match %c (expected %c)\n", *leaf->begin, leaf->symbol); | ||
| 758 | return false; | ||
| 759 | }; | ||
| 760 | |||
| 761 | // printf ("matched terminal "); | ||
| 762 | // print_symbol (grammar, leaf->symbol); | ||
| 763 | // printf ("\n"); | ||
| 764 | } else { // nonterminal | ||
| 765 | struct selections selections = | ||
| 766 | grammar.nonterminals.items[leaf->symbol - TERMINAL_COUNT].selections; | ||
| 767 | DYNAMIC_ARRAY_FOREACH (selection, selections) { | ||
| 768 | if (!(*leaf->begin == '\n' && selection->input == 256 || | ||
| 769 | *leaf->begin == '\0' && selection->input == 257 || | ||
| 770 | selection->input == (uint8_t) *leaf->begin)) | ||
| 771 | continue; | ||
| 772 | |||
| 773 | struct production production = | ||
| 774 | grammar.productions.items[selection->production]; | ||
| 775 | if (production.to.count == 0) { | ||
| 776 | if (!leaf->parent) REACHED ("peculiar situation"); | ||
| 777 | // print_tree (grammar, leaf); | ||
| 778 | if (parse_impl (grammar, tree_next_leaf (leaf))) | ||
| 779 | return true; | ||
| 780 | } else { | ||
| 781 | DYNAMIC_ARRAY_FOREACH (sym, production.to) { | ||
| 782 | DYNAMIC_ARRAY_APPEND (leaf->children, ((struct tree) { | ||
| 783 | .symbol = *sym, | ||
| 784 | .parent = leaf, | ||
| 785 | })); | ||
| 786 | } | ||
| 787 | // print_tree (grammar, leaf); | ||
| 788 | if (parse_impl (grammar, tree_next_leaf (leaf))) | ||
| 789 | return true; | ||
| 790 | DYNAMIC_ARRAY_RESET (leaf->children); | ||
| 791 | } | ||
| 792 | } | ||
| 793 | // puts ("backtracking"); | ||
| 794 | return false; | ||
| 795 | } | ||
| 796 | } while (leaf = tree_next_leaf (leaf)); | ||
| 797 | return true; // only return true if fully parsed! | ||
| 798 | } | ||
| 799 | |||
| 800 | static struct tree | ||
| 801 | parse (struct grammar grammar, const char *str) | ||
| 802 | { | ||
| 803 | struct tree tree = { | ||
| 804 | .symbol = grammar.initial_nonterminal, | ||
| 805 | .begin = str, | ||
| 806 | }; | ||
| 807 | parse_impl (grammar, &tree); | ||
| 808 | return tree; | ||
| 809 | } | ||
| 810 | |||
| 811 | static void | ||
| 812 | grammar_reduce (struct grammar *grammar) | ||
| 813 | { | ||
| 814 | struct symbols reachable = {0}; | ||
| 815 | DYNAMIC_ARRAY_APPEND (reachable, grammar->initial_nonterminal); | ||
| 816 | for (uint32_t i = 0; i < reachable.count; ++i) { | ||
| 817 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 818 | if (production->from != reachable.items[i]) continue; | ||
| 819 | DYNAMIC_ARRAY_FOREACH (symbol, production->to) { | ||
| 820 | if (*symbol < TERMINAL_COUNT) continue; | ||
| 821 | DYNAMIC_ARRAY_FOREACH (nonterminal, reachable) | ||
| 822 | if (*nonterminal == *symbol) goto reachable_next_symbol; | ||
| 823 | DYNAMIC_ARRAY_APPEND (reachable, *symbol); | ||
| 824 | reachable_next_symbol: | ||
| 825 | } | ||
| 826 | } | ||
| 827 | } | ||
| 828 | |||
| 829 | struct { | ||
| 830 | struct production **items; | ||
| 831 | uint32_t count; | ||
| 832 | uint32_t capacity; | ||
| 833 | } productive = {0}; | ||
| 834 | uint32_t added; | ||
| 835 | do { | ||
| 836 | added = productive.count; | ||
| 837 | |||
| 838 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 839 | DYNAMIC_ARRAY_FOREACH (productive_production, productive) { | ||
| 840 | if (*productive_production == production) | ||
| 841 | goto productive_next_production; | ||
| 842 | } | ||
| 843 | |||
| 844 | DYNAMIC_ARRAY_FOREACH (symbol, production->to) { | ||
| 845 | if (*symbol < TERMINAL_COUNT) continue; | ||
| 846 | DYNAMIC_ARRAY_FOREACH (productive_production, productive) { | ||
| 847 | if ((*productive_production)->from == *symbol) | ||
| 848 | goto productive_next_symbol; | ||
| 849 | } | ||
| 850 | goto productive_next_production; | ||
| 851 | productive_next_symbol: | ||
| 852 | } | ||
| 853 | |||
| 854 | DYNAMIC_ARRAY_APPEND (productive, production); | ||
| 855 | productive_next_production: | ||
| 856 | } | ||
| 857 | |||
| 858 | added = productive.count - added; | ||
| 859 | } while (added); | ||
| 860 | |||
| 861 | // print_symbols (*grammar, reachable); | ||
| 862 | // DYNAMIC_ARRAY_FOREACH (production, productive) | ||
| 863 | // print_production (*grammar, **production); | ||
| 864 | |||
| 865 | struct productions productions = {0}; | ||
| 866 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 867 | DYNAMIC_ARRAY_FOREACH (reachable_nonterminal, reachable) | ||
| 868 | if (*reachable_nonterminal == production->from) goto reachable; | ||
| 869 | DYNAMIC_ARRAY_FREE (production->to); | ||
| 870 | continue; | ||
| 871 | reachable: | ||
| 872 | DYNAMIC_ARRAY_FOREACH (productive_production, productive) | ||
| 873 | if (*productive_production == production) goto productive; | ||
| 874 | DYNAMIC_ARRAY_FREE (production->to); | ||
| 875 | continue; | ||
| 876 | productive: | ||
| 877 | DYNAMIC_ARRAY_APPEND (productions, *production); | ||
| 878 | } | ||
| 879 | DYNAMIC_ARRAY_FREE (grammar->productions); | ||
| 880 | grammar->productions = productions; | ||
| 881 | } | ||
| 882 | |||
| 883 | static void | ||
| 884 | grammar_add_combinations (struct grammar *grammar, struct symbols input, | ||
| 885 | struct production output1, symbol sym, uint32_t remove) | ||
| 886 | { | ||
| 887 | if (remove == 0) { | ||
| 888 | DYNAMIC_ARRAY_APPEND_MANY (output1.to, input.items, input.count); | ||
| 889 | DYNAMIC_ARRAY_APPEND (grammar->productions, output1); | ||
| 890 | return; | ||
| 891 | } | ||
| 892 | |||
| 893 | while (input.count != 0 && input.items[0] != sym) { | ||
| 894 | DYNAMIC_ARRAY_APPEND (output1.to, input.items[0]); | ||
| 895 | input.items += 1; | ||
| 896 | input.count -= 1; | ||
| 897 | } | ||
| 898 | |||
| 899 | if (input.count == 0) { | ||
| 900 | DYNAMIC_ARRAY_FREE (output1.to); | ||
| 901 | return; | ||
| 902 | } | ||
| 903 | |||
| 904 | struct production output2 = { .from = output1.from }; | ||
| 905 | DYNAMIC_ARRAY_APPEND_MANY (output2.to, output1.to.items, output1.to.count); | ||
| 906 | DYNAMIC_ARRAY_APPEND (output1.to, input.items[0]); | ||
| 907 | input.items += 1; | ||
| 908 | input.count -= 1; | ||
| 909 | grammar_add_combinations (grammar, input, output1, sym, remove - 0); | ||
| 910 | grammar_add_combinations (grammar, input, output2, sym, remove - 1); | ||
| 911 | } | ||
| 912 | |||
| 913 | static void | ||
| 914 | grammar_make_epsilon_free (struct grammar *grammar) | ||
| 915 | { | ||
| 916 | bool has_initial_epsilon = false; | ||
| 917 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 918 | if (production->to.count != 0) continue; | ||
| 919 | if (production->from == grammar->initial_nonterminal) | ||
| 920 | has_initial_epsilon = true; | ||
| 921 | |||
| 922 | symbol from = production->from; | ||
| 923 | if (production < &DYNAMIC_ARRAY_LAST (grammar->productions)) { | ||
| 924 | memmove (production, production + 1, | ||
| 925 | (&DYNAMIC_ARRAY_LAST (grammar->productions) - production) * | ||
| 926 | sizeof (*production)); | ||
| 927 | } | ||
| 928 | production -= 1; | ||
| 929 | grammar->productions.count -= 1; | ||
| 930 | |||
| 931 | DYNAMIC_ARRAY_FOREACH (other_production, grammar->productions) { | ||
| 932 | uint32_t count = 0; | ||
| 933 | DYNAMIC_ARRAY_FOREACH (symbol, other_production->to) | ||
| 934 | if (*symbol == from) count += 1; | ||
| 935 | if (count == 0) continue; | ||
| 936 | |||
| 937 | for (uint32_t i = 1; i <= count; ++i) { | ||
| 938 | grammar_add_combinations (grammar, other_production->to, | ||
| 939 | (struct production) { .from = other_production->from }, | ||
| 940 | from, i); | ||
| 941 | } | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | if (has_initial_epsilon) { | ||
| 946 | symbol new_initial_nonterminal = TERMINAL_COUNT + grammar->nonterminals.count; | ||
| 947 | |||
| 948 | const char *initial_nonterminal_name = | ||
| 949 | grammar->nonterminals.items[grammar->initial_nonterminal - TERMINAL_COUNT].name; | ||
| 950 | size_t initial_nonterminal_name_len = strlen (initial_nonterminal_name); | ||
| 951 | char *new_nonterminal_name = malloc (initial_nonterminal_name_len + 2); | ||
| 952 | memcpy (new_nonterminal_name, initial_nonterminal_name, initial_nonterminal_name_len); | ||
| 953 | new_nonterminal_name[initial_nonterminal_name_len + 0] = '*'; | ||
| 954 | new_nonterminal_name[initial_nonterminal_name_len + 1] = '\0'; | ||
| 955 | DYNAMIC_ARRAY_APPEND (grammar->nonterminals, ((struct nonterminal) { .name = new_nonterminal_name })); | ||
| 956 | |||
| 957 | struct production new_production = { .from = new_initial_nonterminal }; | ||
| 958 | DYNAMIC_ARRAY_APPEND (grammar->productions, new_production); | ||
| 959 | DYNAMIC_ARRAY_APPEND (new_production.to, grammar->initial_nonterminal); | ||
| 960 | DYNAMIC_ARRAY_APPEND (grammar->productions, new_production); | ||
| 961 | |||
| 962 | grammar->initial_nonterminal = new_initial_nonterminal; | ||
| 963 | } | ||
| 964 | } | ||
| 965 | |||
| 966 | static void | ||
| 967 | grammar_remove_unit_productions (struct grammar *grammar) | ||
| 968 | { | ||
| 969 | DYNAMIC_ARRAY_FOREACH (production, grammar->productions) { | ||
| 970 | if (production->to.count != 1 || | ||
| 971 | production->to.items[0] < TERMINAL_COUNT) continue; | ||
| 972 | |||
| 973 | symbol from = production->from; | ||
| 974 | symbol to = production->to.items[0]; | ||
| 975 | if (production < &DYNAMIC_ARRAY_LAST (grammar->productions)) { | ||
| 976 | memmove (production, production + 1, | ||
| 977 | (&DYNAMIC_ARRAY_LAST (grammar->productions) - production) * | ||
| 978 | sizeof (*production)); | ||
| 979 | } grammar->productions.count -= 1; | ||
| 980 | |||
| 981 | DYNAMIC_ARRAY_FOREACH (other_production, grammar->productions) { | ||
| 982 | if (other_production->from == from) continue; | ||
| 983 | if (other_production->from != to) continue; | ||
| 984 | if (!(other_production->to.count != 1 || | ||
| 985 | other_production->to.items[0] < TERMINAL_COUNT)) continue; | ||
| 986 | struct production new_production = { .from = from }; | ||
| 987 | DYNAMIC_ARRAY_APPEND_DYNAMIC_ARRAY (new_production.to, other_production->to); | ||
| 988 | uint32_t i = other_production - grammar->productions.items; | ||
| 989 | DYNAMIC_ARRAY_APPEND (grammar->productions, new_production); | ||
| 990 | other_production = &grammar->productions.items[i]; | ||
| 991 | } | ||
| 992 | } | ||
| 993 | } | ||
| 994 | |||
| 995 | static bool | ||
| 996 | starts_with (const char *str, const char *start) | ||
| 997 | { | ||
| 998 | size_t str_len = strlen (str); | ||
| 999 | size_t start_len = strlen (start); | ||
| 1000 | |||
| 1001 | if (str_len < start_len) return false; | ||
| 1002 | return !memcmp (start, str, start_len); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | static bool | ||
| 1006 | symbol_is_property (struct grammar grammar, symbol sym) | ||
| 1007 | { | ||
| 1008 | const char *name = grammar.nonterminals.items[sym - TERMINAL_COUNT].name; | ||
| 1009 | return (starts_with (name, "noun-") || | ||
| 1010 | starts_with (name, "pronoun-") || | ||
| 1011 | starts_with (name, "fragment-s") || | ||
| 1012 | starts_with (name, "fragment-p") || | ||
| 1013 | starts_with (name, "verb-sg-p1-") || | ||
| 1014 | starts_with (name, "verb-sg-p1-") || | ||
| 1015 | starts_with (name, "verb-sg-p2-") || | ||
| 1016 | starts_with (name, "verb-pl-p3-") || | ||
| 1017 | starts_with (name, "verb-pl-p2-") || | ||
| 1018 | starts_with (name, "verb-pl-p3-") || | ||
| 1019 | starts_with (name, "verb-perfective")); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | static void | ||
| 1023 | print_tree_dot (struct grammar grammar, struct tree tree) | ||
| 1024 | { | ||
| 1025 | FILE *fp = fopen ("./tmp/tree.dot", "w"); | ||
| 1026 | fprintf (fp, "digraph {" "\n"); | ||
| 1027 | fprintf (fp, "\t" "node [shape=box, fontname=\"Aporetic Serif Mono,mono\"]" "\n"); | ||
| 1028 | |||
| 1029 | struct stack { | ||
| 1030 | struct tree **items; | ||
| 1031 | uint32_t count; | ||
| 1032 | uint32_t capacity; | ||
| 1033 | }; | ||
| 1034 | struct stack stack_front = {0}; | ||
| 1035 | struct stack stack_back = {0}; | ||
| 1036 | DYNAMIC_ARRAY_APPEND (stack_front, &tree); | ||
| 1037 | while (stack_front.count) { | ||
| 1038 | DYNAMIC_ARRAY_FOREACH (node, stack_front) { | ||
| 1039 | if ((*node)->symbol < TERMINAL_COUNT) continue; | ||
| 1040 | if ((*node)->children.count == 0) continue; | ||
| 1041 | |||
| 1042 | const char *shape = "box"; | ||
| 1043 | if ((*node)->symbol == grammar.initial_nonterminal) | ||
| 1044 | shape = "diamond"; | ||
| 1045 | else if (symbol_is_property (grammar, (*node)->symbol)) | ||
| 1046 | shape = "octagon"; | ||
| 1047 | fprintf (fp, "\t" "n%lu [shape=%s, label=\"%s\"]" "\n", (size_t) *node, shape, | ||
| 1048 | grammar.nonterminals.items[(*node)->symbol - TERMINAL_COUNT].name); | ||
| 1049 | if ((*node)->parent) | ||
| 1050 | fprintf (fp, "\t" "n%lu -> n%lu" "\n", (size_t) (*node)->parent, (size_t) *node); | ||
| 1051 | |||
| 1052 | DYNAMIC_ARRAY_FOREACH (child, (*node)->children) { | ||
| 1053 | DYNAMIC_ARRAY_APPEND (stack_back, child); | ||
| 1054 | } | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | struct stack tmp = stack_front; | ||
| 1058 | stack_front = stack_back; | ||
| 1059 | stack_back = tmp; | ||
| 1060 | DYNAMIC_ARRAY_RESET (stack_back); | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | fprintf (fp, "\t" "subgraph {" "\n"); | ||
| 1064 | fprintf (fp, "\t\t" "node [shape=ellipse, fontname=\"Aporetic Serif,serif\"]" "\n"); | ||
| 1065 | fprintf (fp, "\t\t" "rank=same" "\n"); | ||
| 1066 | struct tree *last = NULL; | ||
| 1067 | for (struct tree *leaf = tree_next_leaf (&tree); leaf; leaf = tree_next_leaf (leaf)) { | ||
| 1068 | if (!(leaf->symbol < TERMINAL_COUNT)) continue; | ||
| 1069 | struct tree *parent = leaf->parent; | ||
| 1070 | fprintf (fp, "\t\t" "l%lu [label=\"", (size_t) leaf); | ||
| 1071 | struct tree *start = leaf; | ||
| 1072 | while (leaf->symbol < TERMINAL_COUNT) { | ||
| 1073 | symbol sym = leaf->symbol; | ||
| 1074 | if (!last && start == leaf) | ||
| 1075 | sym = toupper (sym); | ||
| 1076 | fprint_symbol (fp, grammar, sym); | ||
| 1077 | if (leaf == &DYNAMIC_ARRAY_LAST (parent->children)) | ||
| 1078 | break; | ||
| 1079 | leaf += 1; | ||
| 1080 | } | ||
| 1081 | fprintf (fp, "\"]" "\n"); | ||
| 1082 | if (last) | ||
| 1083 | fprintf (fp, "\t\t" "l%lu -> l%lu [style=invis]\n", (size_t) last, (size_t) start); | ||
| 1084 | last = start; | ||
| 1085 | } | ||
| 1086 | fprintf (fp, "\t" "}" "\n"); | ||
| 1087 | |||
| 1088 | fprintf (fp, "\n"); | ||
| 1089 | for (struct tree *leaf = tree_next_leaf (&tree); leaf; leaf = tree_next_leaf (leaf)) { | ||
| 1090 | if (!(leaf->symbol < TERMINAL_COUNT)) continue; | ||
| 1091 | struct tree *parent = leaf->parent; | ||
| 1092 | fprintf (fp, "\t" "n%lu -> l%lu\n", (size_t) parent, (size_t) leaf); | ||
| 1093 | while (leaf->symbol < TERMINAL_COUNT) { | ||
| 1094 | if (leaf == &DYNAMIC_ARRAY_LAST (parent->children)) | ||
| 1095 | break; | ||
| 1096 | leaf += 1; | ||
| 1097 | } | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | fprintf (fp, "}" "\n"); | ||
| 1101 | fclose (fp); | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | static bool | ||
| 1105 | check_upcase (const char *str) | ||
| 1106 | { | ||
| 1107 | if (isupper (str[0])) return true; | ||
| 1108 | if (!memcmp (str, "Ď", 2)) return true; | ||
| 1109 | if (!memcmp (str, "Ť", 2)) return true; | ||
| 1110 | if (!memcmp (str, "Ň", 2)) return true; | ||
| 1111 | if (!memcmp (str, "Ľ", 2)) return true; | ||
| 1112 | if (!memcmp (str, "Č", 2)) return true; | ||
| 1113 | if (!memcmp (str, "Š", 2)) return true; | ||
| 1114 | if (!memcmp (str, "Ž", 2)) return true; | ||
| 1115 | if (!memcmp (str, "Á", 2)) return true; | ||
| 1116 | if (!memcmp (str, "Ó", 2)) return true; | ||
| 1117 | if (!memcmp (str, "Ú", 2)) return true; | ||
| 1118 | if (!memcmp (str, "É", 2)) return true; | ||
| 1119 | if (!memcmp (str, "Í", 2)) return true; | ||
| 1120 | if (!memcmp (str, "Ý", 2)) return true; | ||
| 1121 | if (!memcmp (str, "Ŕ", 2)) return true; | ||
| 1122 | if (!memcmp (str, "Ĺ", 2)) return true; | ||
| 1123 | if (!memcmp (str, "Ô", 2)) return true; | ||
| 1124 | if (!memcmp (str, "Ä", 2)) return true; | ||
| 1125 | return false; | ||
| 1126 | } | ||
| 1127 | |||
| 1128 | static void | ||
| 1129 | downcase (char *str) | ||
| 1130 | { | ||
| 1131 | if (isascii (str[0])) {str[0] = tolower (str[0]); return;} | ||
| 1132 | if (!memcmp (str, "Ď", 2)) { memcpy (str, "ď", 2); return; } | ||
| 1133 | if (!memcmp (str, "Ť", 2)) { memcpy (str, "ť", 2); return; } | ||
| 1134 | if (!memcmp (str, "Ň", 2)) { memcpy (str, "ň", 2); return; } | ||
| 1135 | if (!memcmp (str, "Ľ", 2)) { memcpy (str, "ľ", 2); return; } | ||
| 1136 | if (!memcmp (str, "Č", 2)) { memcpy (str, "č", 2); return; } | ||
| 1137 | if (!memcmp (str, "Š", 2)) { memcpy (str, "š", 2); return; } | ||
| 1138 | if (!memcmp (str, "Ž", 2)) { memcpy (str, "ž", 2); return; } | ||
| 1139 | if (!memcmp (str, "Á", 2)) { memcpy (str, "á", 2); return; } | ||
| 1140 | if (!memcmp (str, "Ó", 2)) { memcpy (str, "ó", 2); return; } | ||
| 1141 | if (!memcmp (str, "Ú", 2)) { memcpy (str, "ú", 2); return; } | ||
| 1142 | if (!memcmp (str, "É", 2)) { memcpy (str, "é", 2); return; } | ||
| 1143 | if (!memcmp (str, "Í", 2)) { memcpy (str, "í", 2); return; } | ||
| 1144 | if (!memcmp (str, "Ý", 2)) { memcpy (str, "ý", 2); return; } | ||
| 1145 | if (!memcmp (str, "Ŕ", 2)) { memcpy (str, "ŕ", 2); return; } | ||
| 1146 | if (!memcmp (str, "Ĺ", 2)) { memcpy (str, "ĺ", 2); return; } | ||
| 1147 | if (!memcmp (str, "Ô", 2)) { memcpy (str, "ô", 2); return; } | ||
| 1148 | if (!memcmp (str, "Ä", 2)) { memcpy (str, "ä", 2); return; } | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | int | ||
| 1152 | main (int argc, char *argv[]) | ||
| 1153 | { | ||
| 1154 | const char *progname = *argv++; | ||
| 1155 | if (argc < 2) DIE ("Usage: %s file", progname); | ||
| 1156 | |||
| 1157 | const char *filename = *argv++; | ||
| 1158 | struct string_view file_contents = load_file (filename); | ||
| 1159 | |||
| 1160 | struct bnf_lexer lexer = bnf_lexer_from_string_view (file_contents, filename); | ||
| 1161 | struct bnf_syntax syntax = bnf_parse_syntax (&lexer); | ||
| 1162 | assert (bnf_check_syntax (&lexer, syntax)); // check if grammar has rules for all nonterminals | ||
| 1163 | |||
| 1164 | // save_dot (syntax); | ||
| 1165 | // print_bnf (syntax); | ||
| 1166 | |||
| 1167 | int seed = time (NULL); | ||
| 1168 | // seed = 0; | ||
| 1169 | srand (seed); | ||
| 1170 | |||
| 1171 | printf ("Correct examples:\n"); | ||
| 1172 | print_random_rule (syntax, "<sentence>"); | ||
| 1173 | for (int i = 0; i < 16; ++i) { | ||
| 1174 | printf ("\n"); | ||
| 1175 | print_random_rule (syntax, "<sentence>"); | ||
| 1176 | } printf ("\n"); | ||
| 1177 | |||
| 1178 | struct grammar grammar = build_grammar (syntax); | ||
| 1179 | // puts ("build:"); print_grammar (grammar); | ||
| 1180 | // grammar_make_epsilon_free (&grammar); | ||
| 1181 | // puts ("make_epsilon_free:"); print_grammar (grammar); | ||
| 1182 | // grammar_remove_unit_productions (&grammar); | ||
| 1183 | // puts ("remove_unit_productions:"); print_grammar (grammar); | ||
| 1184 | // grammar_reduce (&grammar); // remove rules with (unreachable and unproductive) nonterminals | ||
| 1185 | // puts ("reduce:"); print_grammar (grammar); | ||
| 1186 | grammar_left_factor_common_prefixes (&grammar); | ||
| 1187 | // puts ("left_factor_common_prefixes:"); print_grammar (grammar); | ||
| 1188 | // TODO **expose indirect left recurisons | ||
| 1189 | // TODO *eliminate left recurions | ||
| 1190 | // TODO **ensure disjoint first and follow sets | ||
| 1191 | build_lookup_table (&grammar); | ||
| 1192 | |||
| 1193 | char *line = NULL; | ||
| 1194 | size_t line_size = 0; | ||
| 1195 | for (;;) { | ||
| 1196 | printf ("Enter string to parse: "); | ||
| 1197 | getline (&line, &line_size, stdin); | ||
| 1198 | size_t line_len = strlen (line); | ||
| 1199 | if (line_len == 1) break; | ||
| 1200 | line[line_len - 1] = '\0'; | ||
| 1201 | |||
| 1202 | printf ("matching: `%s'...\n", line); | ||
| 1203 | if (!check_upcase (line)) { | ||
| 1204 | puts ("didn't match!"); | ||
| 1205 | continue; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | downcase (line); | ||
| 1209 | struct tree tree = parse (grammar, line); | ||
| 1210 | if (tree.children.count == 0) | ||
| 1211 | puts ("didn't match!"); | ||
| 1212 | else { | ||
| 1213 | puts ("matched"); | ||
| 1214 | print_tree_dot (grammar, tree); | ||
| 1215 | printf ("./tmp/tree.dot:1:0: info: generated parsing tree\n"); | ||
| 1216 | } | ||
| 1217 | } puts ("Exiting..."); | ||
| 1218 | |||
| 1219 | free (line); | ||
| 1220 | DYNAMIC_ARRAY_FREE (syntax); | ||
| 1221 | STRING_BUILDER_FREE (sb); | ||
| 1222 | unload_file (file_contents); | ||
| 1223 | |||
| 1224 | return EXIT_SUCCESS; | ||
| 1225 | } | ||
diff --git a/src/slovak-adjective.bnf b/src/slovak-adjective.bnf new file mode 100644 index 0000000..4174d16 --- /dev/null +++ b/src/slovak-adjective.bnf | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | # -*- BNF -*- | ||
| 2 | # resources: | ||
| 3 | # - https://www.naturalminevita.sk/portal/slovencina/pridavne-mena-vzory-a-sklonovanie-pridavnych-mien/ | ||
| 4 | |||
| 5 | <adjectives-sg-masculine-nominative> ::= <adjective-sg-masculine-nominative> " a " <adjective-sg-masculine-nominative> | <adjective-sg-masculine-nominative> ", " <adjectives-sg-masculine-nominative> | ||
| 6 | <adjectives-sg-masculine-genitive> ::= <adjective-sg-masculine-genitive> " a " <adjective-sg-masculine-genitive> | <adjective-sg-masculine-genitive> ", " <adjectives-sg-masculine-genitive> | ||
| 7 | <adjectives-sg-masculine-dative> ::= <adjective-sg-masculine-dative> " a " <adjective-sg-masculine-dative> | <adjective-sg-masculine-dative> ", " <adjectives-sg-masculine-dative> | ||
| 8 | <adjectives-sg-masculine-accusative> ::= <adjective-sg-masculine-accusative> " a " <adjective-sg-masculine-accusative> | <adjective-sg-masculine-accusative> ", " <adjectives-sg-masculine-accusative> | ||
| 9 | <adjectives-sg-masculine-locative> ::= <adjective-sg-masculine-locative> " a " <adjective-sg-masculine-locative> | <adjective-sg-masculine-locative> ", " <adjectives-sg-masculine-locative> | ||
| 10 | <adjectives-sg-masculine-instrumental> ::= <adjective-sg-masculine-instrumental> " a " <adjective-sg-masculine-instrumental> | <adjective-sg-masculine-instrumental> ", " <adjectives-sg-masculine-instrumental> | ||
| 11 | <adjectives-sg-feminine-nominative> ::= <adjective-sg-feminine-nominative> " a " <adjective-sg-feminine-nominative> | <adjective-sg-feminine-nominative> ", " <adjectives-sg-feminine-nominative> | ||
| 12 | <adjectives-sg-feminine-genitive> ::= <adjective-sg-feminine-genitive> " a " <adjective-sg-feminine-genitive> | <adjective-sg-feminine-genitive> ", " <adjectives-sg-feminine-genitive> | ||
| 13 | <adjectives-sg-feminine-dative> ::= <adjective-sg-feminine-dative> " a " <adjective-sg-feminine-dative> | <adjective-sg-feminine-dative> ", " <adjectives-sg-feminine-dative> | ||
| 14 | <adjectives-sg-feminine-accusative> ::= <adjective-sg-feminine-accusative> " a " <adjective-sg-feminine-accusative> | <adjective-sg-feminine-accusative> ", " <adjectives-sg-feminine-accusative> | ||
| 15 | <adjectives-sg-feminine-locative> ::= <adjective-sg-feminine-locative> " a " <adjective-sg-feminine-locative> | <adjective-sg-feminine-locative> ", " <adjectives-sg-feminine-locative> | ||
| 16 | <adjectives-sg-feminine-instrumental> ::= <adjective-sg-feminine-instrumental> " a " <adjective-sg-feminine-instrumental> | <adjective-sg-feminine-instrumental> ", " <adjectives-sg-feminine-instrumental> | ||
| 17 | <adjectives-sg-neuter-nominative> ::= <adjective-sg-neuter-nominative> " a " <adjective-sg-neuter-nominative> | <adjective-sg-neuter-nominative> ", " <adjectives-sg-neuter-nominative> | ||
| 18 | <adjectives-sg-neuter-genitive> ::= <adjective-sg-neuter-genitive> " a " <adjective-sg-neuter-genitive> | <adjective-sg-neuter-genitive> ", " <adjectives-sg-neuter-genitive> | ||
| 19 | <adjectives-sg-neuter-dative> ::= <adjective-sg-neuter-dative> " a " <adjective-sg-neuter-dative> | <adjective-sg-neuter-dative> ", " <adjectives-sg-neuter-dative> | ||
| 20 | <adjectives-sg-neuter-accusative> ::= <adjective-sg-neuter-accusative> " a " <adjective-sg-neuter-accusative> | <adjective-sg-neuter-accusative> ", " <adjectives-sg-neuter-accusative> | ||
| 21 | <adjectives-sg-neuter-locative> ::= <adjective-sg-neuter-locative> " a " <adjective-sg-neuter-locative> | <adjective-sg-neuter-locative> ", " <adjectives-sg-neuter-locative> | ||
| 22 | <adjectives-sg-neuter-instrumental> ::= <adjective-sg-neuter-instrumental> " a " <adjective-sg-neuter-instrumental> | <adjective-sg-neuter-instrumental> ", " <adjectives-sg-neuter-instrumental> | ||
| 23 | <adjectives-pl-masculine-nominative> ::= <adjective-pl-masculine-nominative> " a " <adjective-pl-masculine-nominative> | <adjective-pl-masculine-nominative> ", " <adjectives-pl-masculine-nominative> | ||
| 24 | <adjectives-pl-masculine-genitive> ::= <adjective-pl-masculine-genitive> " a " <adjective-pl-masculine-genitive> | <adjective-pl-masculine-genitive> ", " <adjectives-pl-masculine-genitive> | ||
| 25 | <adjectives-pl-masculine-dative> ::= <adjective-pl-masculine-dative> " a " <adjective-pl-masculine-dative> | <adjective-pl-masculine-dative> ", " <adjectives-pl-masculine-dative> | ||
| 26 | <adjectives-pl-masculine-accusative> ::= <adjective-pl-masculine-accusative> " a " <adjective-pl-masculine-accusative> | <adjective-pl-masculine-accusative> ", " <adjectives-pl-masculine-accusative> | ||
| 27 | <adjectives-pl-masculine-locative> ::= <adjective-pl-masculine-locative> " a " <adjective-pl-masculine-locative> | <adjective-pl-masculine-locative> ", " <adjectives-pl-masculine-locative> | ||
| 28 | <adjectives-pl-masculine-instrumental> ::= <adjective-pl-masculine-instrumental> " a " <adjective-pl-masculine-instrumental> | <adjective-pl-masculine-instrumental> ", " <adjectives-pl-masculine-instrumental> | ||
| 29 | <adjectives-pl-other-nominative> ::= <adjective-pl-other-nominative> " a " <adjective-pl-other-nominative> | <adjective-pl-other-nominative> ", " <adjectives-pl-other-nominative> | ||
| 30 | <adjectives-pl-other-genitive> ::= <adjective-pl-other-genitive> " a " <adjective-pl-other-genitive> | <adjective-pl-other-genitive> ", " <adjectives-pl-other-genitive> | ||
| 31 | <adjectives-pl-other-dative> ::= <adjective-pl-other-dative> " a " <adjective-pl-other-dative> | <adjective-pl-other-dative> ", " <adjectives-pl-other-dative> | ||
| 32 | <adjectives-pl-other-accusative> ::= <adjective-pl-other-accusative> " a " <adjective-pl-other-accusative> | <adjective-pl-other-accusative> ", " <adjectives-pl-other-accusative> | ||
| 33 | <adjectives-pl-other-locative> ::= <adjective-pl-other-locative> " a " <adjective-pl-other-locative> | <adjective-pl-other-locative> ", " <adjectives-pl-other-locative> | ||
| 34 | <adjectives-pl-other-instrumental> ::= <adjective-pl-other-instrumental> " a " <adjective-pl-other-instrumental> | <adjective-pl-other-instrumental> ", " <adjectives-pl-other-instrumental> | ||
| 35 | |||
| 36 | <adjective-sg-masculine-nominative> ::= <adjective-pekny-sg-masculine-nominative> | ||
| 37 | <adjective-sg-masculine-genitive> ::= <adjective-pekny-sg-masculine-genitive> | ||
| 38 | <adjective-sg-masculine-dative> ::= <adjective-pekny-sg-masculine-dative> | ||
| 39 | <adjective-sg-masculine-accusative> ::= <adjective-pekny-sg-masculine-accusative> | ||
| 40 | <adjective-sg-masculine-locative> ::= <adjective-pekny-sg-masculine-locative> | ||
| 41 | <adjective-sg-masculine-instrumental> ::= <adjective-pekny-sg-masculine-instrumental> | ||
| 42 | <adjective-sg-feminine-nominative> ::= <adjective-pekny-sg-feminine-nominative> | ||
| 43 | <adjective-sg-feminine-genitive> ::= <adjective-pekny-sg-feminine-genitive> | ||
| 44 | <adjective-sg-feminine-dative> ::= <adjective-pekny-sg-feminine-dative> | ||
| 45 | <adjective-sg-feminine-accusative> ::= <adjective-pekny-sg-feminine-accusative> | ||
| 46 | <adjective-sg-feminine-locative> ::= <adjective-pekny-sg-feminine-locative> | ||
| 47 | <adjective-sg-feminine-instrumental> ::= <adjective-pekny-sg-feminine-instrumental> | ||
| 48 | <adjective-sg-neuter-nominative> ::= <adjective-pekny-sg-neuter-nominative> | ||
| 49 | <adjective-sg-neuter-genitive> ::= <adjective-pekny-sg-neuter-genitive> | ||
| 50 | <adjective-sg-neuter-dative> ::= <adjective-pekny-sg-neuter-dative> | ||
| 51 | <adjective-sg-neuter-accusative> ::= <adjective-pekny-sg-neuter-accusative> | ||
| 52 | <adjective-sg-neuter-locative> ::= <adjective-pekny-sg-neuter-locative> | ||
| 53 | <adjective-sg-neuter-instrumental> ::= <adjective-pekny-sg-neuter-instrumental> | ||
| 54 | <adjective-pl-masculine-nominative> ::= <adjective-pekny-pl-masculine-nominative> | ||
| 55 | <adjective-pl-masculine-genitive> ::= <adjective-pekny-pl-masculine-genitive> | ||
| 56 | <adjective-pl-masculine-dative> ::= <adjective-pekny-pl-masculine-dative> | ||
| 57 | <adjective-pl-masculine-accusative> ::= <adjective-pekny-pl-masculine-accusative> | ||
| 58 | <adjective-pl-masculine-locative> ::= <adjective-pekny-pl-masculine-locative> | ||
| 59 | <adjective-pl-masculine-instrumental> ::= <adjective-pekny-pl-masculine-instrumental> | ||
| 60 | <adjective-pl-other-nominative> ::= <adjective-pekny-pl-other-nominative> | ||
| 61 | <adjective-pl-other-genitive> ::= <adjective-pekny-pl-other-genitive> | ||
| 62 | <adjective-pl-other-dative> ::= <adjective-pekny-pl-other-dative> | ||
| 63 | <adjective-pl-other-accusative> ::= <adjective-pekny-pl-other-accusative> | ||
| 64 | <adjective-pl-other-locative> ::= <adjective-pekny-pl-other-locative> | ||
| 65 | <adjective-pl-other-instrumental> ::= <adjective-pekny-pl-other-instrumental> | ||
| 66 | |||
| 67 | <adjective-pekny-sg-masculine-nominative> ::= <adjective-stem-pekny> "ý" | ||
| 68 | <adjective-pekny-sg-masculine-genitive> ::= <adjective-stem-pekny> "ého" | ||
| 69 | <adjective-pekny-sg-masculine-dative> ::= <adjective-stem-pekny> "émy" | ||
| 70 | <adjective-pekny-sg-masculine-accusative> ::= <adjective-stem-pekny> "ého" | ||
| 71 | <adjective-pekny-sg-masculine-locative> ::= <adjective-stem-pekny> "om" | ||
| 72 | <adjective-pekny-sg-masculine-instrumental> ::= <adjective-stem-pekny> "ým" | ||
| 73 | <adjective-pekny-sg-feminine-nominative> ::= <adjective-stem-pekny> "á" | ||
| 74 | <adjective-pekny-sg-feminine-genitive> ::= <adjective-stem-pekny> "ej" | ||
| 75 | <adjective-pekny-sg-feminine-dative> ::= <adjective-stem-pekny> "ej" | ||
| 76 | <adjective-pekny-sg-feminine-accusative> ::= <adjective-stem-pekny> "ú" | ||
| 77 | <adjective-pekny-sg-feminine-locative> ::= <adjective-stem-pekny> "ej" | ||
| 78 | <adjective-pekny-sg-feminine-instrumental> ::= <adjective-stem-pekny> "ou" | ||
| 79 | <adjective-pekny-sg-neuter-nominative> ::= <adjective-stem-pekny> "é" | ||
| 80 | <adjective-pekny-sg-neuter-genitive> ::= <adjective-stem-pekny> "ého" | ||
| 81 | <adjective-pekny-sg-neuter-dative> ::= <adjective-stem-pekny> "émy" | ||
| 82 | <adjective-pekny-sg-neuter-accusative> ::= <adjective-stem-pekny> "é" | ||
| 83 | <adjective-pekny-sg-neuter-locative> ::= <adjective-stem-pekny> "om" | ||
| 84 | <adjective-pekny-sg-neuter-instrumental> ::= <adjective-stem-pekny> "ým" | ||
| 85 | <adjective-pekny-pl-masculine-nominative> ::= <adjective-stem-pekny> "í" | ||
| 86 | <adjective-pekny-pl-masculine-genitive> ::= <adjective-stem-pekny> "ých" | ||
| 87 | <adjective-pekny-pl-masculine-dative> ::= <adjective-stem-pekny> "ým" | ||
| 88 | <adjective-pekny-pl-masculine-accusative> ::= <adjective-stem-pekny> "ých" | ||
| 89 | <adjective-pekny-pl-masculine-locative> ::= <adjective-stem-pekny> "ých" | ||
| 90 | <adjective-pekny-pl-masculine-instrumental> ::= <adjective-stem-pekny> "ými" | ||
| 91 | <adjective-pekny-pl-other-nominative> ::= <adjective-stem-pekny> "é" | ||
| 92 | <adjective-pekny-pl-other-genitive> ::= <adjective-stem-pekny> "ých" | ||
| 93 | <adjective-pekny-pl-other-dative> ::= <adjective-stem-pekny> "ým" | ||
| 94 | <adjective-pekny-pl-other-accusative> ::= <adjective-stem-pekny> "é" | ||
| 95 | <adjective-pekny-pl-other-locative> ::= <adjective-stem-pekny> "ých" | ||
| 96 | <adjective-pekny-pl-other-instrumental> ::= <adjective-stem-pekny> "ými" | ||
| 97 | <adjective-stem-pekny> ::= "pekn" | ||
diff --git a/src/slovak-adverb.bnf b/src/slovak-adverb.bnf new file mode 100644 index 0000000..fab601f --- /dev/null +++ b/src/slovak-adverb.bnf | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | # -*- BNF -*- | ||
| 2 | # resources: | ||
| 3 | # - https://yourslovakteacher.com/grammar_theory/formation-of-slovak-adverbs-rules-and-examples/ | ||
diff --git a/src/slovak-noun.bnf b/src/slovak-noun.bnf new file mode 100644 index 0000000..8e81a08 --- /dev/null +++ b/src/slovak-noun.bnf | |||
| @@ -0,0 +1,321 @@ | |||
| 1 | # slovak-noun.bnf -*- BNF -*- | ||
| 2 | # resources: | ||
| 3 | # - https://www.naturalminevita.sk/portal/slovencina/podstatne-mena/ | ||
| 4 | |||
| 5 | <noun> ::= <noun-masculine> | <noun-feminine> | <noun-neuter> | ||
| 6 | <noun-sg> ::= <noun-masculine-sg> | <noun-feminine-sg> | <noun-neuter-sg> | ||
| 7 | <noun-pl> ::= <noun-masculine-pl> | <noun-feminine-pl> | <noun-neuter-pl> | ||
| 8 | |||
| 9 | <noun-sg-nominative> ::= <noun-masculine-sg-nominative> | <noun-feminine-sg-nominative> | <noun-neuter-sg-nominative> | ||
| 10 | <noun-pl-nominative> ::= <noun-masculine-pl-nominative> | <noun-feminine-pl-nominative> | <noun-neuter-pl-nominative> | ||
| 11 | |||
| 12 | <noun-masculine> ::= <noun-masculine-sg> | <noun-masculine-pl> | ||
| 13 | <noun-masculine-sg> ::= <noun-masculine-sg-nominative> | <noun-masculine-sg-genitive> | <noun-masculine-sg-dative> | <noun-masculine-sg-accusative> | <noun-masculine-sg-locative> | <noun-masculine-sg-instrumental> | ||
| 14 | <noun-masculine-pl> ::= <noun-masculine-pl-nominative> | <noun-masculine-pl-genitive> | <noun-masculine-pl-dative> | <noun-masculine-pl-accusative> | <noun-masculine-pl-locative> | <noun-masculine-pl-instrumental> | ||
| 15 | <noun-masculine-sg-nominative> ::= <noun-chlap-sg-nominative> | <noun-hrdina-sg-nominative> | <noun-dub-sg-nominative> | <noun-stroj-sg-nominative> | <noun-kuli-sg-nominative> | ||
| 16 | <noun-masculine-sg-genitive> ::= <noun-chlap-sg-genitive> | <noun-hrdina-sg-genitive> | <noun-dub-sg-genitive> | <noun-stroj-sg-genitive> | <noun-kuli-sg-genitive> | ||
| 17 | <noun-masculine-sg-dative> ::= <noun-chlap-sg-dative> | <noun-hrdina-sg-dative> | <noun-dub-sg-dative> | <noun-stroj-sg-dative> | <noun-kuli-sg-dative> | ||
| 18 | <noun-masculine-sg-accusative> ::= <noun-chlap-sg-accusative> | <noun-hrdina-sg-accusative> | <noun-dub-sg-accusative> | <noun-stroj-sg-accusative> | <noun-kuli-sg-accusative> | ||
| 19 | <noun-masculine-sg-locative> ::= <noun-chlap-sg-locative> | <noun-hrdina-sg-locative> | <noun-dub-sg-locative> | <noun-stroj-sg-locative> | <noun-kuli-sg-locative> | ||
| 20 | <noun-masculine-sg-instrumental> ::= <noun-chlap-sg-instrumental> | <noun-hrdina-sg-instrumental> | <noun-dub-sg-instrumental> | <noun-stroj-sg-instrumental> | <noun-kuli-sg-instrumental> | ||
| 21 | <noun-masculine-pl-nominative> ::= <noun-chlap-pl-nominative> | <noun-hrdina-pl-nominative> | <noun-dub-pl-nominative> | <noun-stroj-pl-nominative> | <noun-kuli-pl-nominative> | ||
| 22 | <noun-masculine-pl-genitive> ::= <noun-chlap-pl-genitive> | <noun-hrdina-pl-genitive> | <noun-dub-pl-genitive> | <noun-stroj-pl-genitive> | <noun-kuli-pl-genitive> | ||
| 23 | <noun-masculine-pl-dative> ::= <noun-chlap-pl-dative> | <noun-hrdina-pl-dative> | <noun-dub-pl-dative> | <noun-stroj-pl-dative> | <noun-kuli-pl-dative> | ||
| 24 | <noun-masculine-pl-accusative> ::= <noun-chlap-pl-accusative> | <noun-hrdina-pl-accusative> | <noun-dub-pl-accusative> | <noun-stroj-pl-accusative> | <noun-kuli-pl-accusative> | ||
| 25 | <noun-masculine-pl-locative> ::= <noun-chlap-pl-locative> | <noun-hrdina-pl-locative> | <noun-dub-pl-locative> | <noun-stroj-pl-locative> | <noun-kuli-pl-locative> | ||
| 26 | <noun-masculine-pl-instrumental> ::= <noun-chlap-pl-instrumental> | <noun-hrdina-pl-instrumental> | <noun-dub-pl-instrumental> | <noun-stroj-pl-instrumental> | <noun-kuli-pl-instrumental> | ||
| 27 | |||
| 28 | <noun-chlap> ::= <noun-chlap-sg> | <noun-chlap-pl> | ||
| 29 | <noun-chlap-sg> ::= <noun-chlap-sg-nominative> | <noun-chlap-sg-genitive> | <noun-chlap-sg-dative> | <noun-chlap-sg-accusative> | <noun-chlap-sg-locative> | <noun-chlap-sg-instrumental> | ||
| 30 | <noun-chlap-pl> ::= <noun-chlap-pl-nominative> | <noun-chlap-pl-genitive> | <noun-chlap-pl-dative> | <noun-chlap-pl-accusative> | <noun-chlap-pl-locative> | <noun-chlap-pl-instrumental> | ||
| 31 | <noun-chlap-sg-nominative> ::= <noun-stem-chlap-i> | <noun-stem-chlap-ia> | <noun-stem-chlap-ovia> "o" | ||
| 32 | <noun-chlap-sg-genitive> ::= <noun-stem-chlap> "a" | ||
| 33 | <noun-chlap-sg-dative> ::= <noun-stem-chlap> "ovi" | ||
| 34 | <noun-chlap-sg-accusative> ::= <noun-stem-chlap> "a" | ||
| 35 | <noun-chlap-sg-locative> ::= <noun-stem-chlap> "ovi" | ||
| 36 | <noun-chlap-sg-instrumental> ::= <noun-stem-chlap> "om" | ||
| 37 | <noun-chlap-pl-nominative> ::= <noun-stem-chlap-i> "i" | <noun-stem-chlap-ia> "ia" | <noun-stem-chlap-ovia> "ovia" | ||
| 38 | <noun-chlap-pl-genitive> ::= <noun-stem-chlap> "ov" | ||
| 39 | <noun-chlap-pl-dative> ::= <noun-stem-chlap> "om" | ||
| 40 | <noun-chlap-pl-accusative> ::= <noun-stem-chlap> "ov" | ||
| 41 | <noun-chlap-pl-locative> ::= <noun-stem-chlap> "och" | ||
| 42 | <noun-chlap-pl-instrumental> ::= <noun-stem-chlap> "mi" | ||
| 43 | <noun-stem-chlap> ::= <noun-stem-chlap-i> | <noun-stem-chlap-ia> | <noun-stem-chlap-ovia> | ||
| 44 | <noun-stem-chlap-i> ::= "chlap" | "rytier" | # "chlapec" | "otec" | "pes" | ||
| 45 | <noun-stem-chlap-ia> ::= "brat" # | "učiteľ" | ||
| 46 | <noun-stem-chlap-ovia> ::= "uj" | "ded" | "Pal" | "strýk" # | "syn" | ||
| 47 | |||
| 48 | <noun-hrdina> ::= <noun-hrdina-sg> | <noun-hrdina-pl> | ||
| 49 | <noun-hrdina-sg> ::= <noun-hrdina-sg-nominative> | <noun-hrdina-sg-genitive> | <noun-hrdina-sg-dative> | <noun-hrdina-sg-accusative> | <noun-hrdina-sg-locative> | <noun-hrdina-sg-instrumental> | ||
| 50 | <noun-hrdina-pl> ::= <noun-hrdina-pl-nominative> | <noun-hrdina-pl-genitive> | <noun-hrdina-pl-dative> | <noun-hrdina-pl-accusative> | <noun-hrdina-pl-locative> | <noun-hrdina-pl-instrumental> | ||
| 51 | <noun-hrdina-sg-nominative> ::= <noun-stem-hrdina-a> "a" | <noun-stem-hrdina-o> "o" | <noun-stem-hrdina-ista> "ista" | ||
| 52 | <noun-hrdina-sg-genitive> ::= <noun-stem-hrdina> "u" | ||
| 53 | <noun-hrdina-sg-dative> ::= <noun-stem-hrdina> "ovi" | ||
| 54 | <noun-hrdina-sg-accusative> ::= <noun-stem-hrdina> "u" | ||
| 55 | <noun-hrdina-sg-locative> ::= <noun-stem-hrdina> "ovi" | ||
| 56 | <noun-hrdina-sg-instrumental> ::= <noun-stem-hrdina> "om" | ||
| 57 | <noun-hrdina-pl-nominative> ::= <noun-stem-hrdina-a> "ovia" | <noun-stem-hrdina-o> "ovia" | <noun-stem-hrdina-ista> "i" | ||
| 58 | <noun-hrdina-pl-genitive> ::= <noun-stem-hrdina> "ov" | ||
| 59 | <noun-hrdina-pl-dative> ::= <noun-stem-hrdina> "om" | ||
| 60 | <noun-hrdina-pl-accusative> ::= <noun-stem-hrdina> "ov" | ||
| 61 | <noun-hrdina-pl-locative> ::= <noun-stem-hrdina> "och" | ||
| 62 | <noun-hrdina-pl-instrumental> ::= <noun-stem-hrdina> "ami" | ||
| 63 | <noun-stem-hrdina> ::= <noun-stem-hrdina-a> | <noun-stem-hrdina-o> | <noun-stem-hrdina-ista> | ||
| 64 | <noun-stem-hrdina-a> ::= "hrdin" | "sluh" | "sudc" | ||
| 65 | <noun-stem-hrdina-o> ::= "Krask" | ||
| 66 | <noun-stem-hrdina-ista> ::= "husl" | ||
| 67 | |||
| 68 | <noun-dub> ::= <noun-dub-sg> | <noun-dub-pl> | ||
| 69 | <noun-dub-sg> ::= <noun-dub-sg-nominative> | <noun-dub-sg-genitive> | <noun-dub-sg-dative> | <noun-dub-sg-accusative> | <noun-dub-sg-locative> | <noun-dub-sg-instrumental> | ||
| 70 | <noun-dub-pl> ::= <noun-dub-pl-nominative> | <noun-dub-pl-genitive> | <noun-dub-pl-dative> | <noun-dub-pl-accusative> | <noun-dub-pl-locative> | <noun-dub-pl-instrumental> | ||
| 71 | <noun-dub-sg-nominative> ::= <noun-stem-dub> "" | ||
| 72 | <noun-dub-sg-genitive> ::= <noun-stem-dub> "a" | ||
| 73 | <noun-dub-sg-dative> ::= <noun-stem-dub> "u" | ||
| 74 | <noun-dub-sg-accusative> ::= <noun-stem-dub> "" | ||
| 75 | <noun-dub-sg-locative> ::= <noun-stem-dub> "e" | ||
| 76 | <noun-dub-sg-instrumental> ::= <noun-stem-dub> "om" | ||
| 77 | <noun-dub-pl-nominative> ::= <noun-stem-dub> "y" | ||
| 78 | <noun-dub-pl-genitive> ::= <noun-stem-dub> "ov" | ||
| 79 | <noun-dub-pl-dative> ::= <noun-stem-dub> "om" | ||
| 80 | <noun-dub-pl-accusative> ::= <noun-stem-dub> "y" | ||
| 81 | <noun-dub-pl-locative> ::= <noun-stem-dub> "och" | ||
| 82 | <noun-dub-pl-instrumental> ::= <noun-stem-dub> "i" | ||
| 83 | <noun-stem-dub> ::= "dub" | "dom" | "strom" | "vodopád" | "obraz" | "autobus" | "trolejbus" | "pot" | ||
| 84 | |||
| 85 | <noun-stroj> ::= <noun-stroj-sg> | <noun-stroj-pl> | ||
| 86 | <noun-stroj-sg> ::= <noun-stroj-sg-nominative> | <noun-stroj-sg-genitive> | <noun-stroj-sg-dative> | <noun-stroj-sg-accusative> | <noun-stroj-sg-locative> | <noun-stroj-sg-instrumental> | ||
| 87 | <noun-stroj-pl> ::= <noun-stroj-pl-nominative> | <noun-stroj-pl-genitive> | <noun-stroj-pl-dative> | <noun-stroj-pl-accusative> | <noun-stroj-pl-locative> | <noun-stroj-pl-instrumental> | ||
| 88 | <noun-stroj-sg-nominative> ::= <noun-stem-stroj> "" | ||
| 89 | <noun-stroj-sg-genitive> ::= <noun-stem-stroj> "a" | ||
| 90 | <noun-stroj-sg-dative> ::= <noun-stem-stroj> "u" | ||
| 91 | <noun-stroj-sg-accusative> ::= <noun-stem-stroj> "" | ||
| 92 | <noun-stroj-sg-locative> ::= <noun-stem-stroj> "i" | ||
| 93 | <noun-stroj-sg-instrumental> ::= <noun-stem-stroj> "om" | ||
| 94 | <noun-stroj-pl-nominative> ::= <noun-stem-stroj> "e" | ||
| 95 | <noun-stroj-pl-genitive> ::= <noun-stem-stroj> "ov" | ||
| 96 | <noun-stroj-pl-dative> ::= <noun-stem-stroj> "om" | ||
| 97 | <noun-stroj-pl-accusative> ::= <noun-stem-stroj> "e" | ||
| 98 | <noun-stroj-pl-locative> ::= <noun-stem-stroj> "och" | ||
| 99 | <noun-stroj-pl-instrumental> ::= <noun-stem-stroj> "mi" | ||
| 100 | <noun-stem-stroj> ::= "stroj" | "počítač" | "dážď" | "máj" | "kameň" | "kôš" | "nôž" | "terč" | "olej" | "čaj" # | "oheň" | ||
| 101 | |||
| 102 | <noun-kuli> ::= <noun-kuli-sg> | <noun-kuli-pl> | ||
| 103 | <noun-kuli-sg> ::= <noun-kuli-sg-nominative> | <noun-kuli-sg-genitive> | <noun-kuli-sg-dative> | <noun-kuli-sg-accusative> | <noun-kuli-sg-locative> | <noun-kuli-sg-instrumental> | ||
| 104 | <noun-kuli-pl> ::= <noun-kuli-pl-nominative> | <noun-kuli-pl-genitive> | <noun-kuli-pl-dative> | <noun-kuli-pl-accusative> | <noun-kuli-pl-locative> | <noun-kuli-pl-instrumental> | ||
| 105 | <noun-kuli-sg-nominative> ::= <noun-stem-kuli> "" | ||
| 106 | <noun-kuli-sg-genitive> ::= <noun-stem-kuli> "ho" | ||
| 107 | <noun-kuli-sg-dative> ::= <noun-stem-kuli> "mu" | ||
| 108 | <noun-kuli-sg-accusative> ::= <noun-stem-kuli> "ho" | ||
| 109 | <noun-kuli-sg-locative> ::= <noun-stem-kuli> "m" | ||
| 110 | <noun-kuli-sg-instrumental> ::= <noun-stem-kuli> "m" | ||
| 111 | <noun-kuli-pl-nominative> ::= <noun-stem-kuli> "ovia" | ||
| 112 | <noun-kuli-pl-genitive> ::= <noun-stem-kuli> "ov" | ||
| 113 | <noun-kuli-pl-dative> ::= <noun-stem-kuli> "om" | ||
| 114 | <noun-kuli-pl-accusative> ::= <noun-stem-kuli> "ob" | ||
| 115 | <noun-kuli-pl-locative> ::= <noun-stem-kuli> "och" | ||
| 116 | <noun-kuli-pl-instrumental> ::= <noun-stem-kuli> "ami" | ||
| 117 | <noun-stem-kuli> ::= "kuli" | "pony" | "grizly" | "Goethe" | "Pálfy" | "Škultéty" | "Goethe" | "Krčméry" | ||
| 118 | |||
| 119 | <noun-feminine> ::= <noun-feminine-sg> | <noun-feminine-pl> | ||
| 120 | <noun-feminine-sg> ::= <noun-feminine-sg-nominative> | <noun-feminine-sg-genitive> | <noun-feminine-sg-dative> | <noun-feminine-sg-accusative> | <noun-feminine-sg-locative> | <noun-feminine-sg-instrumental> | ||
| 121 | <noun-feminine-pl> ::= <noun-feminine-pl-nominative> | <noun-feminine-pl-genitive> | <noun-feminine-pl-dative> | <noun-feminine-pl-accusative> | <noun-feminine-pl-locative> | <noun-feminine-pl-instrumental> | ||
| 122 | <noun-feminine-sg-nominative> ::= <noun-zena-sg-nominative> | <noun-ulica-sg-nominative> | <noun-dlan-sg-nominative> | <noun-kost-sg-nominative> | <noun-gazdina-sg-nominative> | <noun-idea-sg-nominative> | ||
| 123 | <noun-feminine-sg-genitive> ::= <noun-zena-sg-genitive> | <noun-ulica-sg-genitive> | <noun-dlan-sg-genitive> | <noun-kost-sg-genitive> | <noun-gazdina-sg-genitive> | <noun-idea-sg-genitive> | ||
| 124 | <noun-feminine-sg-dative> ::= <noun-zena-sg-dative> | <noun-ulica-sg-dative> | <noun-dlan-sg-dative> | <noun-kost-sg-dative> | <noun-gazdina-sg-dative> | <noun-idea-sg-dative> | ||
| 125 | <noun-feminine-sg-accusative> ::= <noun-zena-sg-accusative> | <noun-ulica-sg-accusative> | <noun-dlan-sg-accusative> | <noun-kost-sg-accusative> | <noun-gazdina-sg-accusative> | <noun-idea-sg-accusative> | ||
| 126 | <noun-feminine-sg-locative> ::= <noun-zena-sg-locative> | <noun-ulica-sg-locative> | <noun-dlan-sg-locative> | <noun-kost-sg-locative> | <noun-gazdina-sg-locative> | <noun-idea-sg-locative> | ||
| 127 | <noun-feminine-sg-instrumental> ::= <noun-zena-sg-instrumental> | <noun-ulica-sg-instrumental> | <noun-dlan-sg-instrumental> | <noun-kost-sg-instrumental> | <noun-gazdina-sg-instrumental> | <noun-idea-sg-instrumental> | ||
| 128 | <noun-feminine-pl-nominative> ::= <noun-zena-pl-nominative> | <noun-ulica-pl-nominative> | <noun-dlan-pl-nominative> | <noun-kost-pl-nominative> | <noun-gazdina-pl-nominative> | <noun-idea-pl-nominative> | ||
| 129 | <noun-feminine-pl-genitive> ::= <noun-zena-pl-genitive> | <noun-ulica-pl-genitive> | <noun-dlan-pl-genitive> | <noun-kost-pl-genitive> | <noun-gazdina-pl-genitive> | <noun-idea-pl-genitive> | ||
| 130 | <noun-feminine-pl-dative> ::= <noun-zena-pl-dative> | <noun-ulica-pl-dative> | <noun-dlan-pl-dative> | <noun-kost-pl-dative> | <noun-gazdina-pl-dative> | <noun-idea-pl-dative> | ||
| 131 | <noun-feminine-pl-accusative> ::= <noun-zena-pl-accusative> | <noun-ulica-pl-accusative> | <noun-dlan-pl-accusative> | <noun-kost-pl-accusative> | <noun-gazdina-pl-accusative> | <noun-idea-pl-accusative> | ||
| 132 | <noun-feminine-pl-locative> ::= <noun-zena-pl-locative> | <noun-ulica-pl-locative> | <noun-dlan-pl-locative> | <noun-kost-pl-locative> | <noun-gazdina-pl-locative> | <noun-idea-pl-locative> | ||
| 133 | <noun-feminine-pl-instrumental> ::= <noun-zena-pl-instrumental> | <noun-ulica-pl-instrumental> | <noun-dlan-pl-instrumental> | <noun-kost-pl-instrumental> | <noun-gazdina-pl-instrumental> | <noun-idea-pl-instrumental> | ||
| 134 | |||
| 135 | <noun-zena> ::= <noun-zena-sg> | <noun-zena-pl> | ||
| 136 | <noun-zena-sg> ::= <noun-zena-sg-nominative> | <noun-zena-sg-genitive> | <noun-zena-sg-dative> | <noun-zena-sg-accusative> | <noun-zena-sg-locative> | <noun-zena-sg-instrumental> | ||
| 137 | <noun-zena-pl> ::= <noun-zena-pl-nominative> | <noun-zena-pl-genitive> | <noun-zena-pl-dative> | <noun-zena-pl-accusative> | <noun-zena-pl-locative> | <noun-zena-pl-instrumental> | ||
| 138 | <noun-zena-sg-nominative> ::= <noun-stem-zena> "a" | ||
| 139 | <noun-zena-sg-genitive> ::= <noun-stem-zena> "y" | ||
| 140 | <noun-zena-sg-dative> ::= <noun-stem-zena> "e" | ||
| 141 | <noun-zena-sg-accusative> ::= <noun-stem-zena> "y" | ||
| 142 | <noun-zena-sg-locative> ::= <noun-stem-zena> "e" | ||
| 143 | <noun-zena-sg-instrumental> ::= <noun-stem-zena> "ou" | ||
| 144 | <noun-zena-pl-nominative> ::= <noun-stem-zena> "y" | ||
| 145 | <noun-zena-pl-genitive> ::= <noun-stem-zena> "í" # meh | ||
| 146 | <noun-zena-pl-dative> ::= <noun-stem-zena> "ám" | ||
| 147 | <noun-zena-pl-accusative> ::= <noun-stem-zena> "y" | ||
| 148 | <noun-zena-pl-locative> ::= <noun-stem-zena> "ách" | ||
| 149 | <noun-zena-pl-instrumental> ::= <noun-stem-zena> "ami" | ||
| 150 | <noun-stem-zena> ::= "žen" | "dcér" | "mam" |"vnučk" | "rodin" | "knih" | "cest" | "vod" | "ruk" | "noh" | "hlav" | "stred" | "sobot" | ||
| 151 | |||
| 152 | <noun-ulica> ::= <noun-ulica-sg> | <noun-ulica-pl> | ||
| 153 | <noun-ulica-sg> ::= <noun-ulica-sg-nominative> | <noun-ulica-sg-genitive> | <noun-ulica-sg-dative> | <noun-ulica-sg-accusative> | <noun-ulica-sg-locative> | <noun-ulica-sg-instrumental> | ||
| 154 | <noun-ulica-pl> ::= <noun-ulica-pl-nominative> | <noun-ulica-pl-genitive> | <noun-ulica-pl-dative> | <noun-ulica-pl-accusative> | <noun-ulica-pl-locative> | <noun-ulica-pl-instrumental> | ||
| 155 | <noun-ulica-sg-nominative> ::= <noun-stem-ulica> "a" | ||
| 156 | <noun-ulica-sg-genitive> ::= <noun-stem-ulica> "e" | ||
| 157 | <noun-ulica-sg-dative> ::= <noun-stem-ulica> "i" | ||
| 158 | <noun-ulica-sg-accusative> ::= <noun-stem-ulica> "u" | ||
| 159 | <noun-ulica-sg-locative> ::= <noun-stem-ulica> "i" | ||
| 160 | <noun-ulica-sg-instrumental> ::= <noun-stem-ulica> "ou" | ||
| 161 | <noun-ulica-pl-nominative> ::= <noun-stem-ulica> "e" | ||
| 162 | <noun-ulica-pl-genitive> ::= <noun-stem-ulica> "íc" # ulíc | ||
| 163 | <noun-ulica-pl-dative> ::= <noun-stem-ulica> "iam" | ||
| 164 | <noun-ulica-pl-accusative> ::= <noun-stem-ulica> "e" | ||
| 165 | <noun-ulica-pl-locative> ::= <noun-stem-ulica> "iach" | ||
| 166 | <noun-ulica-pl-instrumental> ::= <noun-stem-ulica> "ami" | ||
| 167 | <noun-stem-ulica> ::= "ulic" | "lodenic" | "vež" | "košeľ" | "prác" | "stanic" | "schôdz" | "tabuľ" | "ruž" | "cibuľ" | "vaň" | "fľaš" | "nedeľ" | "chvíľ" | ||
| 168 | |||
| 169 | <noun-dlan> ::= <noun-dlan-sg> | <noun-dlan-pl> | ||
| 170 | <noun-dlan-sg> ::= <noun-dlan-sg-nominative> | <noun-dlan-sg-genitive> | <noun-dlan-sg-dative> | <noun-dlan-sg-accusative> | <noun-dlan-sg-locative> | <noun-dlan-sg-instrumental> | ||
| 171 | <noun-dlan-pl> ::= <noun-dlan-pl-nominative> | <noun-dlan-pl-genitive> | <noun-dlan-pl-dative> | <noun-dlan-pl-accusative> | <noun-dlan-pl-locative> | <noun-dlan-pl-instrumental> | ||
| 172 | <noun-dlan-sg-nominative> ::= <noun-stem-dlan> "" | ||
| 173 | <noun-dlan-sg-genitive> ::= <noun-stem-dlan> "e" | ||
| 174 | <noun-dlan-sg-dative> ::= <noun-stem-dlan> "i" | ||
| 175 | <noun-dlan-sg-accusative> ::= <noun-stem-dlan> "" | ||
| 176 | <noun-dlan-sg-locative> ::= <noun-stem-dlan> "i" | ||
| 177 | <noun-dlan-sg-instrumental> ::= <noun-stem-dlan> "ou" | ||
| 178 | <noun-dlan-pl-nominative> ::= <noun-stem-dlan> "e" | ||
| 179 | <noun-dlan-pl-genitive> ::= <noun-stem-dlan> "í" | ||
| 180 | <noun-dlan-pl-dative> ::= <noun-stem-dlan> "iam" | ||
| 181 | <noun-dlan-pl-accusative> ::= <noun-stem-dlan> "e" | ||
| 182 | <noun-dlan-pl-locative> ::= <noun-stem-dlan> "iach" | ||
| 183 | <noun-dlan-pl-instrumental> ::= <noun-stem-dlan> "ami" | ||
| 184 | <noun-stem-dlan> ::= "dlaň" | "púšť" | "čakáreň" | "pec" | "pieseň" | "loď" | "báseň" | "reportáž" | "posteľ" | "predpoveď" | ||
| 185 | |||
| 186 | <noun-kost> ::= <noun-kost-sg> | <noun-kost-pl> | ||
| 187 | <noun-kost-sg> ::= <noun-kost-sg-nominative> | <noun-kost-sg-genitive> | <noun-kost-sg-dative> | <noun-kost-sg-accusative> | <noun-kost-sg-locative> | <noun-kost-sg-instrumental> | ||
| 188 | <noun-kost-pl> ::= <noun-kost-pl-nominative> | <noun-kost-pl-genitive> | <noun-kost-pl-dative> | <noun-kost-pl-accusative> | <noun-kost-pl-locative> | <noun-kost-pl-instrumental> | ||
| 189 | <noun-kost-sg-nominative> ::= <noun-stem-kost> "" | ||
| 190 | <noun-kost-sg-genitive> ::= <noun-stem-kost> "i" | ||
| 191 | <noun-kost-sg-dative> ::= <noun-stem-kost> "i" | ||
| 192 | <noun-kost-sg-accusative> ::= <noun-stem-kost> "" | ||
| 193 | <noun-kost-sg-locative> ::= <noun-stem-kost> "i" | ||
| 194 | <noun-kost-sg-instrumental> ::= <noun-stem-kost> "ou" | ||
| 195 | <noun-kost-pl-nominative> ::= <noun-stem-kost> "i" | ||
| 196 | <noun-kost-pl-genitive> ::= <noun-stem-kost> "í" | ||
| 197 | <noun-kost-pl-dative> ::= <noun-stem-kost> "iam" | ||
| 198 | <noun-kost-pl-accusative> ::= <noun-stem-kost> "i" | ||
| 199 | <noun-kost-pl-locative> ::= <noun-stem-kost> "iach" | ||
| 200 | <noun-kost-pl-instrumental> ::= <noun-stem-kost> "ami" | ||
| 201 | <noun-stem-kost> ::= "kosť" | "radosť" | "vec" | "noc" | "step" | "pomoc" | "zmes" | "krv" | "tvrdosť" | ||
| 202 | |||
| 203 | <noun-gazdina> ::= <noun-gazdina-sg> | <noun-gazdina-pl> | ||
| 204 | <noun-gazdina-sg> ::= <noun-gazdina-sg-nominative> | <noun-gazdina-sg-genitive> | <noun-gazdina-sg-dative> | <noun-gazdina-sg-accusative> | <noun-gazdina-sg-locative> | <noun-gazdina-sg-instrumental> | ||
| 205 | <noun-gazdina-pl> ::= <noun-gazdina-pl-nominative> | <noun-gazdina-pl-genitive> | <noun-gazdina-pl-dative> | <noun-gazdina-pl-accusative> | <noun-gazdina-pl-locative> | <noun-gazdina-pl-instrumental> | ||
| 206 | <noun-gazdina-sg-nominative> ::= <noun-stem-gazdina> "á" | ||
| 207 | <noun-gazdina-sg-genitive> ::= <noun-stem-gazdina> "ej" | ||
| 208 | <noun-gazdina-sg-dative> ::= <noun-stem-gazdina> "ej" | ||
| 209 | <noun-gazdina-sg-accusative> ::= <noun-stem-gazdina> "ú" | ||
| 210 | <noun-gazdina-sg-locative> ::= <noun-stem-gazdina> "ej" | ||
| 211 | <noun-gazdina-sg-instrumental> ::= <noun-stem-gazdina> "ou" | ||
| 212 | <noun-gazdina-pl-nominative> ::= <noun-stem-gazdina> "é" | ||
| 213 | <noun-gazdina-pl-genitive> ::= <noun-stem-gazdina> "ín" # meh | ||
| 214 | <noun-gazdina-pl-dative> ::= <noun-stem-gazdina> "ám" | ||
| 215 | <noun-gazdina-pl-accusative> ::= <noun-stem-gazdina> "é" | ||
| 216 | <noun-gazdina-pl-locative> ::= <noun-stem-gazdina> "ách" | ||
| 217 | <noun-gazdina-pl-instrumental> ::= <noun-stem-gazdina> "ami" | ||
| 218 | <noun-stem-gazdina> ::= "gazdin" | "princezn" | "kráľovn" | "cisárovn" | "šľachtičn" | "kňažn" | ||
| 219 | |||
| 220 | <noun-idea> ::= <noun-idea-sg> | <noun-idea-pl> | ||
| 221 | <noun-idea-sg> ::= <noun-idea-sg-nominative> | <noun-idea-sg-genitive> | <noun-idea-sg-dative> | <noun-idea-sg-accusative> | <noun-idea-sg-locative> | <noun-idea-sg-instrumental> | ||
| 222 | <noun-idea-pl> ::= <noun-idea-pl-nominative> | <noun-idea-pl-genitive> | <noun-idea-pl-dative> | <noun-idea-pl-accusative> | <noun-idea-pl-locative> | <noun-idea-pl-instrumental> | ||
| 223 | <noun-idea-sg-nominative> ::= <noun-stem-idea> "a" | ||
| 224 | <noun-idea-sg-genitive> ::= <noun-stem-idea> "y" | ||
| 225 | <noun-idea-sg-dative> ::= <noun-stem-idea> "i" | ||
| 226 | <noun-idea-sg-accusative> ::= <noun-stem-idea> "u" | ||
| 227 | <noun-idea-sg-locative> ::= <noun-stem-idea> "i" | ||
| 228 | <noun-idea-sg-instrumental> ::= <noun-stem-idea> "ou" | ||
| 229 | <noun-idea-pl-nominative> ::= <noun-stem-idea> "y" | ||
| 230 | <noun-idea-pl-genitive> ::= <noun-stem-idea> "í" | ||
| 231 | <noun-idea-pl-dative> ::= <noun-stem-idea> "ám" | ||
| 232 | <noun-idea-pl-accusative> ::= <noun-stem-idea> "y" | ||
| 233 | <noun-idea-pl-locative> ::= <noun-stem-idea> "ách" | ||
| 234 | <noun-idea-pl-instrumental> ::= <noun-stem-idea> "ami" | ||
| 235 | <noun-stem-idea> ::= "ide" | "orchide" | "Nikaragu" | "Ike" | "Andre" | "alo" | ||
| 236 | |||
| 237 | <noun-neuter> ::= <noun-neuter-sg> | <noun-neuter-pl> | ||
| 238 | <noun-neuter-sg> ::= <noun-neuter-sg-nominative> | <noun-neuter-sg-genitive> | <noun-neuter-sg-dative> | <noun-neuter-sg-accusative> | <noun-neuter-sg-locative> | <noun-neuter-sg-instrumental> | ||
| 239 | <noun-neuter-pl> ::= <noun-neuter-pl-nominative> | <noun-neuter-pl-genitive> | <noun-neuter-pl-dative> | <noun-neuter-pl-accusative> | <noun-neuter-pl-locative> | <noun-neuter-pl-instrumental> | ||
| 240 | <noun-neuter-sg-nominative> ::= <noun-mesto-sg-nominative> | <noun-srdce-sg-nominative> | <noun-vysvedcenie-sg-nominative> | <noun-dievca-sg-nominative> | ||
| 241 | <noun-neuter-sg-genitive> ::= <noun-mesto-sg-genitive> | <noun-srdce-sg-genitive> | <noun-vysvedcenie-sg-genitive> | <noun-dievca-sg-genitive> | ||
| 242 | <noun-neuter-sg-dative> ::= <noun-mesto-sg-dative> | <noun-srdce-sg-dative> | <noun-vysvedcenie-sg-dative> | <noun-dievca-sg-dative> | ||
| 243 | <noun-neuter-sg-accusative> ::= <noun-mesto-sg-accusative> | <noun-srdce-sg-accusative> | <noun-vysvedcenie-sg-accusative> | <noun-dievca-sg-accusative> | ||
| 244 | <noun-neuter-sg-locative> ::= <noun-mesto-sg-locative> | <noun-srdce-sg-locative> | <noun-vysvedcenie-sg-locative> | <noun-dievca-sg-locative> | ||
| 245 | <noun-neuter-sg-instrumental> ::= <noun-mesto-sg-instrumental> | <noun-srdce-sg-instrumental> | <noun-vysvedcenie-sg-instrumental> | <noun-dievca-sg-instrumental> | ||
| 246 | <noun-neuter-pl-nominative> ::= <noun-mesto-pl-nominative> | <noun-srdce-pl-nominative> | <noun-vysvedcenie-pl-nominative> | <noun-dievca-pl-nominative> | ||
| 247 | <noun-neuter-pl-genitive> ::= <noun-mesto-pl-genitive> | <noun-srdce-pl-genitive> | <noun-vysvedcenie-pl-genitive> | <noun-dievca-pl-genitive> | ||
| 248 | <noun-neuter-pl-dative> ::= <noun-mesto-pl-dative> | <noun-srdce-pl-dative> | <noun-vysvedcenie-pl-dative> | <noun-dievca-pl-dative> | ||
| 249 | <noun-neuter-pl-accusative> ::= <noun-mesto-pl-accusative> | <noun-srdce-pl-accusative> | <noun-vysvedcenie-pl-accusative> | <noun-dievca-pl-accusative> | ||
| 250 | <noun-neuter-pl-locative> ::= <noun-mesto-pl-locative> | <noun-srdce-pl-locative> | <noun-vysvedcenie-pl-locative> | <noun-dievca-pl-locative> | ||
| 251 | <noun-neuter-pl-instrumental> ::= <noun-mesto-pl-instrumental> | <noun-srdce-pl-instrumental> | <noun-vysvedcenie-pl-instrumental> | <noun-dievca-pl-instrumental> | ||
| 252 | |||
| 253 | <noun-mesto> ::= <noun-mesto-sg> | <noun-mesto-pl> | ||
| 254 | <noun-mesto-sg> ::= <noun-mesto-sg-nominative> | <noun-mesto-sg-genitive> | <noun-mesto-sg-dative> | <noun-mesto-sg-accusative> | <noun-mesto-sg-locative> | <noun-mesto-sg-instrumental> | ||
| 255 | <noun-mesto-pl> ::= <noun-mesto-pl-nominative> | <noun-mesto-pl-genitive> | <noun-mesto-pl-dative> | <noun-mesto-pl-accusative> | <noun-mesto-pl-locative> | <noun-mesto-pl-instrumental> | ||
| 256 | <noun-mesto-sg-nominative> ::= <noun-stem-mesto> "o" | ||
| 257 | <noun-mesto-sg-genitive> ::= <noun-stem-mesto> "a" | ||
| 258 | <noun-mesto-sg-dative> ::= <noun-stem-mesto> "u" | ||
| 259 | <noun-mesto-sg-accusative> ::= <noun-stem-mesto> "o" | ||
| 260 | <noun-mesto-sg-locative> ::= <noun-stem-mesto> "e" # -u | ||
| 261 | <noun-mesto-sg-instrumental> ::= <noun-stem-mesto> "om" | ||
| 262 | <noun-mesto-pl-nominative> ::= <noun-stem-mesto> "á" | ||
| 263 | <noun-mesto-pl-genitive> ::= <noun-stem-mesto> "í" # miest | ||
| 264 | <noun-mesto-pl-dative> ::= <noun-stem-mesto> "ám" | ||
| 265 | <noun-mesto-pl-accusative> ::= <noun-stem-mesto> "á" | ||
| 266 | <noun-mesto-pl-locative> ::= <noun-stem-mesto> "ách" | ||
| 267 | <noun-mesto-pl-instrumental> ::= <noun-stem-mesto> "ami" | ||
| 268 | <noun-stem-mesto> ::= "mest" | "mravenisk" | "aut" | "let" | "aut" | "koles" | "jablk" | "Slovensk" | "kaka" | "drev" | "diel" | "vedr" | "ok" | "uch" | "bruch" | "mydl" | "rádi" # | "gymnázium", "štúdium" | ||
| 269 | |||
| 270 | <noun-srdce> ::= <noun-srdce-sg> | <noun-srdce-pl> | ||
| 271 | <noun-srdce-sg> ::= <noun-srdce-sg-nominative> | <noun-srdce-sg-genitive> | <noun-srdce-sg-dative> | <noun-srdce-sg-accusative> | <noun-srdce-sg-locative> | <noun-srdce-sg-instrumental> | ||
| 272 | <noun-srdce-pl> ::= <noun-srdce-pl-nominative> | <noun-srdce-pl-genitive> | <noun-srdce-pl-dative> | <noun-srdce-pl-accusative> | <noun-srdce-pl-locative> | <noun-srdce-pl-instrumental> | ||
| 273 | <noun-srdce-sg-nominative> ::= <noun-stem-srdce> "e" | ||
| 274 | <noun-srdce-sg-genitive> ::= <noun-stem-srdce> "a" | ||
| 275 | <noun-srdce-sg-dative> ::= <noun-stem-srdce> "u" | ||
| 276 | <noun-srdce-sg-accusative> ::= <noun-stem-srdce> "e" | ||
| 277 | <noun-srdce-sg-locative> ::= <noun-stem-srdce> "i" # -u | ||
| 278 | <noun-srdce-sg-instrumental> ::= <noun-stem-srdce> "om" | ||
| 279 | <noun-srdce-pl-nominative> ::= <noun-stem-srdce> "ia" | ||
| 280 | <noun-srdce-pl-genitive> ::= <noun-stem-srdce> "" # sŕdc | ||
| 281 | <noun-srdce-pl-dative> ::= <noun-stem-srdce> "iam" | ||
| 282 | <noun-srdce-pl-accusative> ::= <noun-stem-srdce> "ia" | ||
| 283 | <noun-srdce-pl-locative> ::= <noun-stem-srdce> "iach" | ||
| 284 | <noun-srdce-pl-instrumental> ::= <noun-stem-srdce> "ami" | ||
| 285 | <noun-stem-srdce> ::= "srdc" | "pol" | "mor" | "vrec" | "vajc" | "mor" | "nástupišt" | "pol" | ||
| 286 | |||
| 287 | <noun-vysvedcenie> ::= <noun-vysvedcenie-sg> | <noun-vysvedcenie-pl> | ||
| 288 | <noun-vysvedcenie-sg> ::= <noun-vysvedcenie-sg-nominative> | <noun-vysvedcenie-sg-genitive> | <noun-vysvedcenie-sg-dative> | <noun-vysvedcenie-sg-accusative> | <noun-vysvedcenie-sg-locative> | <noun-vysvedcenie-sg-instrumental> | ||
| 289 | <noun-vysvedcenie-pl> ::= <noun-vysvedcenie-pl-nominative> | <noun-vysvedcenie-pl-genitive> | <noun-vysvedcenie-pl-dative> | <noun-vysvedcenie-pl-accusative> | <noun-vysvedcenie-pl-locative> | <noun-vysvedcenie-pl-instrumental> | ||
| 290 | <noun-vysvedcenie-sg-nominative> ::= <noun-stem-vysvedcenie> "ie" | ||
| 291 | <noun-vysvedcenie-sg-genitive> ::= <noun-stem-vysvedcenie> "ia" | ||
| 292 | <noun-vysvedcenie-sg-dative> ::= <noun-stem-vysvedcenie> "iu" | ||
| 293 | <noun-vysvedcenie-sg-accusative> ::= <noun-stem-vysvedcenie> "ie" | ||
| 294 | <noun-vysvedcenie-sg-locative> ::= <noun-stem-vysvedcenie> "í" | ||
| 295 | <noun-vysvedcenie-sg-instrumental> ::= <noun-stem-vysvedcenie> "ím" | ||
| 296 | <noun-vysvedcenie-pl-nominative> ::= <noun-stem-vysvedcenie> "ia" | ||
| 297 | <noun-vysvedcenie-pl-genitive> ::= <noun-stem-vysvedcenie> "í" | ||
| 298 | <noun-vysvedcenie-pl-dative> ::= <noun-stem-vysvedcenie> "iam" | ||
| 299 | <noun-vysvedcenie-pl-accusative> ::= <noun-stem-vysvedcenie> "ia" | ||
| 300 | <noun-vysvedcenie-pl-locative> ::= <noun-stem-vysvedcenie> "iach" | ||
| 301 | <noun-vysvedcenie-pl-instrumental> ::= <noun-stem-vysvedcenie> "iami" | ||
| 302 | <noun-stem-vysvedcenie> ::= "vysvedčen" | "pozad" | "úskal" | "obdob" | "líst" | "stretnut" | "zahranič" | "námest" | "umenie" | ||
| 303 | |||
| 304 | <noun-dievca> ::= <noun-dievca-sg> | <noun-dievca-pl> | ||
| 305 | <noun-dievca-sg> ::= <noun-dievca-sg-nominative> | <noun-dievca-sg-genitive> | <noun-dievca-sg-dative> | <noun-dievca-sg-accusative> | <noun-dievca-sg-locative> | <noun-dievca-sg-instrumental> | ||
| 306 | <noun-dievca-pl> ::= <noun-dievca-pl-nominative> | <noun-dievca-pl-genitive> | <noun-dievca-pl-dative> | <noun-dievca-pl-accusative> | <noun-dievca-pl-locative> | <noun-dievca-pl-instrumental> | ||
| 307 | <noun-dievca-sg-nominative> ::= <noun-stem-dievca> "" | ||
| 308 | <noun-dievca-sg-genitive> ::= <noun-stem-dievca> "ťa" | ||
| 309 | <noun-dievca-sg-dative> ::= <noun-stem-dievca> "ťu" | ||
| 310 | <noun-dievca-sg-accusative> ::= <noun-stem-dievca> "" | ||
| 311 | <noun-dievca-sg-locative> ::= <noun-stem-dievca> "ti" | ||
| 312 | <noun-dievca-sg-instrumental> ::= <noun-stem-dievca> "ťom" | ||
| 313 | <noun-dievca-pl-nominative> ::= <noun-stem-dievca> "tá" | ||
| 314 | <noun-dievca-pl-genitive> ::= <noun-stem-dievca> "t" | ||
| 315 | <noun-dievca-pl-dative> ::= <noun-stem-dievca> "tám" | ||
| 316 | <noun-dievca-pl-accusative> ::= <noun-stem-dievca> "tá" | ||
| 317 | <noun-dievca-pl-locative> ::= <noun-stem-dievca> "tách" | ||
| 318 | <noun-dievca-pl-instrumental> ::= <noun-stem-dievca> "tami" | ||
| 319 | <noun-stem-dievca> ::= "dievča" | "vnúča" | "zviera" | "páža" | "knieža" | "mláďa" | "kura" | "chlápä" | "žriebä" # | "dieťa" | ||
| 320 | |||
| 321 | # slovak-noun.bnf ends here | ||
diff --git a/src/slovak-object.bnf b/src/slovak-object.bnf new file mode 100644 index 0000000..fb8062c --- /dev/null +++ b/src/slovak-object.bnf | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | # -*- BNF -*- | ||
| 2 | |||
| 3 | <objects> ::= <object> | <object> " " <objects> | ||
| 4 | |||
| 5 | <object> ::= <object-masculine> | <object-feminine> | <object-neuter> | ||
| 6 | <object-masculine> ::= <object-masculine-sg> | <object-masculine-pl> | ||
| 7 | <object-feminine> ::= <object-feminine-sg> | <object-feminine-sg> | ||
| 8 | <object-neuter> ::= <object-neuter-sg> | <object-neuter-sg> | ||
| 9 | |||
| 10 | <object-masculine-sg> ::= <object-masculine-sg-nominative> | <object-masculine-sg-genitive> | <object-masculine-sg-dative> | <object-masculine-sg-accusative> | <object-masculine-sg-locative> | <object-masculine-sg-instrumental> | ||
| 11 | <object-masculine-sg-nominative> ::= <noun-masculine-sg-nominative> | <adjective-sg-masculine-nominative> " " <noun-masculine-sg-nominative> | <adjectives-sg-masculine-nominative> " " <noun-masculine-sg-nominative> | ||
| 12 | <object-masculine-sg-genitive> ::= <noun-masculine-sg-genitive> | <adjective-sg-masculine-genitive> " " <noun-masculine-sg-genitive> | <adjectives-sg-masculine-genitive> " " <noun-masculine-sg-genitive> | ||
| 13 | <object-masculine-sg-dative> ::= <noun-masculine-sg-dative> | <adjective-sg-masculine-dative> " " <noun-masculine-sg-dative> | <adjectives-sg-masculine-dative> " " <noun-masculine-sg-dative> | ||
| 14 | <object-masculine-sg-accusative> ::= <noun-masculine-sg-accusative> | <adjective-sg-masculine-accusative> " " <noun-masculine-sg-accusative> | <adjectives-sg-masculine-accusative> " " <noun-masculine-sg-accusative> | ||
| 15 | <object-masculine-sg-locative> ::= <noun-masculine-sg-locative> | <adjective-sg-masculine-locative> " " <noun-masculine-sg-locative> | <adjectives-sg-masculine-locative> " " <noun-masculine-sg-locative> | ||
| 16 | <object-masculine-sg-instrumental> ::= <noun-masculine-sg-instrumental> | <adjective-sg-masculine-instrumental> " " <noun-masculine-sg-instrumental> | <adjectives-sg-masculine-instrumental> " " <noun-masculine-sg-instrumental> | ||
| 17 | |||
| 18 | <object-masculine-pl> ::= <object-masculine-pl-nominative> | <object-masculine-pl-genitive> | <object-masculine-pl-dative> | <object-masculine-pl-accusative> | <object-masculine-pl-locative> | <object-masculine-pl-instrumental> | ||
| 19 | <object-masculine-pl-nominative> ::= <noun-masculine-pl-nominative> | <adjective-pl-masculine-nominative> " " <noun-masculine-pl-nominative> | <adjectives-pl-masculine-nominative> " " <noun-masculine-pl-nominative> | ||
| 20 | <object-masculine-pl-genitive> ::= <noun-masculine-pl-genitive> | <adjective-pl-masculine-genitive> " " <noun-masculine-pl-genitive> | <adjectives-pl-masculine-genitive> " " <noun-masculine-pl-genitive> | ||
| 21 | <object-masculine-pl-dative> ::= <noun-masculine-pl-dative> | <adjective-pl-masculine-dative> " " <noun-masculine-pl-dative> | <adjectives-pl-masculine-dative> " " <noun-masculine-pl-dative> | ||
| 22 | <object-masculine-pl-accusative> ::= <noun-masculine-pl-accusative> | <adjective-pl-masculine-accusative> " " <noun-masculine-pl-accusative> | <adjectives-pl-masculine-accusative> " " <noun-masculine-pl-accusative> | ||
| 23 | <object-masculine-pl-locative> ::= <noun-masculine-pl-locative> | <adjective-pl-masculine-locative> " " <noun-masculine-pl-locative> | <adjectives-pl-masculine-locative> " " <noun-masculine-pl-locative> | ||
| 24 | <object-masculine-pl-instrumental> ::= <noun-masculine-pl-instrumental> | <adjective-pl-masculine-instrumental> " " <noun-masculine-pl-instrumental> | <adjectives-pl-masculine-instrumental> " " <noun-masculine-pl-instrumental> | ||
| 25 | |||
| 26 | <object-feminine-sg> ::= <object-feminine-sg-nominative> | <object-feminine-sg-genitive> | <object-feminine-sg-dative> | <object-feminine-sg-accusative> | <object-feminine-sg-locative> | <object-feminine-sg-instrumental> | ||
| 27 | <object-feminine-sg-nominative> ::= <noun-feminine-sg-nominative> | <adjective-sg-feminine-nominative> " " <noun-feminine-sg-nominative> | <adjectives-sg-feminine-nominative> " " <noun-feminine-sg-nominative> | ||
| 28 | <object-feminine-sg-genitive> ::= <noun-feminine-sg-genitive> | <adjective-sg-feminine-genitive> " " <noun-feminine-sg-genitive> | <adjectives-sg-feminine-genitive> " " <noun-feminine-sg-genitive> | ||
| 29 | <object-feminine-sg-dative> ::= <noun-feminine-sg-dative> | <adjective-sg-feminine-dative> " " <noun-feminine-sg-dative> | <adjectives-sg-feminine-dative> " " <noun-feminine-sg-dative> | ||
| 30 | <object-feminine-sg-accusative> ::= <noun-feminine-sg-accusative> | <adjective-sg-feminine-accusative> " " <noun-feminine-sg-accusative> | <adjectives-sg-feminine-accusative> " " <noun-feminine-sg-accusative> | ||
| 31 | <object-feminine-sg-locative> ::= <noun-feminine-sg-locative> | <adjective-sg-feminine-locative> " " <noun-feminine-sg-locative> | <adjectives-sg-feminine-locative> " " <noun-feminine-sg-locative> | ||
| 32 | <object-feminine-sg-instrumental> ::= <noun-feminine-sg-instrumental> | <adjective-sg-feminine-instrumental> " " <noun-feminine-sg-instrumental> | <adjectives-sg-feminine-instrumental> " " <noun-feminine-sg-instrumental> | ||
| 33 | |||
| 34 | <object-feminine-pl> ::= <object-feminine-pl-nominative> | <object-feminine-pl-genitive> | <object-feminine-pl-dative> | <object-feminine-pl-accusative> | <object-feminine-pl-locative> | <object-feminine-pl-instrumental> | ||
| 35 | <object-feminine-pl-nominative> ::= <noun-feminine-pl-nominative> | <adjective-pl-other-nominative> " " <noun-feminine-pl-nominative> | <adjectives-pl-other-nominative> " " <noun-feminine-pl-nominative> | ||
| 36 | <object-feminine-pl-genitive> ::= <noun-feminine-pl-genitive> | <adjective-pl-other-genitive> " " <noun-feminine-pl-genitive> | <adjectives-pl-other-genitive> " " <noun-feminine-pl-genitive> | ||
| 37 | <object-feminine-pl-dative> ::= <noun-feminine-pl-dative> | <adjective-pl-other-dative> " " <noun-feminine-pl-dative> | <adjectives-pl-other-dative> " " <noun-feminine-pl-dative> | ||
| 38 | <object-feminine-pl-accusative> ::= <noun-feminine-pl-accusative> | <adjective-pl-other-accusative> " " <noun-feminine-pl-accusative> | <adjectives-pl-other-accusative> " " <noun-feminine-pl-accusative> | ||
| 39 | <object-feminine-pl-locative> ::= <noun-feminine-pl-locative> | <adjective-pl-other-locative> " " <noun-feminine-pl-locative> | <adjectives-pl-other-locative> " " <noun-feminine-pl-locative> | ||
| 40 | <object-feminine-pl-instrumental> ::= <noun-feminine-pl-instrumental> | <adjective-pl-other-instrumental> " " <noun-feminine-pl-instrumental> | <adjectives-pl-other-instrumental> " " <noun-feminine-pl-instrumental> | ||
| 41 | |||
| 42 | <object-neuter-sg> ::= <object-neuter-sg-nominative> | <object-neuter-sg-genitive> | <object-neuter-sg-dative> | <object-neuter-sg-accusative> | <object-neuter-sg-locative> | <object-neuter-sg-instrumental> | ||
| 43 | <object-neuter-sg-nominative> ::= <noun-neuter-sg-nominative> | <adjective-sg-neuter-nominative> " " <noun-neuter-sg-nominative> | <adjectives-sg-neuter-nominative> " " <noun-neuter-sg-nominative> | ||
| 44 | <object-neuter-sg-genitive> ::= <noun-neuter-sg-genitive> | <adjective-sg-neuter-genitive> " " <noun-neuter-sg-genitive> | <adjectives-sg-neuter-genitive> " " <noun-neuter-sg-genitive> | ||
| 45 | <object-neuter-sg-dative> ::= <noun-neuter-sg-dative> | <adjective-sg-neuter-dative> " " <noun-neuter-sg-dative> | <adjectives-sg-neuter-dative> " " <noun-neuter-sg-dative> | ||
| 46 | <object-neuter-sg-accusative> ::= <noun-neuter-sg-accusative> | <adjective-sg-neuter-accusative> " " <noun-neuter-sg-accusative> | <adjectives-sg-neuter-accusative> " " <noun-neuter-sg-accusative> | ||
| 47 | <object-neuter-sg-locative> ::= <noun-neuter-sg-locative> | <adjective-sg-neuter-locative> " " <noun-neuter-sg-locative> | <adjectives-sg-neuter-locative> " " <noun-neuter-sg-locative> | ||
| 48 | <object-neuter-sg-instrumental> ::= <noun-neuter-sg-instrumental> | <adjective-sg-neuter-instrumental> " " <noun-neuter-sg-instrumental> | <adjectives-sg-neuter-instrumental> " " <noun-neuter-sg-instrumental> | ||
| 49 | |||
| 50 | <object-neuter-pl> ::= <object-neuter-pl-nominative> | <object-neuter-pl-genitive> | <object-neuter-pl-dative> | <object-neuter-pl-accusative> | <object-neuter-pl-locative> | <object-neuter-pl-instrumental> | ||
| 51 | <object-neuter-pl-nominative> ::= <noun-neuter-pl-nominative> | <adjective-pl-other-nominative> " " <noun-neuter-pl-nominative> | <adjectives-pl-other-nominative> " " <noun-neuter-pl-nominative> | ||
| 52 | <object-neuter-pl-genitive> ::= <noun-neuter-pl-genitive> | <adjective-pl-other-genitive> " " <noun-neuter-pl-genitive> | <adjectives-pl-other-genitive> " " <noun-neuter-pl-genitive> | ||
| 53 | <object-neuter-pl-dative> ::= <noun-neuter-pl-dative> | <adjective-pl-other-dative> " " <noun-neuter-pl-dative> | <adjectives-pl-other-dative> " " <noun-neuter-pl-dative> | ||
| 54 | <object-neuter-pl-accusative> ::= <noun-neuter-pl-accusative> | <adjective-pl-other-accusative> " " <noun-neuter-pl-accusative> | <adjectives-pl-other-accusative> " " <noun-neuter-pl-accusative> | ||
| 55 | <object-neuter-pl-locative> ::= <noun-neuter-pl-locative> | <adjective-pl-other-locative> " " <noun-neuter-pl-locative> | <adjectives-pl-other-locative> " " <noun-neuter-pl-locative> | ||
| 56 | <object-neuter-pl-instrumental> ::= <noun-neuter-pl-instrumental> | <adjective-pl-other-instrumental> " " <noun-neuter-pl-instrumental> | <adjectives-pl-other-instrumental> " " <noun-neuter-pl-instrumental> | ||
diff --git a/src/slovak-sentence.bnf b/src/slovak-sentence.bnf new file mode 100644 index 0000000..b2a9205 --- /dev/null +++ b/src/slovak-sentence.bnf | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # slovak-sentence.bnf -*- BNF -*- | ||
| 2 | # references: | ||
| 3 | # - https://www.zones.sk/studentske-prace/gramatika/11209-morfologia-slovenskeho-jazyka/ | ||
| 4 | # - https://yourslovakteacher.com/grammar_theory/introduction-to-slovak-syntax-and-sentence-structure/ | ||
| 5 | |||
| 6 | <initial> ::= <sentence> <EOF> | ||
| 7 | <sentence> ::= <fragment> <sentence-end> | <fragment> ", " <sentence> | <fragment> ", " <fragment-joiner> " " <sentence> | ||
| 8 | <fragment> ::= <verb> " " <object> | <fragment-sg> | <fragment-pl> | ||
| 9 | <fragment-sg> ::= <fragment-sg-p1> | <fragment-sg-p2> | <fragment-sg-p3> | ||
| 10 | <fragment-pl> ::= <fragment-pl-p1> | <fragment-pl-p2> | <fragment-pl-p3> | ||
| 11 | <fragment-sg-p1> ::= <subject-sg-p1> " " <verb-sg-p1> " " <object> | ||
| 12 | <fragment-sg-p2> ::= <subject-sg-p2> " " <verb-sg-p2> " " <object> | ||
| 13 | <fragment-sg-p3> ::= <subject-sg-p3> " " <verb-sg-p3> " " <object> | ||
| 14 | <fragment-pl-p1> ::= <subject-pl-p1> " " <verb-pl-p1> " " <object> | ||
| 15 | <fragment-pl-p2> ::= <subject-pl-p2> " " <verb-pl-p2> " " <object> | ||
| 16 | <fragment-pl-p3> ::= <subject-pl-p3> " " <verb-pl-p3> " " <object> | ||
| 17 | |||
| 18 | <fragment-joiner> ::= "ale" | "keď" | ||
| 19 | <sentence-end> ::= "." | "?" | "!" | "..." | ||
| 20 | |||
| 21 | <object> ::= <noun> | ||
| 22 | |||
| 23 | <subject> ::= <subject-sg> | <subject-pl> | ||
| 24 | <subject-sg> ::= <subject-sg-p1> | <subject-sg-p2> | <subject-sg-p3> | ||
| 25 | <subject-pl> ::= <subject-pl-p1> | <subject-pl-p2> | <subject-pl-p3> | ||
| 26 | <subject-sg-p1> ::= <pronoun-sg-p1> | ||
| 27 | <subject-sg-p2> ::= <pronoun-sg-p2> | ||
| 28 | <subject-sg-p3> ::= <pronoun-sg-p3> | <noun-sg-nominative> | ||
| 29 | <subject-pl-p1> ::= <pronoun-pl-p1> | ||
| 30 | <subject-pl-p2> ::= <pronoun-pl-p2> | ||
| 31 | <subject-pl-p3> ::= <pronoun-pl-p3> | <noun-pl-nominative> | ||
| 32 | |||
| 33 | <pronoun> ::= <pronoun-sg> | <pronoun-pl> | ||
| 34 | <pronoun-sg> ::= <pronoun-sg-p1> | <pronoun-sg-p2> | <pronoun-sg-p3> | ||
| 35 | <pronoun-pl> ::= <pronoun-pl-p1> | <pronoun-pl-p2> | <pronoun-pl-p3> | ||
| 36 | <pronoun-sg-p1> ::= "ja" | ||
| 37 | <pronoun-sg-p2> ::= "ty" | ||
| 38 | <pronoun-sg-p3> ::= "on" | "ona" | "ono" | ||
| 39 | <pronoun-pl-p1> ::= "my" | ||
| 40 | <pronoun-pl-p2> ::= "vy" | ||
| 41 | <pronoun-pl-p3> ::= "oni" | "ony" | ||
| 42 | |||
| 43 | # slovak-sentence.bnf ends here | ||
diff --git a/src/slovak-verb.bnf b/src/slovak-verb.bnf new file mode 100644 index 0000000..c29f991 --- /dev/null +++ b/src/slovak-verb.bnf | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | # slovak-verb.bnf -*- BNF -*- | ||
| 2 | # resources: | ||
| 3 | # - https://www.verbix.com/languages/slovak | ||
| 4 | # - https://slovake.eu/grammar/classes/verbs | ||
| 5 | # - https://www.eduself.sk/slovensky-jazyk/gramatika/slovesa | ||
| 6 | # - https://yourslovakteacher.com/patterns-in-slovak-verb-conjugation/ | ||
| 7 | # - https://yourslovakteacher.com/grammar_theory/overview-of-slovak-verb-tenses/ | ||
| 8 | # - https://www.zones.sk/studentske-prace/gramatika/11209-morfologia-slovenskeho-jazyka/ | ||
| 9 | |||
| 10 | <verb> ::= <verb-imperfective> | <verb-perfective> | ||
| 11 | <verb-sg> ::= <verb-sg-p1> | <verb-sg-p2> | <verb-sg-p3> | ||
| 12 | <verb-pl> ::= <verb-pl-p1> | <verb-pl-p2> | <verb-pl-p3> | ||
| 13 | <verb-sg-p1> ::= <verb-imperfective-indicative-future-sg-p1> | <verb-perfective-indicative-future-sg-p1> | ||
| 14 | <verb-sg-p2> ::= <verb-imperfective-indicative-future-sg-p2> | <verb-perfective-indicative-future-sg-p2> | ||
| 15 | <verb-sg-p3> ::= <verb-imperfective-indicative-future-sg-p3> | <verb-perfective-indicative-future-sg-p3> | ||
| 16 | <verb-pl-p1> ::= <verb-imperfective-indicative-future-pl-p1> | <verb-perfective-indicative-future-pl-p1> | ||
| 17 | <verb-pl-p2> ::= <verb-imperfective-indicative-future-pl-p2> | <verb-perfective-indicative-future-pl-p2> | ||
| 18 | <verb-pl-p3> ::= <verb-imperfective-indicative-future-pl-p3> | <verb-perfective-indicative-future-pl-p3> | ||
| 19 | |||
| 20 | <verb-imperfective> ::= <verb-imperfective-indicative> # | | ||
| 21 | <verb-imperfective-indicative> ::= <verb-imperfective-indicative-future> # | | ||
| 22 | <verb-imperfective-indicative-future> ::= <verb-imperfective-indicative-future-sg> | <verb-imperfective-indicative-future-pl> | ||
| 23 | <verb-imperfective-indicative-future-sg> ::= <verb-imperfective-indicative-future-sg-p1> | <verb-imperfective-indicative-future-sg-p2> | <verb-imperfective-indicative-future-sg-p3> | ||
| 24 | <verb-imperfective-indicative-future-pl> ::= <verb-imperfective-indicative-future-pl-p1> | <verb-imperfective-indicative-future-pl-p2> | <verb-imperfective-indicative-future-pl-p3> | ||
| 25 | <verb-imperfective-indicative-future-sg-p1> ::= "budem " <verb-infinitive> | ||
| 26 | <verb-imperfective-indicative-future-sg-p2> ::= "budeš " <verb-infinitive> | ||
| 27 | <verb-imperfective-indicative-future-sg-p3> ::= "bude " <verb-infinitive> | ||
| 28 | <verb-imperfective-indicative-future-pl-p1> ::= "budeme " <verb-infinitive> | ||
| 29 | <verb-imperfective-indicative-future-pl-p2> ::= "budete " <verb-infinitive> | ||
| 30 | <verb-imperfective-indicative-future-pl-p3> ::= "budú " <verb-infinitive> | ||
| 31 | |||
| 32 | <verb-perfective> ::= <verb-perfective-indicative> # | | ||
| 33 | <verb-perfective-indicative> ::= <verb-perfective-indicative-future> # | | ||
| 34 | <verb-perfective-indicative-future> ::= <verb-perfective-indicative-future-sg> | <verb-perfective-indicative-future-pl> | ||
| 35 | <verb-perfective-indicative-future-sg> ::= <verb-perfective-indicative-future-sg-p1> | <verb-perfective-indicative-future-sg-p2> | <verb-perfective-indicative-future-sg-p3> | ||
| 36 | <verb-perfective-indicative-future-pl> ::= <verb-perfective-indicative-future-pl-p1> | <verb-perfective-indicative-future-pl-p2> | <verb-perfective-indicative-future-pl-p3> | ||
| 37 | <verb-perfective-indicative-future-sg-p1> ::= <verb-perfective-indicative-future-sg-p1-g1-a> | <verb-perfective-indicative-future-sg-p1-g1-b> | <verb-perfective-indicative-future-sg-p1-g2-a> | <verb-perfective-indicative-future-sg-p1-g2-b> | <verb-perfective-indicative-future-sg-p1-g3> | <verb-perfective-indicative-future-sg-p1-g4> | ||
| 38 | <verb-perfective-indicative-future-sg-p2> ::= <verb-perfective-indicative-future-sg-p2-g1-a> | <verb-perfective-indicative-future-sg-p2-g1-b> | <verb-perfective-indicative-future-sg-p2-g2-a> | <verb-perfective-indicative-future-sg-p2-g2-b> | <verb-perfective-indicative-future-sg-p2-g3> | <verb-perfective-indicative-future-sg-p2-g4> | ||
| 39 | <verb-perfective-indicative-future-sg-p3> ::= <verb-perfective-indicative-future-sg-p3-g1-a> | <verb-perfective-indicative-future-sg-p3-g1-b> | <verb-perfective-indicative-future-sg-p3-g2-a> | <verb-perfective-indicative-future-sg-p3-g2-b> | <verb-perfective-indicative-future-sg-p3-g3> | <verb-perfective-indicative-future-sg-p3-g4> | ||
| 40 | <verb-perfective-indicative-future-pl-p1> ::= <verb-perfective-indicative-future-pl-p1-g1-a> | <verb-perfective-indicative-future-pl-p1-g1-b> | <verb-perfective-indicative-future-pl-p1-g2-a> | <verb-perfective-indicative-future-pl-p1-g2-b> | <verb-perfective-indicative-future-pl-p1-g3> | <verb-perfective-indicative-future-pl-p1-g4> | ||
| 41 | <verb-perfective-indicative-future-pl-p2> ::= <verb-perfective-indicative-future-pl-p2-g1-a> | <verb-perfective-indicative-future-pl-p2-g1-b> | <verb-perfective-indicative-future-pl-p2-g2-a> | <verb-perfective-indicative-future-pl-p2-g2-b> | <verb-perfective-indicative-future-pl-p2-g3> | <verb-perfective-indicative-future-pl-p2-g4> | ||
| 42 | <verb-perfective-indicative-future-pl-p3> ::= <verb-perfective-indicative-future-pl-p3-g1-a> | <verb-perfective-indicative-future-pl-p3-g1-b> | <verb-perfective-indicative-future-pl-p3-g2-a> | <verb-perfective-indicative-future-pl-p3-g2-b> | <verb-perfective-indicative-future-pl-p3-g3> | <verb-perfective-indicative-future-pl-p3-g4> | ||
| 43 | |||
| 44 | <verb-perfective-indicative-future-sg-p1-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "ám" | ||
| 45 | <verb-perfective-indicative-future-sg-p2-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "áš" | ||
| 46 | <verb-perfective-indicative-future-sg-p3-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "á" | ||
| 47 | <verb-perfective-indicative-future-pl-p1-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "áme" | ||
| 48 | <verb-perfective-indicative-future-pl-p2-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "áte" | ||
| 49 | <verb-perfective-indicative-future-pl-p3-g1-a> ::= <verb-prefix> <verb-stem-g1-a> "ajú" | ||
| 50 | |||
| 51 | <verb-perfective-indicative-future-sg-p1-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "am" | ||
| 52 | <verb-perfective-indicative-future-sg-p2-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "aš" | ||
| 53 | <verb-perfective-indicative-future-sg-p3-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "a" | ||
| 54 | <verb-perfective-indicative-future-pl-p1-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "ame" | ||
| 55 | <verb-perfective-indicative-future-pl-p2-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "ate" | ||
| 56 | <verb-perfective-indicative-future-pl-p3-g1-b> ::= <verb-prefix> <verb-stem-g1-b> "ajú" | ||
| 57 | |||
| 58 | <verb-perfective-indicative-future-sg-p1-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "ím" | ||
| 59 | <verb-perfective-indicative-future-sg-p2-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "íš" | ||
| 60 | <verb-perfective-indicative-future-sg-p3-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "í" | ||
| 61 | <verb-perfective-indicative-future-pl-p1-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "íme" | ||
| 62 | <verb-perfective-indicative-future-pl-p2-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "íte" | ||
| 63 | <verb-perfective-indicative-future-pl-p3-g2-a> ::= <verb-prefix> <verb-stem-g2-a> "ia" | ||
| 64 | |||
| 65 | <verb-perfective-indicative-future-sg-p1-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "im" | ||
| 66 | <verb-perfective-indicative-future-sg-p2-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "iš" | ||
| 67 | <verb-perfective-indicative-future-sg-p3-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "i" | ||
| 68 | <verb-perfective-indicative-future-pl-p1-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "ime" | ||
| 69 | <verb-perfective-indicative-future-pl-p2-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "ite" | ||
| 70 | <verb-perfective-indicative-future-pl-p3-g2-b> ::= <verb-prefix> <verb-stem-g2-b> "ia" | ||
| 71 | |||
| 72 | <verb-perfective-indicative-future-sg-p1-g3> ::= <verb-prefix> <verb-stem-g3> "ujem" | ||
| 73 | <verb-perfective-indicative-future-sg-p2-g3> ::= <verb-prefix> <verb-stem-g3> "uješ" | ||
| 74 | <verb-perfective-indicative-future-sg-p3-g3> ::= <verb-prefix> <verb-stem-g3> "uje" | ||
| 75 | <verb-perfective-indicative-future-pl-p1-g3> ::= <verb-prefix> <verb-stem-g3> "ujeme" | ||
| 76 | <verb-perfective-indicative-future-pl-p2-g3> ::= <verb-prefix> <verb-stem-g3> "ujete" | ||
| 77 | <verb-perfective-indicative-future-pl-p3-g3> ::= <verb-prefix> <verb-stem-g3> "ujú" | ||
| 78 | |||
| 79 | <verb-perfective-indicative-future-sg-p1-g4> ::= <verb-prefix> <verb-stem-g4> "iem" | ||
| 80 | <verb-perfective-indicative-future-sg-p2-g4> ::= <verb-prefix> <verb-stem-g4> "ieš" | ||
| 81 | <verb-perfective-indicative-future-sg-p3-g4> ::= <verb-prefix> <verb-stem-g4> "ie" | ||
| 82 | <verb-perfective-indicative-future-pl-p1-g4> ::= <verb-prefix> <verb-stem-g4> "ieme" | ||
| 83 | <verb-perfective-indicative-future-pl-p2-g4> ::= <verb-prefix> <verb-stem-g4> "iete" | ||
| 84 | <verb-perfective-indicative-future-pl-p3-g4> ::= <verb-prefix> <verb-stem-g4> "ejú" | ||
| 85 | |||
| 86 | <verb-infinitive> ::= <verb-intinifive-g1> | <verb-intinifive-g2> | <verb-intinifive-g3> | <verb-intinifive-g4> | ||
| 87 | <verb-intinifive-g1> ::= <verb-stem-g1> "ať" | ||
| 88 | <verb-intinifive-g2> ::= <verb-stem-g2> "iť" | ||
| 89 | <verb-intinifive-g3> ::= <verb-stem-g3> "ovať" | ||
| 90 | <verb-intinifive-g4> ::= <verb-stem-g4> "ieť" | ||
| 91 | |||
| 92 | <verb-stem> ::= <verb-stem-g1> | <verb-stem-g2> | <verb-stem-g3> | <verb-stem-g4> # ať | iť | ovať | ieť | ||
| 93 | <verb-stem-g1> ::= <verb-stem-g1-a> | <verb-stem-g1-b> | ||
| 94 | <verb-stem-g2> ::= <verb-stem-g2-a> | <verb-stem-g2-b> | ||
| 95 | <verb-stem-g1-a> ::= "m" | "hľad" | "pozn" | "hr" | ||
| 96 | <verb-stem-g1-b> ::= "čít" | "pláv" | "umýv" | "liet" | ||
| 97 | <verb-stem-g2-a> ::= "rob" | "uč" | "var" | "lieč" | ||
| 98 | <verb-stem-g2-b> ::= "kúp" | "ľúb" | "pál" | ||
| 99 | <verb-stem-g3> ::= "kup" | "okup" | "poráb" | "vyráb" | "preráb" | "štud" | "prac" | "tanc" | "telefon" | "fotograf" | "mail" | "maľ" | ||
| 100 | <verb-stem-g4> ::= "rozum" | "ved" | ||
| 101 | |||
| 102 | <verb-prefix> ::= "u" | "na" | "vy" | "pre" | "po" | "pod" | "za" | "pri" | "o" | "ob" | ||
| 103 | |||
| 104 | # NOTE(cmmm): modal verbs don't have prefixes | ||
| 105 | # TODO(cmmm): separate modal verbs... | ||
| 106 | # TODO(cmmm): ensure (using past tense conjugations for future tense) | ||
| 107 | # budem rozumieť | budem sa rozumieť | porozumel by som | porozumel by som sa | ||
| 108 | # budeš rozumieť | budeš sa rozumieť | porozumel by si | porozumel by si sa | porozumela by si | porozumela by si sa | porozumelo by si | porozumela by si sa | ||
| 109 | # bude rozumieť | bude sa rozumieť | porozumel by | porozumela by | porozumelo by | ||
| 110 | # budeme rozumieť | budeme sa rozumieť | budeme si rozumieť | porozumeli by sme | porozumeli by sme sa | porozumeli by sme si | ||
| 111 | # budete rozumieť | budete sa rozumieť | budete si rozumieť | porozumeli by ste | porozumeli by ste sa | porozumeli by ste si | ||
| 112 | # budú rozumieť | budú sa rozumieť | budú si rozumieť | porozumeli by sa | porozumeli by si | ||
| 113 | # slovak-verb.bnf ends here | ||
diff --git a/src/string-builder.c b/src/string-builder.c new file mode 100644 index 0000000..5880da8 --- /dev/null +++ b/src/string-builder.c | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #include "string-builder.h" | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | void | ||
| 6 | string_builder_append_ntimes (STRING_BUILDER sb, char c, size_t n) | ||
| 7 | { | ||
| 8 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, n); | ||
| 9 | memset (sb->items + sb->count, c, n); | ||
| 10 | sb->count += n; | ||
| 11 | } | ||
| 12 | |||
| 13 | void | ||
| 14 | string_builder_append_cstrs (STRING_BUILDER sb, ...) | ||
| 15 | { | ||
| 16 | va_list args; | ||
| 17 | |||
| 18 | va_start (args, sb); | ||
| 19 | const char *arg = va_arg (args, const char *); | ||
| 20 | while (arg) { | ||
| 21 | STRING_BUILDER_APPEND_CSTR(*sb, arg); | ||
| 22 | arg = va_arg (args, const char *); | ||
| 23 | } va_end (args); | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | string_builder_printf (STRING_BUILDER sb, const char *format, ...) | ||
| 28 | { | ||
| 29 | va_list args; | ||
| 30 | |||
| 31 | va_start (args, format); | ||
| 32 | int size = vsnprintf (NULL, 0, format, args); | ||
| 33 | va_end (args); | ||
| 34 | ASSERT (size >= 0); | ||
| 35 | |||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, size + 1); | ||
| 37 | va_start (args, format); | ||
| 38 | vsnprintf (&sb->items[sb->count], size + 1, format, args); | ||
| 39 | va_end (args); | ||
| 40 | sb->count += size; | ||
| 41 | } | ||
diff --git a/src/string-builder.h b/src/string-builder.h new file mode 100644 index 0000000..0ba8a4e --- /dev/null +++ b/src/string-builder.h | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #ifndef INCLUDED_STRING_BUILDER_H | ||
| 2 | #define INCLUDED_STRING_BUILDER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include "utils.h" | ||
| 7 | #include "dynamic-array.h" | ||
| 8 | |||
| 9 | struct string_builder { | ||
| 10 | union { char *items, *string; }; | ||
| 11 | size_t count; | ||
| 12 | size_t capacity; | ||
| 13 | }; | ||
| 14 | #define STRING_BUILDER struct string_builder * | ||
| 15 | |||
| 16 | #define STRING_BUILDER_APPEND_CHAR(SB, CHAR) \ | ||
| 17 | DYNAMIC_ARRAY_APPEND (SB, CHAR) | ||
| 18 | |||
| 19 | #define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \ | ||
| 20 | DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE) | ||
| 21 | |||
| 22 | #define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \ | ||
| 23 | ({ \ | ||
| 24 | const char *s = (CSTR); \ | ||
| 25 | size_t n = strlen (s); \ | ||
| 26 | DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \ | ||
| 27 | }) | ||
| 28 | |||
| 29 | #define STRING_BUILDER_APPEND_CSTRS(SB, ...) \ | ||
| 30 | string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL) | ||
| 31 | |||
| 32 | #define STRING_BUILDER_APPEND_STRING_VIEW(SB, SV) \ | ||
| 33 | DYNAMIC_ARRAY_APPEND_MANY(SB, (SV).beg, STRING_VIEW_LEN (SV)); | ||
| 34 | |||
| 35 | #define STRING_BUILDER_APPEND_NULL(SB) \ | ||
| 36 | DYNAMIC_ARRAY_APPEND (SB, '\0') | ||
| 37 | |||
| 38 | #define STRING_BUILDER_RESET(SB) \ | ||
| 39 | DYNAMIC_ARRAY_RESET (SB) | ||
| 40 | |||
| 41 | #define STRING_BUILDER_FREE(SB) \ | ||
| 42 | DYNAMIC_ARRAY_FREE(SB) | ||
| 43 | |||
| 44 | extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n); | ||
| 45 | extern void string_builder_append_cstrs (STRING_BUILDER, ...); | ||
| 46 | extern void string_builder_printf (STRING_BUILDER, const char *format, ...); | ||
| 47 | |||
| 48 | #endif | ||
diff --git a/src/string-view.h b/src/string-view.h new file mode 100644 index 0000000..0b6034a --- /dev/null +++ b/src/string-view.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef INCLUDED_STRING_VIEW_H | ||
| 2 | #define INCLUDED_STRING_VIEW_H | ||
| 3 | |||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | struct string_view { | ||
| 7 | const char *beg; | ||
| 8 | const char *end; | ||
| 9 | }; | ||
| 10 | |||
| 11 | #define STRING_VIEW_FROM_CSTR(CSTR) ((struct string_view) { .beg = (CSTR), .end = (CSTR) + strlen (CSTR) }) | ||
| 12 | #define STRING_VIEW_LEN(SV) ((SV).end - (SV).beg) | ||
| 13 | #define STRING_VIEW_EQUALS(A, B) \ | ||
| 14 | (STRING_VIEW_LEN (A) == STRING_VIEW_LEN (B) && \ | ||
| 15 | !memcmp ((A).beg, (B).beg, STRING_VIEW_LEN (A))) | ||
| 16 | |||
| 17 | #endif // INCLUDED_STRING_VIEW_H | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..16679e4 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | #include <assert.h> | ||
| 8 | |||
| 9 | #define UNUSED(X) ((void) (X)) | ||
| 10 | #define UNREACHABLE() ASSERT (!"reached the unreachable...") | ||
| 11 | #define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__) | ||
| 12 | // #define TODO(...) | ||
| 13 | #define REACHED(FMT, ...) fprintf (stderr, "%s:%d: info: %s: reached: "FMT"\n", __FILE__, __LINE__, __func__ __VA_OPT__(,) __VA_ARGS__) | ||
| 14 | |||
| 15 | #define ASSERT assert | ||
| 16 | #define REALLOC realloc | ||
| 17 | #define FREE free | ||
| 18 | |||
| 19 | #define DIE(FMT, ...) \ | ||
| 20 | ({ \ | ||
| 21 | fprintf (stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__); \ | ||
| 22 | exit (EXIT_FAILURE); \ | ||
| 23 | }) | ||
| 24 | |||
| 25 | #define ARRAY_LEN(array) (sizeof (array)/sizeof (*array)) | ||
| 26 | #define ARRAY_GET(array, index) \ | ||
| 27 | (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index]) | ||
| 28 | |||
| 29 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 30 | |||
| 31 | struct string_views { | ||
| 32 | struct string_view *items; | ||
| 33 | uint32_t count; | ||
| 34 | uint32_t capacity; | ||
| 35 | }; | ||
| 36 | |||
| 37 | #endif | ||
