def test_ambig_highlight(self): p = self.p p.add( ParseRule( "top", "top", [Terminal("a"), NonTerminal("a"), Terminal("a")])) p.add(ParseRule("1", "a", [Terminal("a")])) p.add(ParseRule("2", "a", [Terminal("a")])) self.ambig("a a a", start_index=1, end_index=2)
def test_complexity(self): # Correctly written an Earley parse should be O(n) for several sorts of grammar # and O(n^3) worse case p = self.p p.add(ParseRule("1", "top", [NonTerminal("a", star=True)])) p.add(ParseRule("2", "a", [Terminal("a")])) p.add(ParseRule("3", "a", [Terminal("a")])) # Assuming the EarleyParser is correct, this should only take linear time/space to compute, despite 2^n possible # parses. Note that you can set this value well beyond Python's recursion depth n = 1000 forest = parse(self.p, "top", lex("a " * n)) self.assertEqual(forest.count(), 2**n) self.assertEqual(forest.internal_node_count, 4 + 5 * n)