Visualize DFAs over a finite set of tokens
This commit is contained in:
parent
868f4e203f
commit
0656a2faf4
2 changed files with 28 additions and 9 deletions
|
|
@ -6,6 +6,7 @@ module Rextra.Dfa
|
|||
( Dfa
|
||||
, dfa
|
||||
, State(..)
|
||||
, stateTransition
|
||||
, normalize
|
||||
, mapState
|
||||
, transitionsByState
|
||||
|
|
@ -25,6 +26,12 @@ data State s t = State
|
|||
, defaultTransition :: s
|
||||
} deriving (Show, Eq, Ord)
|
||||
|
||||
stateTransition :: (Ord t) => State s t -> t -> s
|
||||
stateTransition state token =
|
||||
case transitions state Map.!? token of
|
||||
Nothing -> defaultTransition state
|
||||
Just s -> s
|
||||
|
||||
normalize :: (Eq s) => State s t -> State s t
|
||||
normalize State{transitions, defaultTransition} =
|
||||
State { transitions = Map.filter (/= defaultTransition) transitions
|
||||
|
|
@ -50,16 +57,9 @@ type Dfa s t = Fa State s t
|
|||
|
||||
instance (Ord s) => Executable (Fa State s) s where
|
||||
startState = entryState
|
||||
transition = dfaTransition
|
||||
transition a s = stateTransition (getState a s)
|
||||
accepts a s = s `Set.member` exitStates a
|
||||
|
||||
dfaTransition :: (Ord s, Ord t) => Dfa s t -> s -> t -> s
|
||||
dfaTransition a s t =
|
||||
let state = getState a s
|
||||
in case transitions state Map.!? t of
|
||||
(Just nextState) -> nextState
|
||||
Nothing -> defaultTransition state
|
||||
|
||||
dfa :: (Ord s, Ord t) => [(s, [(t, s)], s)] -> s -> [s] -> Maybe (Dfa s t)
|
||||
dfa stateInfo entryState exitStates =
|
||||
let stateList = map (\(s, ts, dt) -> (s, State (Map.fromList ts) dt)) stateInfo
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue