-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfa-proc-test.c
52 lines (41 loc) · 1003 Bytes
/
nfa-proc-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* RE to NFA compiler and NFA processor Sample
*
* Copyright (c) 2020-2021 Alexei A. Smekalkine <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <stdio.h>
#include <peruse/nfa-proc.h>
#include <peruse/nfa-parse.h>
static int nfa_proc_match (struct nfa_proc *o, const char *s)
{
int state = nfa_proc_start (o);
for (; *s != '\0'; ++s)
if ((state = nfa_proc_step (o, *s)) < 0)
return 0;
return state > 0;
}
int main (int argc, char *argv[])
{
struct nfa_state *nfa;
struct nfa_proc *proc;
int i;
if (argc < 3) {
fprintf (stderr, "usage:\n\tnfa-test RE string...\n");
return 1;
}
if ((nfa = nfa_parse_re (argv[1], 1)) == NULL) {
fprintf (stderr, "nfa-test: cannot compile RE\n");
return 1;
}
if ((proc = nfa_proc_alloc (nfa)) == NULL) {
fprintf (stderr, "E: cannot initialize NFA processor\n");
return 1;
}
for (i = 2; i < argc; ++i)
if (nfa_proc_match (proc, argv[i]))
printf ("%s\n", argv[i]);
nfa_proc_free (proc);
return 0;
}