-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfa.sql
56 lines (51 loc) · 1.14 KB
/
dfa.sql
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
53
54
55
56
-- $ psql some-database < dfa.sql
-- CREATE TABLE
-- INSERT 0 6
-- CREATE TABLE
-- INSERT 0 3
-- CREATE TABLE
-- INSERT 0 3
-- source | accepted
-- --------+----------
-- baa | f
-- a | f
-- baba | t
-- (3 rows)
--
-- DROP TABLE
-- DROP TABLE
-- DROP TABLE
CREATE TABLE rules (state INTEGER, chr VARCHAR(10), next_state INTEGER);
INSERT INTO rules VALUES
(1, 'a', 2), (1, 'b', 1),
(2, 'a', 2), (2, 'b', 3),
(3, 'a', 3), (3, 'b', 3);
CREATE TABLE accept_states (state INTEGER, accepted BOOLEAN);
INSERT INTO accept_states VALUES (1, false), (2, false), (3, true);
CREATE TABLE inputs (str TEXT);
INSERT INTO inputs VALUES ('a'), ('baa'), ('baba');
WITH RECURSIVE dfa(source, state, work) AS (
SELECT
inputs.str, 1, inputs.str
FROM
inputs
UNION ALL
SELECT
source,
next_rules.next_state,
SUBSTR(dfa.work, 2, 10)
FROM
dfa JOIN (SELECT * FROM rules) AS next_rules ON dfa.state = next_rules.state
WHERE
chr = SUBSTR(dfa.work, 1, 1)
)
SELECT
source,
accepted
FROM
dfa JOIN accept_states ON dfa.state = accept_states.state
WHERE
work = '';
DROP TABLE rules;
DROP TABLE accept_states;
DROP TABLE inputs;