From 139c88dd58d44372bc6977c16c0699bfbc3660a4 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Oct 2024 14:39:28 -0400 Subject: [PATCH] Started working on '+' operator --- main.go | 6 ++++-- matching.go | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 968dbee..3d765a8 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( const CONCAT rune = '~' func isOperator(c rune) bool { - if c == '*' || c == '|' || c == CONCAT { + if c == '+' || c == '*' || c == '|' || c == CONCAT { return true } return false @@ -19,7 +19,7 @@ func isOperator(c rune) bool { /* priority returns the priority of the given operator */ func priority(op rune) int { - precedence := []rune{'|', CONCAT, '*'} + precedence := []rune{'|', CONCAT, '+', '*'} return slices.Index(precedence, op) } @@ -138,6 +138,8 @@ func thompson(re string) *State { } stateToAdd.transitions[s1.content] = append(stateToAdd.transitions[s1.content], s1) nfa = append(nfa, stateToAdd) + case '+': + case '|': s1 := pop(&nfa) s2 := pop(&nfa) diff --git a/matching.go b/matching.go index 6904cf5..07a7eff 100644 --- a/matching.go +++ b/matching.go @@ -40,15 +40,16 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset currentStates := make([]*State, 0) tempStates := make([]*State, 0) // Used to store states that should be used in next loop iteration i := 0 // Index in string + startingFrom := i // Store starting index // Increment until we hit a character matching the start state (assuming not 0-state) if start.isEmpty == false { for i < len(str) && int(str[i]) != start.content { i++ } startIdx = i + startingFrom = i i++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character } - startingFrom := i currentStates = append(currentStates, start) // Main loop @@ -57,6 +58,7 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset zeroStates := make([]*State, 0) // Keep taking zero-states, until there are no more left to take + // Objective: If any of our current states have transitions to 0-states, replace them with the 0-state. Do this until there are no more transitions to 0-states zeroStates, isZero := takeZeroState(currentStates) tempStates = append(tempStates, zeroStates...) for isZero == true { @@ -105,7 +107,8 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset tempStates = nil for _, state := range currentStates { - if state.isLast { + // Only add the match if we the start index is in bounds + if state.isLast && startIdx+offset < len(str)+offset { endIdx = i indices = append(indices, matchIndex{startIdx + offset, endIdx + offset}) }