From ceb7e9ee32e36c7193f833d7d2d35fc8dcd0bf59 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 29 Aug 2021 11:48:49 -0400 Subject: [PATCH] Add EOF token to Start rule patterns --- lib/imbecile/parser.rb | 2 ++ lib/imbecile/token.rb | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/imbecile/parser.rb b/lib/imbecile/parser.rb index 2d3c90d..107c3f9 100644 --- a/lib/imbecile/parser.rb +++ b/lib/imbecile/parser.rb @@ -3,9 +3,11 @@ class Imbecile class Parser def initialize(tokens, rules) + token_eof = Token.new("$", nil, TOKEN_EOF) @item_sets = [] item_sets_set = Set.new start_items = rules["Start"].patterns.map do |pattern| + pattern.components << token_eof Item.new(pattern, 0) end start_item_set = ItemSet.new(start_items) diff --git a/lib/imbecile/token.rb b/lib/imbecile/token.rb index e4283e7..b7607c4 100644 --- a/lib/imbecile/token.rb +++ b/lib/imbecile/token.rb @@ -22,9 +22,11 @@ class Imbecile @name = name @pattern = pattern @id = id - regex = Regex.new(pattern) - regex.nfa.end_state.accepts = self - @nfa = regex.nfa + unless pattern.nil? + regex = Regex.new(pattern) + regex.nfa.end_state.accepts = self + @nfa = regex.nfa + end end def c_name