diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-04-24 20:06:50 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-04-24 20:06:50 +0200 |
| commit | f593da7580f423b1405f4705081368acef0b3c21 (patch) | |
| tree | 875e25e99da4f531e44e7537c7d8006da9ee0aa5 /src/main.zig | |
| parent | 839271627d0cfd2240ec30a603ff60a0165fe6a2 (diff) | |
fix naming
the struct RuleList is now named NonTerminal
and its member rhs is now called Rule.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/main.zig b/src/main.zig index 62877cb..7a0513e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,8 +1,42 @@ const std = @import("std"); +pub const Grammar = @import("grammar.zig"); +pub const recognizer = @import("recognizer.zig"); -pub const grammar = @import("grammar.zig"); +fn help() !noreturn { + const stderr = std.io.getStdErr().writer(); + try stderr.print("gll [grammar] [input]\n", .{}); + std.process.exit(1); +} + +fn read_whole_file(path: []const u8, allocator: std.mem.Allocator) ![]const u8 { + const file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); + const stat = try file.stat(); + return file.readToEndAlloc(allocator, stat.size); +} pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + defer { + if (gpa.deinit() == .leak) { + @panic("memory leak detected"); + } + } + + var args = std.process.args(); + _ = args.next(); + + const grammar_path = args.next() orelse try help(); + const input_path = args.next() orelse try help(); + + const grammar_text = try read_whole_file(grammar_path, allocator); + defer allocator.free(grammar_text); + const input = try read_whole_file(input_path, allocator); + defer allocator.free(input); + + var grammar = try Grammar.parse(grammar_text, allocator); + defer grammar.deinit(allocator); } test { |