Added a field to State, that tells me what kind of assertion (if any) it is making. Also added function to check if a state's contents contain a given value (checks assertions), and to find all matches that a state has for a character
This commit is contained in:
67
nfa.go
67
nfa.go
@@ -1,14 +1,69 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
const EPSILON int = 0
|
const EPSILON int = 0
|
||||||
|
|
||||||
|
type assertType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NONE assertType = iota
|
||||||
|
SOS
|
||||||
|
EOS
|
||||||
|
WBOUND
|
||||||
|
NONWBOUND
|
||||||
|
)
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
content stateContents // Contents of current state
|
content stateContents // Contents of current state
|
||||||
isEmpty bool // If it is empty - Union operator and Kleene star states will be empty
|
isEmpty bool // If it is empty - Union operator and Kleene star states will be empty
|
||||||
isLast bool // If it is the last state (acept state)
|
isLast bool // If it is the last state (acept state)
|
||||||
output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
|
output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
|
||||||
transitions map[int][]*State // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa)
|
transitions map[int][]*State // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa)
|
||||||
isKleene bool // Identifies whether current node is a 0-state representing Kleene star
|
isKleene bool // Identifies whether current node is a 0-state representing Kleene star
|
||||||
|
assert assertType // Type of assertion of current node - NONE means that the node doesn't assert anything
|
||||||
|
zeroMatchFound bool // Whether or not the state has been used for a zero-length match - only relevant for zero states
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if the contents of 's' contain the value at the given index of the given string
|
||||||
|
func (s State) contentContains(str []rune, idx int) bool {
|
||||||
|
if s.assert == SOS {
|
||||||
|
return idx == 0
|
||||||
|
}
|
||||||
|
if s.assert == EOS {
|
||||||
|
return idx == len(str)
|
||||||
|
}
|
||||||
|
if s.assert == WBOUND {
|
||||||
|
if s.assert == WBOUND {
|
||||||
|
return isWordBoundary(str, idx)
|
||||||
|
}
|
||||||
|
if s.assert == NONWBOUND {
|
||||||
|
return !isWordBoundary(str, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Default - s.assert must be NONE
|
||||||
|
return slices.Contains(s.content, int(str[idx]))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the matches for the character at the given index of the given string.
|
||||||
|
// Also returns the number of matches. Returns -1 if an assertion failed.
|
||||||
|
func (s State) matchesFor(str []rune, idx int) ([]*State, int) {
|
||||||
|
// Assertions can be viewed as 'checks'. If the check fails, we return
|
||||||
|
// an empty array and 0.
|
||||||
|
// If it passes, we treat it like any other state, and return all the transitions.
|
||||||
|
if s.assert == SOS && idx != 0 {
|
||||||
|
return make([]*State, 0), -1
|
||||||
|
}
|
||||||
|
if s.assert == EOS && idx != len(str) {
|
||||||
|
return make([]*State, 0), -1
|
||||||
|
}
|
||||||
|
if s.assert == WBOUND && !isWordBoundary(str, idx) {
|
||||||
|
return make([]*State, 0), -1
|
||||||
|
}
|
||||||
|
if s.assert == NONWBOUND && isWordBoundary(str, idx) {
|
||||||
|
return make([]*State, 0), -1
|
||||||
|
}
|
||||||
|
return s.transitions[int(str[idx])], len(s.transitions[int(str[idx])])
|
||||||
}
|
}
|
||||||
|
|
||||||
type NFA struct {
|
type NFA struct {
|
||||||
|
Reference in New Issue
Block a user