diff options
| author | 2026-11-08 05:23:31 +0000 | |
|---|---|---|
| committer | 2026-11-08 05:24:55 +0000 | |
| commit | 3f36aca66e1ac6c872300059e8db691a473614d4 (patch) | |
| tree | 48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/fsm-match.c | |
| download | lab-02-master.tar.gz | |
Diffstat (limited to 'src/fsm-match.c')
| -rw-r--r-- | src/fsm-match.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/fsm-match.c b/src/fsm-match.c new file mode 100644 index 0000000..6bce408 --- /dev/null +++ b/src/fsm-match.c | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | // fsm-match.c --- match string against a pattern using a finate state machine | ||
| 2 | // Usage: fsm-match string | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include "utils.h" | ||
| 7 | #include "fsm.h" | ||
| 8 | |||
| 9 | int | ||
| 10 | main (int argc, char *argv[]) | ||
| 11 | { | ||
| 12 | const char *progname = *argv++; | ||
| 13 | |||
| 14 | if (argc != 2) { | ||
| 15 | fprintf (stderr, "Usage: %s string\n", progname); | ||
| 16 | return EXIT_FAILURE; | ||
| 17 | } | ||
| 18 | |||
| 19 | const char *string = *argv++; | ||
| 20 | for (const char *p = string; *p; ++p) { | ||
| 21 | char s = C2A (*p); | ||
| 22 | if (0 > s || s >= ALPHABET_SYMBOL_COUNT) { | ||
| 23 | fprintf (stderr, "error: symbols not from alphabet " | ||
| 24 | "(starting from `%c' at column %ld)\n", *p, p - string); | ||
| 25 | return EXIT_FAILURE; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | if (fsm_match (string)) { | ||
| 30 | fprintf (stderr, "info: string matches\n"); | ||
| 31 | return EXIT_SUCCESS; | ||
| 32 | } else { | ||
| 33 | fprintf (stderr, "info: string doesn't match\n"); | ||
| 34 | return EXIT_FAILURE; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | // fsm-match.c ends here | ||
