faster: only consider possible values when generating successors

This commit is contained in:
John Doty 2024-04-15 09:55:44 -07:00
parent 5f89f460e5
commit ee60951ffc

View file

@ -122,9 +122,6 @@ class Configuration:
def rest(self): def rest(self):
return self.symbols[(self.position+1):] return self.symbols[(self.position+1):]
def at_symbol(self, symbol):
return self.next == symbol
def __str__(self): def __str__(self):
la = ", " + str(self.lookahead) if self.lookahead != () else "" la = ", " + str(self.lookahead) if self.lookahead != () else ""
return "{name} -> {bits}{lookahead}".format( return "{name} -> {bits}{lookahead}".format(
@ -351,7 +348,7 @@ class GenerateLR0(object):
seeds = tuple( seeds = tuple(
config.replace_position(config.position + 1) config.replace_position(config.position + 1)
for config in config_set for config in config_set
if config.at_symbol(symbol) if config.next == symbol
) )
closure = self.gen_closure(seeds) closure = self.gen_closure(seeds)
@ -359,8 +356,14 @@ class GenerateLR0(object):
def gen_all_successors(self, config_set: typing.Iterable[Configuration]) -> list[ConfigSet]: def gen_all_successors(self, config_set: typing.Iterable[Configuration]) -> list[ConfigSet]:
"""Return all of the non-empty successors for the given config set.""" """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 = [] next = []
for symbol in self.alphabet: for symbol in possible:
successor = self.gen_successor(config_set, symbol) successor = self.gen_successor(config_set, symbol)
if len(successor) > 0: if len(successor) > 0:
next.append(successor) next.append(successor)