From 08e01a1c817901f72481d966df84e6695c993d4f Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sat, 25 Jan 2025 13:09:41 -0500 Subject: [PATCH] Loosened restrictions for concatenation - It's okay if one of the elements is missing --- compile.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/compile.go b/compile.go index 252d3b7..759a482 100644 --- a/compile.go +++ b/compile.go @@ -828,9 +828,16 @@ func thompson(re []postfixNode) (Reg, error) { switch c.nodetype { case CONCATENATE: s2 := mustPop(&nfa) - s1 := mustPop(&nfa) - s1 = concatenate(s1, s2) - nfa = append(nfa, s1) + // Relax the requirements for concatenation a little bit - If + // the second element is not found ie. the postfixNodes look + // like 'a~', then that's fine, we just skip the concatenation. + s1, err := pop(&nfa) + if err != nil { + nfa = append(nfa, s2) + } else { + s1 = concatenate(s1, s2) + nfa = append(nfa, s1) + } case KLEENE: // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state s1 := mustPop(&nfa) stateToAdd := kleene(*s1)