From ee60951ffccc1090e826e4e6c3443dd1d63ba81c Mon Sep 17 00:00:00 2001 From: John Doty Date: Mon, 15 Apr 2024 09:55:44 -0700 Subject: [PATCH] faster: only consider possible values when generating successors --- parser_faster.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/parser_faster.py b/parser_faster.py index 6861961..40c2b71 100644 --- a/parser_faster.py +++ b/parser_faster.py @@ -122,9 +122,6 @@ class Configuration: def rest(self): return self.symbols[(self.position+1):] - def at_symbol(self, symbol): - return self.next == symbol - def __str__(self): la = ", " + str(self.lookahead) if self.lookahead != () else "" return "{name} -> {bits}{lookahead}".format( @@ -351,7 +348,7 @@ class GenerateLR0(object): seeds = tuple( config.replace_position(config.position + 1) for config in config_set - if config.at_symbol(symbol) + if config.next == symbol ) closure = self.gen_closure(seeds) @@ -359,8 +356,14 @@ class GenerateLR0(object): def gen_all_successors(self, config_set: typing.Iterable[Configuration]) -> list[ConfigSet]: """Return all of the non-empty successors for the given config set.""" + possible = tuple(sorted({ + config.next + for config in config_set + if config.next is not None + })) + next = [] - for symbol in self.alphabet: + for symbol in possible: successor = self.gen_successor(config_set, symbol) if len(successor) > 0: next.append(successor)