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(); name: u8, rule: NonTerminal.Rule, 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("[ {c}, {s}, {}, {} ]", .{ self.name, self.rule, self.inner_position, self.input_position, }); } }; 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 (entry.rules()) |rule| { const node = try allocator.create(gss.Node(State)); node.* = gss.Node(State).init(State { .name = entry.name, .rule = rule, .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| { if (node.state.inner_position == node.state.rule.len) { if ( node.parent == null and node.state.name == entry.name 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 { .name = parent.state.name, .rule = parent.state.rule, .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 (node.state.rule[node.state.inner_position]) { .terminal => |t| { if (!epsilon_only and input[node.state.input_position] == t) { const sibling = try node.clone(State { .name = node.state.name, .rule = node.state.rule, .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_name(n); const rules = non_terminal.rules(); if (!epsilon_only and non_terminal.first.is_set(input[node.state.input_position])) { for (rules) |rule| { const child = try node.push(State { .name = non_terminal.name, .rule = rule, .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 { .name = node.state.name, .rule = node.state.rule, .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 { .name = node.state.name, .rule = node.state.rule, .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(text, allocator); defer grammar.deinit(allocator); 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(text, allocator); defer grammar.deinit(allocator); 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(text, allocator); defer grammar.deinit(allocator); 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(text, allocator); defer grammar.deinit(allocator); 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)); } }