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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
const std = @import("std");
const Grammar = @import("grammar.zig");
const NonTerminal = @import("non-terminal.zig");
const Character = @import("character.zig").Character;
const Generator = @import("generator.zig").Generator;
const gss = @import("gss.zig");
const State = struct {
const Self = @This();
id: usize,
rule_index: usize,
inner_position: usize,
input_position: usize,
pub fn format(
self: *const Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
try writer.print("[ {}, {}, {}, {} ]", .{
self.id,
self.rule_index,
self.inner_position,
self.input_position,
});
}
pub fn debug(self: *const Self, grammar: *Grammar, input: []const u8) void {
const rule = grammar.non_terminal_by_id(self.id).rules()[self.rule_index];
std.debug.print("input ({s}○{s}) state (", .{
input[0..self.input_position],
input[self.input_position..],
});
for (rule, 0..) |char, index| {
if (index == self.inner_position) {
std.debug.print("○", .{});
}
std.debug.print("{}", .{char});
}
if (rule.len == self.inner_position) {
std.debug.print("○", .{});
}
std.debug.print(")\n", .{});
}
};
pub fn check(grammar: *Grammar, input: []const u8, inner_allocator: std.mem.Allocator) !bool {
var arena = std.heap.ArenaAllocator.init(inner_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const entry = grammar.entry_point();
var queues = [2]std.ArrayList(*gss.Node(State)) {
std.ArrayList(*gss.Node(State)).init(allocator),
std.ArrayList(*gss.Node(State)).init(allocator),
};
defer for (queues) |queue| queue.deinit();
var processing_queue = &queues[0];
for (0..entry.rules().len) |index| {
const node = try allocator.create(gss.Node(State));
node.* = gss.Node(State).init(State {
.id = entry.id,
.rule_index = index,
.inner_position = 0,
.input_position = 0,
});
try processing_queue.append(node);
}
var next_processing_queue = &queues[1];
while (processing_queue.items.len > 0) {
for (processing_queue.items) |node| {
const rule = grammar.non_terminal_by_id(node.state.id).rules()[node.state.rule_index];
if (node.state.inner_position == rule.len) {
if (
node.parent == null and
node.state.id == entry.id and
node.state.input_position == input.len
) {
return true;
}
const old_state, const parent_node = node.pop(allocator);
if (parent_node) |parent| {
const sibling = try parent.clone(State {
.id = parent.state.id,
.rule_index = parent.state.rule_index,
.inner_position = parent.state.inner_position + 1,
.input_position = old_state.input_position,
}, allocator);
try next_processing_queue.append(sibling);
}
} else {
const epsilon_only = node.state.input_position == input.len;
switch (rule[node.state.inner_position]) {
.terminal => |t| {
if (!epsilon_only and input[node.state.input_position] == t) {
const sibling = try node.clone(State {
.id = node.state.id,
.rule_index = node.state.rule_index,
.inner_position = node.state.inner_position + 1,
.input_position = node.state.input_position + 1,
}, allocator);
try next_processing_queue.append(sibling);
}
},
.non_terminal => |n| {
const non_terminal = grammar.non_terminal_by_id(n);
if (!epsilon_only and non_terminal.first.is_set(input[node.state.input_position])) {
for (0..non_terminal.rules().len) |index| {
const child = try node.push(State {
.id = non_terminal.id,
.rule_index = index,
.inner_position = 0,
.input_position = node.state.input_position,
}, allocator);
try next_processing_queue.append(child);
}
}
if (non_terminal.first.is_set(Character.EPSILON)) {
const sibling = try node.clone(State {
.id = node.state.id,
.rule_index = node.state.rule_index,
.inner_position = node.state.inner_position + 1,
.input_position = node.state.input_position
}, allocator);
try next_processing_queue.append(sibling);
}
},
.epsilon => {
const sibling = try node.clone(State {
.id = node.state.id,
.rule_index = node.state.rule_index,
.inner_position = node.state.inner_position + 1,
.input_position = node.state.input_position
}, allocator);
try next_processing_queue.append(sibling);
},
}
}
}
const swap = processing_queue;
processing_queue = next_processing_queue;
next_processing_queue = swap;
next_processing_queue.clearRetainingCapacity();
}
return false;
}
test "expr" {
const text =
\\S -> B A
\\A -> '+' B A
\\A -> ''
\\B -> D C
\\C -> '*' D C
\\C -> ''
\\D -> '(' S ')'
\\D -> 'a'
\\D -> 'b'
;
const input = "b+a*b";
const allocator = std.testing.allocator;
var grammar = try Grammar.parse("S", text, allocator);
defer grammar.deinit();
try std.testing.expect(try check(&grammar, input, allocator));
}
test "simple 0 - success" {
const text =
\\S -> A S 'd'
\\S -> B S
\\S -> ''
\\A -> 'a'
\\A -> 'c'
\\B -> 'a'
\\B -> 'b'
;
const input = "aad";
const allocator = std.testing.allocator;
var grammar = try Grammar.parse("S", text, allocator);
defer grammar.deinit();
try std.testing.expect(try check(&grammar, input, allocator));
}
test "simple 0 - fail" {
const text =
\\S -> A S 'd'
\\S -> B S
\\S -> ''
\\A -> 'a'
\\A -> 'c'
\\B -> 'a'
\\B -> 'b'
;
const input = "accd";
const allocator = std.testing.allocator;
var grammar = try Grammar.parse("S", text, allocator);
defer grammar.deinit();
try std.testing.expect(!try check(&grammar, input, allocator));
}
test "simple 0 - fuzzy" {
const text =
\\S -> A S 'd'
\\S -> B S
\\S -> ''
\\A -> 'a'
\\A -> 'c'
\\B -> 'a'
\\B -> 'b'
;
const allocator = std.testing.allocator;
var grammar = try Grammar.parse("S", text, allocator);
defer grammar.deinit();
var generator = Generator(struct {
const Self = @This();
pub fn next(_: *Self, n: usize) usize {
return std.crypto.random.uintLessThan(usize, n);
}
}){};
for (0..100) |_| {
const input = try generator.sentential_from_grammar(&grammar, 1000, allocator);
defer allocator.free(input);
try std.testing.expect(try check(&grammar, input, allocator));
}
}
|