From 0ce0fb8babe213cce6f3fa1fdf4894a37acf7a98 Mon Sep 17 00:00:00 2001 From: breandan Date: Sun, 1 Oct 2023 22:12:23 -0400 Subject: [PATCH] okay, bar-hillel works (just need to prune dangling NTs) --- .../kaliningraph/parsing/Normalization.kt | 13 + .../kaliningraph/parsing/SetValiantTest.kt | 762 ++++-------------- 2 files changed, 164 insertions(+), 611 deletions(-) diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt index 323aafdb..5f56f00d 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt @@ -12,6 +12,18 @@ import ai.hypergraph.kaliningraph.types.* val rewriteHistory = LRUCache>() +// Recursively rmoves all productions containing a dangling nonterminal, i.e., +// a nonterminal from a synthetic CFG that does not produce any terminals. +fun CFG.removeDanglingNonterminals( + matches: (Σᐩ) -> Boolean = { it.first() == '[' && it.last() == ']' && it.count { it == ',' } == 2 } +): CFG { + val rw = + filter { it.RHS.all { !matches(it) || it in nonterminals } } + .toSet() + + return if (rw.size == size) this else rw.removeDanglingNonterminals(matches) +} + /** * n.b. Normalization may destroy organic nonterminals! * In order to preserve every organic nonterminal, you @@ -32,6 +44,7 @@ fun CFG.normalize(): CFG = // Must remember to run the unit test if order changes in the future // ./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sat.SATValiantTest.testTLArithmetic" .generateNonterminalStubs() + .removeDanglingNonterminals() .also { cnf -> rewriteHistory.put(cnf.freeze(), rewrites) } } diff --git a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt index 7baa78d7..4c44fc53 100644 --- a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt +++ b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt @@ -709,611 +709,137 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma fun testBarHillel() { // Generated from https://github.com/breandan/bar-hillel/blob/00e04c56d40eccd663d7dc0a00c7a6280fea566c/rayuela/test/cfg/test_epsilon_Bar_Hillel.py val bhcfg = """ - START -> ~[1,START,4] -START -> ~[3,START,4] -[1,+,1] -> [1,ε,1] [1,+,1] -[1,+,1] -> [1,ε,2] [2,+,1] -[1,+,1] -> [1,ε,3] [3,+,1] -[1,+,1] -> [1,ε,4] [4,+,1] -[1,+,2] -> [1,ε,1] [1,+,2] -[1,+,2] -> [1,ε,2] [2,+,2] -[1,+,2] -> [1,ε,3] [3,+,2] -[1,+,2] -> [1,ε,4] [4,+,2] -[1,+,3] -> [1,ε,1] [1,+,3] -[1,+,3] -> [1,ε,2] [2,+,3] -[1,+,3] -> [1,ε,3] [3,+,3] -[1,+,3] -> [1,ε,4] [4,+,3] -[1,+,4] -> [1,ε,1] [1,+,4] -[1,+,4] -> [1,ε,2] [2,+,4] -[1,+,4] -> [1,ε,3] [3,+,4] -[1,+,4] -> [1,ε,4] [4,+,4] -[1,L,1] -> [1,O,1] [1,N,1] -[1,L,1] -> [1,O,2] [2,N,1] -[1,L,1] -> [1,O,3] [3,N,1] -[1,L,1] -> [1,O,4] [4,N,1] -[1,L,2] -> [1,O,1] [1,N,2] -[1,L,2] -> [1,O,2] [2,N,2] -[1,L,2] -> [1,O,3] [3,N,2] -[1,L,2] -> [1,O,4] [4,N,2] -[1,L,3] -> [1,O,1] [1,N,3] -[1,L,3] -> [1,O,2] [2,N,3] -[1,L,3] -> [1,O,3] [3,N,3] -[1,L,3] -> [1,O,4] [4,N,3] -[1,L,4] -> [1,O,1] [1,N,4] -[1,L,4] -> [1,O,2] [2,N,4] -[1,L,4] -> [1,O,3] [3,N,4] -[1,L,4] -> [1,O,4] [4,N,4] -[1,N,1] -> [1,a,1] -[1,N,1] -> [1,b,1] -[1,N,1] -> [1,N,1] [1,N,1] -[1,N,1] -> [1,N,2] [2,N,1] -[1,N,1] -> [1,N,3] [3,N,1] -[1,N,1] -> [1,N,4] [4,N,1] -[1,N,2] -> [1,a,2] -[1,N,2] -> [1,b,2] -[1,N,2] -> [1,N,1] [1,N,2] -[1,N,2] -> [1,N,2] [2,N,2] -[1,N,2] -> [1,N,3] [3,N,2] -[1,N,2] -> [1,N,4] [4,N,2] -[1,N,3] -> [1,a,3] -[1,N,3] -> [1,b,3] -[1,N,3] -> [1,N,1] [1,N,3] -[1,N,3] -> [1,N,2] [2,N,3] -[1,N,3] -> [1,N,3] [3,N,3] -[1,N,3] -> [1,N,4] [4,N,3] -[1,N,4] -> [1,a,4] -[1,N,4] -> [1,b,4] -[1,N,4] -> [1,N,1] [1,N,4] -[1,N,4] -> [1,N,2] [2,N,4] -[1,N,4] -> [1,N,3] [3,N,4] -[1,N,4] -> [1,N,4] [4,N,4] -[1,O,1] -> [1,x,1] -[1,O,1] -> [1,+,1] -[1,O,2] -> [1,x,2] -[1,O,2] -> [1,+,2] -[1,O,3] -> [1,x,3] -[1,O,3] -> [1,+,3] -[1,O,4] -> [1,x,4] -[1,O,4] -> [1,+,4] -[1,START,1] -> [1,N,1] [1,L,1] -[1,START,1] -> [1,N,2] [2,L,1] -[1,START,1] -> [1,N,3] [3,L,1] -[1,START,1] -> [1,N,4] [4,L,1] -[1,START,2] -> [1,N,1] [1,L,2] -[1,START,2] -> [1,N,2] [2,L,2] -[1,START,2] -> [1,N,3] [3,L,2] -[1,START,2] -> [1,N,4] [4,L,2] -[1,START,3] -> [1,N,1] [1,L,3] -[1,START,3] -> [1,N,2] [2,L,3] -[1,START,3] -> [1,N,3] [3,L,3] -[1,START,3] -> [1,N,4] [4,L,3] -[1,START,4] -> [1,N,1] [1,L,4] -[1,START,4] -> [1,N,2] [2,L,4] -[1,START,4] -> [1,N,3] [3,L,4] -[1,START,4] -> [1,N,4] [4,L,4] -[1,a,1] -> [1,ε,1] [1,a,1] -[1,a,1] -> [1,ε,2] [2,a,1] -[1,a,1] -> [1,ε,3] [3,a,1] -[1,a,1] -> [1,ε,4] [4,a,1] -[1,a,2] -> a -[1,a,2] -> [1,ε,1] [1,a,2] -[1,a,2] -> [1,ε,2] [2,a,2] -[1,a,2] -> [1,ε,3] [3,a,2] -[1,a,2] -> [1,ε,4] [4,a,2] -[1,a,3] -> [1,ε,1] [1,a,3] -[1,a,3] -> [1,ε,2] [2,a,3] -[1,a,3] -> [1,ε,3] [3,a,3] -[1,a,3] -> [1,ε,4] [4,a,3] -[1,a,4] -> [1,ε,1] [1,a,4] -[1,a,4] -> [1,ε,2] [2,a,4] -[1,a,4] -> [1,ε,3] [3,a,4] -[1,a,4] -> [1,ε,4] [4,a,4] -[1,b,1] -> [1,ε,1] [1,b,1] -[1,b,1] -> [1,ε,2] [2,b,1] -[1,b,1] -> [1,ε,3] [3,b,1] -[1,b,1] -> [1,ε,4] [4,b,1] -[1,b,2] -> [1,ε,1] [1,b,2] -[1,b,2] -> [1,ε,2] [2,b,2] -[1,b,2] -> [1,ε,3] [3,b,2] -[1,b,2] -> [1,ε,4] [4,b,2] -[1,b,3] -> [1,ε,1] [1,b,3] -[1,b,3] -> [1,ε,2] [2,b,3] -[1,b,3] -> [1,ε,3] [3,b,3] -[1,b,3] -> [1,ε,4] [4,b,3] -[1,b,4] -> [1,ε,1] [1,b,4] -[1,b,4] -> [1,ε,2] [2,b,4] -[1,b,4] -> [1,ε,3] [3,b,4] -[1,b,4] -> [1,ε,4] [4,b,4] -[1,x,1] -> [1,ε,1] [1,x,1] -[1,x,1] -> [1,ε,2] [2,x,1] -[1,x,1] -> [1,ε,3] [3,x,1] -[1,x,1] -> [1,ε,4] [4,x,1] -[1,x,2] -> [1,ε,1] [1,x,2] -[1,x,2] -> [1,ε,2] [2,x,2] -[1,x,2] -> [1,ε,3] [3,x,2] -[1,x,2] -> [1,ε,4] [4,x,2] -[1,x,3] -> [1,ε,1] [1,x,3] -[1,x,3] -> [1,ε,2] [2,x,3] -[1,x,3] -> [1,ε,3] [3,x,3] -[1,x,3] -> [1,ε,4] [4,x,3] -[1,x,4] -> [1,ε,1] [1,x,4] -[1,x,4] -> [1,ε,2] [2,x,4] -[1,x,4] -> [1,ε,3] [3,x,4] -[1,x,4] -> [1,ε,4] [4,x,4] -[2,+,1] -> [2,ε,1] [1,+,1] -[2,+,1] -> [2,ε,2] [2,+,1] -[2,+,1] -> [2,ε,3] [3,+,1] -[2,+,1] -> [2,ε,4] [4,+,1] -[2,+,2] -> [2,ε,1] [1,+,2] -[2,+,2] -> [2,ε,2] [2,+,2] -[2,+,2] -> [2,ε,3] [3,+,2] -[2,+,2] -> [2,ε,4] [4,+,2] -[2,+,3] -> + -[2,+,3] -> [2,ε,1] [1,+,3] -[2,+,3] -> [2,ε,2] [2,+,3] -[2,+,3] -> [2,ε,3] [3,+,3] -[2,+,3] -> [2,ε,4] [4,+,3] -[2,+,4] -> [2,ε,1] [1,+,4] -[2,+,4] -> [2,ε,2] [2,+,4] -[2,+,4] -> [2,ε,3] [3,+,4] -[2,+,4] -> [2,ε,4] [4,+,4] -[2,L,1] -> [2,O,1] [1,N,1] -[2,L,1] -> [2,O,2] [2,N,1] -[2,L,1] -> [2,O,3] [3,N,1] -[2,L,1] -> [2,O,4] [4,N,1] -[2,L,2] -> [2,O,1] [1,N,2] -[2,L,2] -> [2,O,2] [2,N,2] -[2,L,2] -> [2,O,3] [3,N,2] -[2,L,2] -> [2,O,4] [4,N,2] -[2,L,3] -> [2,O,1] [1,N,3] -[2,L,3] -> [2,O,2] [2,N,3] -[2,L,3] -> [2,O,3] [3,N,3] -[2,L,3] -> [2,O,4] [4,N,3] -[2,L,4] -> [2,O,1] [1,N,4] -[2,L,4] -> [2,O,2] [2,N,4] -[2,L,4] -> [2,O,3] [3,N,4] -[2,L,4] -> [2,O,4] [4,N,4] -[2,N,1] -> [2,a,1] -[2,N,1] -> [2,b,1] -[2,N,1] -> [2,N,1] [1,N,1] -[2,N,1] -> [2,N,2] [2,N,1] -[2,N,1] -> [2,N,3] [3,N,1] -[2,N,1] -> [2,N,4] [4,N,1] -[2,N,2] -> [2,a,2] -[2,N,2] -> [2,b,2] -[2,N,2] -> [2,N,1] [1,N,2] -[2,N,2] -> [2,N,2] [2,N,2] -[2,N,2] -> [2,N,3] [3,N,2] -[2,N,2] -> [2,N,4] [4,N,2] -[2,N,3] -> [2,a,3] -[2,N,3] -> [2,b,3] -[2,N,3] -> [2,N,1] [1,N,3] -[2,N,3] -> [2,N,2] [2,N,3] -[2,N,3] -> [2,N,3] [3,N,3] -[2,N,3] -> [2,N,4] [4,N,3] -[2,N,4] -> [2,a,4] -[2,N,4] -> [2,b,4] -[2,N,4] -> [2,N,1] [1,N,4] -[2,N,4] -> [2,N,2] [2,N,4] -[2,N,4] -> [2,N,3] [3,N,4] -[2,N,4] -> [2,N,4] [4,N,4] -[2,O,1] -> [2,x,1] -[2,O,1] -> [2,+,1] -[2,O,2] -> [2,x,2] -[2,O,2] -> [2,+,2] -[2,O,3] -> [2,x,3] -[2,O,3] -> [2,+,3] -[2,O,4] -> [2,x,4] -[2,O,4] -> [2,+,4] -[2,START,1] -> [2,N,1] [1,L,1] -[2,START,1] -> [2,N,2] [2,L,1] -[2,START,1] -> [2,N,3] [3,L,1] -[2,START,1] -> [2,N,4] [4,L,1] -[2,START,2] -> [2,N,1] [1,L,2] -[2,START,2] -> [2,N,2] [2,L,2] -[2,START,2] -> [2,N,3] [3,L,2] -[2,START,2] -> [2,N,4] [4,L,2] -[2,START,3] -> [2,N,1] [1,L,3] -[2,START,3] -> [2,N,2] [2,L,3] -[2,START,3] -> [2,N,3] [3,L,3] -[2,START,3] -> [2,N,4] [4,L,3] -[2,START,4] -> [2,N,1] [1,L,4] -[2,START,4] -> [2,N,2] [2,L,4] -[2,START,4] -> [2,N,3] [3,L,4] -[2,START,4] -> [2,N,4] [4,L,4] -[2,a,1] -> [2,ε,1] [1,a,1] -[2,a,1] -> [2,ε,2] [2,a,1] -[2,a,1] -> [2,ε,3] [3,a,1] -[2,a,1] -> [2,ε,4] [4,a,1] -[2,a,2] -> a -[2,a,2] -> [2,ε,1] [1,a,2] -[2,a,2] -> [2,ε,2] [2,a,2] -[2,a,2] -> [2,ε,3] [3,a,2] -[2,a,2] -> [2,ε,4] [4,a,2] -[2,a,3] -> [2,ε,1] [1,a,3] -[2,a,3] -> [2,ε,2] [2,a,3] -[2,a,3] -> [2,ε,3] [3,a,3] -[2,a,3] -> [2,ε,4] [4,a,3] -[2,a,4] -> [2,ε,1] [1,a,4] -[2,a,4] -> [2,ε,2] [2,a,4] -[2,a,4] -> [2,ε,3] [3,a,4] -[2,a,4] -> [2,ε,4] [4,a,4] -[2,b,1] -> [2,ε,1] [1,b,1] -[2,b,1] -> [2,ε,2] [2,b,1] -[2,b,1] -> [2,ε,3] [3,b,1] -[2,b,1] -> [2,ε,4] [4,b,1] -[2,b,2] -> [2,ε,1] [1,b,2] -[2,b,2] -> [2,ε,2] [2,b,2] -[2,b,2] -> [2,ε,3] [3,b,2] -[2,b,2] -> [2,ε,4] [4,b,2] -[2,b,3] -> [2,ε,1] [1,b,3] -[2,b,3] -> [2,ε,2] [2,b,3] -[2,b,3] -> [2,ε,3] [3,b,3] -[2,b,3] -> [2,ε,4] [4,b,3] -[2,b,4] -> [2,ε,1] [1,b,4] -[2,b,4] -> [2,ε,2] [2,b,4] -[2,b,4] -> [2,ε,3] [3,b,4] -[2,b,4] -> [2,ε,4] [4,b,4] -[2,x,1] -> [2,ε,1] [1,x,1] -[2,x,1] -> [2,ε,2] [2,x,1] -[2,x,1] -> [2,ε,3] [3,x,1] -[2,x,1] -> [2,ε,4] [4,x,1] -[2,x,2] -> [2,ε,1] [1,x,2] -[2,x,2] -> [2,ε,2] [2,x,2] -[2,x,2] -> [2,ε,3] [3,x,2] -[2,x,2] -> [2,ε,4] [4,x,2] -[2,x,3] -> [2,ε,1] [1,x,3] -[2,x,3] -> [2,ε,2] [2,x,3] -[2,x,3] -> [2,ε,3] [3,x,3] -[2,x,3] -> [2,ε,4] [4,x,3] -[2,x,4] -> [2,ε,1] [1,x,4] -[2,x,4] -> [2,ε,2] [2,x,4] -[2,x,4] -> [2,ε,3] [3,x,4] -[2,x,4] -> [2,ε,4] [4,x,4] -[3,+,1] -> [3,ε,1] [1,+,1] -[3,+,1] -> [3,ε,2] [2,+,1] -[3,+,1] -> [3,ε,3] [3,+,1] -[3,+,1] -> [3,ε,4] [4,+,1] -[3,+,2] -> [3,ε,1] [1,+,2] -[3,+,2] -> [3,ε,2] [2,+,2] -[3,+,2] -> [3,ε,3] [3,+,2] -[3,+,2] -> [3,ε,4] [4,+,2] -[3,+,3] -> [3,ε,1] [1,+,3] -[3,+,3] -> [3,ε,2] [2,+,3] -[3,+,3] -> [3,ε,3] [3,+,3] -[3,+,3] -> [3,ε,4] [4,+,3] -[3,+,4] -> [3,ε,1] [1,+,4] -[3,+,4] -> [3,ε,2] [2,+,4] -[3,+,4] -> [3,ε,3] [3,+,4] -[3,+,4] -> [3,ε,4] [4,+,4] -[3,L,1] -> [3,O,1] [1,N,1] -[3,L,1] -> [3,O,2] [2,N,1] -[3,L,1] -> [3,O,3] [3,N,1] -[3,L,1] -> [3,O,4] [4,N,1] -[3,L,2] -> [3,O,1] [1,N,2] -[3,L,2] -> [3,O,2] [2,N,2] -[3,L,2] -> [3,O,3] [3,N,2] -[3,L,2] -> [3,O,4] [4,N,2] -[3,L,3] -> [3,O,1] [1,N,3] -[3,L,3] -> [3,O,2] [2,N,3] -[3,L,3] -> [3,O,3] [3,N,3] -[3,L,3] -> [3,O,4] [4,N,3] -[3,L,4] -> [3,O,1] [1,N,4] -[3,L,4] -> [3,O,2] [2,N,4] -[3,L,4] -> [3,O,3] [3,N,4] -[3,L,4] -> [3,O,4] [4,N,4] -[3,N,1] -> [3,a,1] -[3,N,1] -> [3,b,1] -[3,N,1] -> [3,N,1] [1,N,1] -[3,N,1] -> [3,N,2] [2,N,1] -[3,N,1] -> [3,N,3] [3,N,1] -[3,N,1] -> [3,N,4] [4,N,1] -[3,N,2] -> [3,a,2] -[3,N,2] -> [3,b,2] -[3,N,2] -> [3,N,1] [1,N,2] -[3,N,2] -> [3,N,2] [2,N,2] -[3,N,2] -> [3,N,3] [3,N,2] -[3,N,2] -> [3,N,4] [4,N,2] -[3,N,3] -> [3,a,3] -[3,N,3] -> [3,b,3] -[3,N,3] -> [3,N,1] [1,N,3] -[3,N,3] -> [3,N,2] [2,N,3] -[3,N,3] -> [3,N,3] [3,N,3] -[3,N,3] -> [3,N,4] [4,N,3] -[3,N,4] -> [3,a,4] -[3,N,4] -> [3,b,4] -[3,N,4] -> [3,N,1] [1,N,4] -[3,N,4] -> [3,N,2] [2,N,4] -[3,N,4] -> [3,N,3] [3,N,4] -[3,N,4] -> [3,N,4] [4,N,4] -[3,O,1] -> [3,x,1] -[3,O,1] -> [3,+,1] -[3,O,2] -> [3,x,2] -[3,O,2] -> [3,+,2] -[3,O,3] -> [3,x,3] -[3,O,3] -> [3,+,3] -[3,O,4] -> [3,x,4] -[3,O,4] -> [3,+,4] -[3,START,1] -> [3,N,1] [1,L,1] -[3,START,1] -> [3,N,2] [2,L,1] -[3,START,1] -> [3,N,3] [3,L,1] -[3,START,1] -> [3,N,4] [4,L,1] -[3,START,2] -> [3,N,1] [1,L,2] -[3,START,2] -> [3,N,2] [2,L,2] -[3,START,2] -> [3,N,3] [3,L,2] -[3,START,2] -> [3,N,4] [4,L,2] -[3,START,3] -> [3,N,1] [1,L,3] -[3,START,3] -> [3,N,2] [2,L,3] -[3,START,3] -> [3,N,3] [3,L,3] -[3,START,3] -> [3,N,4] [4,L,3] -[3,START,4] -> [3,N,1] [1,L,4] -[3,START,4] -> [3,N,2] [2,L,4] -[3,START,4] -> [3,N,3] [3,L,4] -[3,START,4] -> [3,N,4] [4,L,4] -[3,a,1] -> [3,ε,1] [1,a,1] -[3,a,1] -> [3,ε,2] [2,a,1] -[3,a,1] -> [3,ε,3] [3,a,1] -[3,a,1] -> [3,ε,4] [4,a,1] -[3,a,2] -> [3,ε,1] [1,a,2] -[3,a,2] -> [3,ε,2] [2,a,2] -[3,a,2] -> [3,ε,3] [3,a,2] -[3,a,2] -> [3,ε,4] [4,a,2] -[3,a,3] -> [3,ε,1] [1,a,3] -[3,a,3] -> [3,ε,2] [2,a,3] -[3,a,3] -> [3,ε,3] [3,a,3] -[3,a,3] -> [3,ε,4] [4,a,3] -[3,a,4] -> [3,ε,1] [1,a,4] -[3,a,4] -> [3,ε,2] [2,a,4] -[3,a,4] -> [3,ε,3] [3,a,4] -[3,a,4] -> [3,ε,4] [4,a,4] -[3,b,1] -> [3,ε,1] [1,b,1] -[3,b,1] -> [3,ε,2] [2,b,1] -[3,b,1] -> [3,ε,3] [3,b,1] -[3,b,1] -> [3,ε,4] [4,b,1] -[3,b,2] -> [3,ε,1] [1,b,2] -[3,b,2] -> [3,ε,2] [2,b,2] -[3,b,2] -> [3,ε,3] [3,b,2] -[3,b,2] -> [3,ε,4] [4,b,2] -[3,b,3] -> [3,ε,1] [1,b,3] -[3,b,3] -> [3,ε,2] [2,b,3] -[3,b,3] -> [3,ε,3] [3,b,3] -[3,b,3] -> [3,ε,4] [4,b,3] -[3,b,4] -> b -[3,b,4] -> [3,ε,1] [1,b,4] -[3,b,4] -> [3,ε,2] [2,b,4] -[3,b,4] -> [3,ε,3] [3,b,4] -[3,b,4] -> [3,ε,4] [4,b,4] -[3,x,1] -> [3,ε,1] [1,x,1] -[3,x,1] -> [3,ε,2] [2,x,1] -[3,x,1] -> [3,ε,3] [3,x,1] -[3,x,1] -> [3,ε,4] [4,x,1] -[3,x,2] -> [3,ε,1] [1,x,2] -[3,x,2] -> [3,ε,2] [2,x,2] -[3,x,2] -> [3,ε,3] [3,x,2] -[3,x,2] -> [3,ε,4] [4,x,2] -[3,x,3] -> [3,ε,1] [1,x,3] -[3,x,3] -> [3,ε,2] [2,x,3] -[3,x,3] -> [3,ε,3] [3,x,3] -[3,x,3] -> [3,ε,4] [4,x,3] -[3,x,4] -> [3,ε,1] [1,x,4] -[3,x,4] -> [3,ε,2] [2,x,4] -[3,x,4] -> [3,ε,3] [3,x,4] -[3,x,4] -> [3,ε,4] [4,x,4] -[4,+,1] -> + -[4,+,1] -> [4,ε,1] [1,+,1] -[4,+,1] -> [4,ε,2] [2,+,1] -[4,+,1] -> [4,ε,3] [3,+,1] -[4,+,1] -> [4,ε,4] [4,+,1] -[4,+,2] -> [4,ε,1] [1,+,2] -[4,+,2] -> [4,ε,2] [2,+,2] -[4,+,2] -> [4,ε,3] [3,+,2] -[4,+,2] -> [4,ε,4] [4,+,2] -[4,+,3] -> [4,ε,1] [1,+,3] -[4,+,3] -> [4,ε,2] [2,+,3] -[4,+,3] -> [4,ε,3] [3,+,3] -[4,+,3] -> [4,ε,4] [4,+,3] -[4,+,4] -> [4,ε,1] [1,+,4] -[4,+,4] -> [4,ε,2] [2,+,4] -[4,+,4] -> [4,ε,3] [3,+,4] -[4,+,4] -> [4,ε,4] [4,+,4] -[4,L,1] -> [4,O,1] [1,N,1] -[4,L,1] -> [4,O,2] [2,N,1] -[4,L,1] -> [4,O,3] [3,N,1] -[4,L,1] -> [4,O,4] [4,N,1] -[4,L,2] -> [4,O,1] [1,N,2] -[4,L,2] -> [4,O,2] [2,N,2] -[4,L,2] -> [4,O,3] [3,N,2] -[4,L,2] -> [4,O,4] [4,N,2] -[4,L,3] -> [4,O,1] [1,N,3] -[4,L,3] -> [4,O,2] [2,N,3] -[4,L,3] -> [4,O,3] [3,N,3] -[4,L,3] -> [4,O,4] [4,N,3] -[4,L,4] -> [4,O,1] [1,N,4] -[4,L,4] -> [4,O,2] [2,N,4] -[4,L,4] -> [4,O,3] [3,N,4] -[4,L,4] -> [4,O,4] [4,N,4] -[4,N,1] -> [4,a,1] -[4,N,1] -> [4,b,1] -[4,N,1] -> [4,N,1] [1,N,1] -[4,N,1] -> [4,N,2] [2,N,1] -[4,N,1] -> [4,N,3] [3,N,1] -[4,N,1] -> [4,N,4] [4,N,1] -[4,N,2] -> [4,a,2] -[4,N,2] -> [4,b,2] -[4,N,2] -> [4,N,1] [1,N,2] -[4,N,2] -> [4,N,2] [2,N,2] -[4,N,2] -> [4,N,3] [3,N,2] -[4,N,2] -> [4,N,4] [4,N,2] -[4,N,3] -> [4,a,3] -[4,N,3] -> [4,b,3] -[4,N,3] -> [4,N,1] [1,N,3] -[4,N,3] -> [4,N,2] [2,N,3] -[4,N,3] -> [4,N,3] [3,N,3] -[4,N,3] -> [4,N,4] [4,N,3] -[4,N,4] -> [4,a,4] -[4,N,4] -> [4,b,4] -[4,N,4] -> [4,N,1] [1,N,4] -[4,N,4] -> [4,N,2] [2,N,4] -[4,N,4] -> [4,N,3] [3,N,4] -[4,N,4] -> [4,N,4] [4,N,4] -[4,O,1] -> [4,x,1] -[4,O,1] -> [4,+,1] -[4,O,2] -> [4,x,2] -[4,O,2] -> [4,+,2] -[4,O,3] -> [4,x,3] -[4,O,3] -> [4,+,3] -[4,O,4] -> [4,x,4] -[4,O,4] -> [4,+,4] -[4,START,1] -> [4,N,1] [1,L,1] -[4,START,1] -> [4,N,2] [2,L,1] -[4,START,1] -> [4,N,3] [3,L,1] -[4,START,1] -> [4,N,4] [4,L,1] -[4,START,2] -> [4,N,1] [1,L,2] -[4,START,2] -> [4,N,2] [2,L,2] -[4,START,2] -> [4,N,3] [3,L,2] -[4,START,2] -> [4,N,4] [4,L,2] -[4,START,3] -> [4,N,1] [1,L,3] -[4,START,3] -> [4,N,2] [2,L,3] -[4,START,3] -> [4,N,3] [3,L,3] -[4,START,3] -> [4,N,4] [4,L,3] -[4,START,4] -> [4,N,1] [1,L,4] -[4,START,4] -> [4,N,2] [2,L,4] -[4,START,4] -> [4,N,3] [3,L,4] -[4,START,4] -> [4,N,4] [4,L,4] -[4,a,1] -> [4,ε,1] [1,a,1] -[4,a,1] -> [4,ε,2] [2,a,1] -[4,a,1] -> [4,ε,3] [3,a,1] -[4,a,1] -> [4,ε,4] [4,a,1] -[4,a,2] -> [4,ε,1] [1,a,2] -[4,a,2] -> [4,ε,2] [2,a,2] -[4,a,2] -> [4,ε,3] [3,a,2] -[4,a,2] -> [4,ε,4] [4,a,2] -[4,a,3] -> [4,ε,1] [1,a,3] -[4,a,3] -> [4,ε,2] [2,a,3] -[4,a,3] -> [4,ε,3] [3,a,3] -[4,a,3] -> [4,ε,4] [4,a,3] -[4,a,4] -> [4,ε,1] [1,a,4] -[4,a,4] -> [4,ε,2] [2,a,4] -[4,a,4] -> [4,ε,3] [3,a,4] -[4,a,4] -> [4,ε,4] [4,a,4] -[4,b,1] -> [4,ε,1] [1,b,1] -[4,b,1] -> [4,ε,2] [2,b,1] -[4,b,1] -> [4,ε,3] [3,b,1] -[4,b,1] -> [4,ε,4] [4,b,1] -[4,b,2] -> [4,ε,1] [1,b,2] -[4,b,2] -> [4,ε,2] [2,b,2] -[4,b,2] -> [4,ε,3] [3,b,2] -[4,b,2] -> [4,ε,4] [4,b,2] -[4,b,3] -> [4,ε,1] [1,b,3] -[4,b,3] -> [4,ε,2] [2,b,3] -[4,b,3] -> [4,ε,3] [3,b,3] -[4,b,3] -> [4,ε,4] [4,b,3] -[4,b,4] -> b -[4,b,4] -> [4,ε,1] [1,b,4] -[4,b,4] -> [4,ε,2] [2,b,4] -[4,b,4] -> [4,ε,3] [3,b,4] -[4,b,4] -> [4,ε,4] [4,b,4] -[4,x,1] -> [4,ε,1] [1,x,1] -[4,x,1] -> [4,ε,2] [2,x,1] -[4,x,1] -> [4,ε,3] [3,x,1] -[4,x,1] -> [4,ε,4] [4,x,1] -[4,x,2] -> [4,ε,1] [1,x,2] -[4,x,2] -> [4,ε,2] [2,x,2] -[4,x,2] -> [4,ε,3] [3,x,2] -[4,x,2] -> [4,ε,4] [4,x,2] -[4,x,3] -> [4,ε,1] [1,x,3] -[4,x,3] -> [4,ε,2] [2,x,3] -[4,x,3] -> [4,ε,3] [3,x,3] -[4,x,3] -> [4,ε,4] [4,x,3] -[4,x,4] -> [4,ε,1] [1,x,4] -[4,x,4] -> [4,ε,2] [2,x,4] -[4,x,4] -> [4,ε,3] [3,x,4] -[4,x,4] -> [4,ε,4] [4,x,4] -~[1,START,1] -> [1,START,1] -~[1,START,1] -> ~[1,START,1] [1,ε,1] -~[1,START,1] -> ~[1,START,2] [2,ε,1] -~[1,START,1] -> ~[1,START,3] [3,ε,1] -~[1,START,1] -> ~[1,START,4] [4,ε,1] -~[1,START,2] -> [1,START,2] -~[1,START,2] -> ~[1,START,1] [1,ε,2] -~[1,START,2] -> ~[1,START,2] [2,ε,2] -~[1,START,2] -> ~[1,START,3] [3,ε,2] -~[1,START,2] -> ~[1,START,4] [4,ε,2] -~[1,START,3] -> [1,START,3] -~[1,START,3] -> ~[1,START,1] [1,ε,3] -~[1,START,3] -> ~[1,START,2] [2,ε,3] -~[1,START,3] -> ~[1,START,3] [3,ε,3] -~[1,START,3] -> ~[1,START,4] [4,ε,3] -~[1,START,4] -> [1,START,4] -~[1,START,4] -> ~[1,START,1] [1,ε,4] -~[1,START,4] -> ~[1,START,2] [2,ε,4] -~[1,START,4] -> ~[1,START,3] [3,ε,4] -~[1,START,4] -> ~[1,START,4] [4,ε,4] -~[2,START,1] -> [2,START,1] -~[2,START,1] -> ~[2,START,1] [1,ε,1] -~[2,START,1] -> ~[2,START,2] [2,ε,1] -~[2,START,1] -> ~[2,START,3] [3,ε,1] -~[2,START,1] -> ~[2,START,4] [4,ε,1] -~[2,START,2] -> [2,START,2] -~[2,START,2] -> ~[2,START,1] [1,ε,2] -~[2,START,2] -> ~[2,START,2] [2,ε,2] -~[2,START,2] -> ~[2,START,3] [3,ε,2] -~[2,START,2] -> ~[2,START,4] [4,ε,2] -~[2,START,3] -> [2,START,3] -~[2,START,3] -> ~[2,START,1] [1,ε,3] -~[2,START,3] -> ~[2,START,2] [2,ε,3] -~[2,START,3] -> ~[2,START,3] [3,ε,3] -~[2,START,3] -> ~[2,START,4] [4,ε,3] -~[2,START,4] -> [2,START,4] -~[2,START,4] -> ~[2,START,1] [1,ε,4] -~[2,START,4] -> ~[2,START,2] [2,ε,4] -~[2,START,4] -> ~[2,START,3] [3,ε,4] -~[2,START,4] -> ~[2,START,4] [4,ε,4] -~[3,START,1] -> [3,START,1] -~[3,START,1] -> ~[3,START,1] [1,ε,1] -~[3,START,1] -> ~[3,START,2] [2,ε,1] -~[3,START,1] -> ~[3,START,3] [3,ε,1] -~[3,START,1] -> ~[3,START,4] [4,ε,1] -~[3,START,2] -> [3,START,2] -~[3,START,2] -> ~[3,START,1] [1,ε,2] -~[3,START,2] -> ~[3,START,2] [2,ε,2] -~[3,START,2] -> ~[3,START,3] [3,ε,2] -~[3,START,2] -> ~[3,START,4] [4,ε,2] -~[3,START,3] -> [3,START,3] -~[3,START,3] -> ~[3,START,1] [1,ε,3] -~[3,START,3] -> ~[3,START,2] [2,ε,3] -~[3,START,3] -> ~[3,START,3] [3,ε,3] -~[3,START,3] -> ~[3,START,4] [4,ε,3] -~[3,START,4] -> [3,START,4] -~[3,START,4] -> ~[3,START,1] [1,ε,4] -~[3,START,4] -> ~[3,START,2] [2,ε,4] -~[3,START,4] -> ~[3,START,3] [3,ε,4] -~[3,START,4] -> ~[3,START,4] [4,ε,4] -~[4,START,1] -> [4,START,1] -~[4,START,1] -> ~[4,START,1] [1,ε,1] -~[4,START,1] -> ~[4,START,2] [2,ε,1] -~[4,START,1] -> ~[4,START,3] [3,ε,1] -~[4,START,1] -> ~[4,START,4] [4,ε,1] -~[4,START,2] -> [4,START,2] -~[4,START,2] -> ~[4,START,1] [1,ε,2] -~[4,START,2] -> ~[4,START,2] [2,ε,2] -~[4,START,2] -> ~[4,START,3] [3,ε,2] -~[4,START,2] -> ~[4,START,4] [4,ε,2] -~[4,START,3] -> [4,START,3] -~[4,START,3] -> ~[4,START,1] [1,ε,3] -~[4,START,3] -> ~[4,START,2] [2,ε,3] -~[4,START,3] -> ~[4,START,3] [3,ε,3] -~[4,START,3] -> ~[4,START,4] [4,ε,3] -~[4,START,4] -> [4,START,4] -~[4,START,4] -> ~[4,START,1] [1,ε,4] -~[4,START,4] -> ~[4,START,2] [2,ε,4] -~[4,START,4] -> ~[4,START,3] [3,ε,4] -~[4,START,4] -> ~[4,START,4] [4,ε,4] -""".parseCFG().noNonterminalStubs + START -> [1,START,4] + START -> [3,START,4] + [1,+,3] -> + + [1,L,1] -> [1,O,1] [1,N,1] + [1,L,1] -> [1,O,3] [3,N,1] + [1,L,1] -> [1,O,4] [4,N,1] + [1,L,3] -> [1,O,1] [1,N,3] + [1,L,3] -> [1,O,3] [3,N,3] + [1,L,3] -> [1,O,4] [4,N,3] + [1,L,4] -> [1,O,1] [1,N,4] + [1,L,4] -> [1,O,3] [3,N,4] + [1,L,4] -> [1,O,4] [4,N,4] + [1,N,1] -> [1,a,1] + [1,N,1] -> [1,b,1] + [1,N,1] -> [1,N,1] [1,N,1] + [1,N,1] -> [1,N,3] [3,N,1] + [1,N,1] -> [1,N,4] [4,N,1] + [1,N,3] -> [1,a,3] + [1,N,3] -> [1,b,3] + [1,N,3] -> [1,N,1] [1,N,3] + [1,N,3] -> [1,N,3] [3,N,3] + [1,N,3] -> [1,N,4] [4,N,3] + [1,N,4] -> [1,a,4] + [1,N,4] -> [1,b,4] + [1,N,4] -> [1,N,1] [1,N,4] + [1,N,4] -> [1,N,3] [3,N,4] + [1,N,4] -> [1,N,4] [4,N,4] + [1,O,1] -> [1,x,1] + [1,O,1] -> [1,+,1] + [1,O,3] -> [1,x,3] + [1,O,3] -> [1,+,3] + [1,O,4] -> [1,x,4] + [1,O,4] -> [1,+,4] + [1,START,1] -> [1,N,1] [1,L,1] + [1,START,1] -> [1,N,3] [3,L,1] + [1,START,1] -> [1,N,4] [4,L,1] + [1,START,3] -> [1,N,1] [1,L,3] + [1,START,3] -> [1,N,3] [3,L,3] + [1,START,3] -> [1,N,4] [4,L,3] + [1,START,4] -> [1,N,1] [1,L,4] + [1,START,4] -> [1,N,3] [3,L,4] + [1,START,4] -> [1,N,4] [4,L,4] + [1,a,1] -> a + [3,L,1] -> [3,O,1] [1,N,1] + [3,L,1] -> [3,O,3] [3,N,1] + [3,L,1] -> [3,O,4] [4,N,1] + [3,L,3] -> [3,O,1] [1,N,3] + [3,L,3] -> [3,O,3] [3,N,3] + [3,L,3] -> [3,O,4] [4,N,3] + [3,L,4] -> [3,O,1] [1,N,4] + [3,L,4] -> [3,O,3] [3,N,4] + [3,L,4] -> [3,O,4] [4,N,4] + [3,N,1] -> [3,a,1] + [3,N,1] -> [3,b,1] + [3,N,1] -> [3,N,1] [1,N,1] + [3,N,1] -> [3,N,3] [3,N,1] + [3,N,1] -> [3,N,4] [4,N,1] + [3,N,3] -> [3,a,3] + [3,N,3] -> [3,b,3] + [3,N,3] -> [3,N,1] [1,N,3] + [3,N,3] -> [3,N,3] [3,N,3] + [3,N,3] -> [3,N,4] [4,N,3] + [3,N,4] -> [3,a,4] + [3,N,4] -> [3,b,4] + [3,N,4] -> [3,N,1] [1,N,4] + [3,N,4] -> [3,N,3] [3,N,4] + [3,N,4] -> [3,N,4] [4,N,4] + [3,O,1] -> [3,x,1] + [3,O,1] -> [3,+,1] + [3,O,3] -> [3,x,3] + [3,O,3] -> [3,+,3] + [3,O,4] -> [3,x,4] + [3,O,4] -> [3,+,4] + [3,START,1] -> [3,N,1] [1,L,1] + [3,START,1] -> [3,N,3] [3,L,1] + [3,START,1] -> [3,N,4] [4,L,1] + [3,START,3] -> [3,N,1] [1,L,3] + [3,START,3] -> [3,N,3] [3,L,3] + [3,START,3] -> [3,N,4] [4,L,3] + [3,START,4] -> [3,N,1] [1,L,4] + [3,START,4] -> [3,N,3] [3,L,4] + [3,START,4] -> [3,N,4] [4,L,4] + [3,b,4] -> b + [4,+,1] -> + + [4,L,1] -> [4,O,1] [1,N,1] + [4,L,1] -> [4,O,3] [3,N,1] + [4,L,1] -> [4,O,4] [4,N,1] + [4,L,3] -> [4,O,1] [1,N,3] + [4,L,3] -> [4,O,3] [3,N,3] + [4,L,3] -> [4,O,4] [4,N,3] + [4,L,4] -> [4,O,1] [1,N,4] + [4,L,4] -> [4,O,3] [3,N,4] + [4,L,4] -> [4,O,4] [4,N,4] + [4,N,1] -> [4,a,1] + [4,N,1] -> [4,b,1] + [4,N,1] -> [4,N,1] [1,N,1] + [4,N,1] -> [4,N,3] [3,N,1] + [4,N,1] -> [4,N,4] [4,N,1] + [4,N,3] -> [4,a,3] + [4,N,3] -> [4,b,3] + [4,N,3] -> [4,N,1] [1,N,3] + [4,N,3] -> [4,N,3] [3,N,3] + [4,N,3] -> [4,N,4] [4,N,3] + [4,N,4] -> [4,a,4] + [4,N,4] -> [4,b,4] + [4,N,4] -> [4,N,1] [1,N,4] + [4,N,4] -> [4,N,3] [3,N,4] + [4,N,4] -> [4,N,4] [4,N,4] + [4,O,1] -> [4,x,1] + [4,O,1] -> [4,+,1] + [4,O,3] -> [4,x,3] + [4,O,3] -> [4,+,3] + [4,O,4] -> [4,x,4] + [4,O,4] -> [4,+,4] + [4,START,1] -> [4,N,1] [1,L,1] + [4,START,1] -> [4,N,3] [3,L,1] + [4,START,1] -> [4,N,4] [4,L,1] + [4,START,3] -> [4,N,1] [1,L,3] + [4,START,3] -> [4,N,3] [3,L,3] + [4,START,3] -> [4,N,4] [4,L,3] + [4,START,4] -> [4,N,1] [1,L,4] + [4,START,4] -> [4,N,3] [3,L,4] + [4,START,4] -> [4,N,4] [4,L,4] + [4,b,4] -> b + """.trimIndent().parseCFG().noNonterminalStubs + + println(bhcfg.pretty) // bhcfg is the intersection of val cfg = """ - S -> N L + START -> N L N -> N N | a | b O -> x O -> + @@ -1322,23 +848,37 @@ START -> ~[3,START,4] val fsa = """ INIT: 1, 3 FINAL: 4 - 1 -[a]-> 2 - 2 -[+]-> 3 - 2 -[a]-> 2 + 1 -[a]-> 1 + 1 -[+]-> 3 3 -[b]-> 4 4 -[+]-> 1 4 -[b]-> 4 """.trimIndent() + val fsaCfg = """ + START -> START b | 3 b + 3 -> 1 + + 1 -> a | 1 a | START + + """.parseCFG() + println("Grammar size: ${bhcfg.size}") println("Solutions:") + + val template = "_ _ _ _ _ _ _" measureTime { -// bhcfg.solve("_ _ _ _ _ _ _", {it.weight}).onEach { - bhcfg.solveSeq("_ _ _ _ _ _ _").onEach { -// assertTrue { it in bhcfg.language } // Something funny going on here - println(it) }.toList().also { println("Found ${it.size} solutions.") } - }.also { println("Took: ${it.inWholeMilliseconds}ms") } + template.tokenizeByWhitespace() + .solve(bhcfg, fillers = bhcfg.terminals) + .map { it.replace("ε", "").tokenizeByWhitespace().joinToString(" ") } + .distinct() + .onEach { println(it) } + .toList().also { println("Found ${it.size} solutions.") } + }.also { println("Brute force solver took: ${it.inWholeMilliseconds}ms") } -// println(bhcfg.parse("a b a + b")!!.prettyPrint()) + measureTime { +// bhcfg.solve(template, {it.weight}) + bhcfg.solveSeq(template) + .onEach { assertTrue { it in bhcfg.language }; println(it) } + .toList().also { println("Found ${it.size} solutions.") } + }.also { println("Sequential solver took: ${it.inWholeMilliseconds}ms") } } } \ No newline at end of file