[Haskell-cafe] Regular Expression to Determinate Finite Automata
S. Doaitse Swierstra
doaitse at swierstra.net
Thu Jul 22 16:10:38 EDT 2010
The simplest way to make a recogniser out of a RE is to use one of the available parsing libraries:
module RE where
data RE = Epsilon | Leaf Char | Selection RE RE | Sequence RE RE | Kleene RE | Optional RE | End
re_to_fsm :: RE -> Parser String
re_to_fsm re = case re of
Leaf c -> lift <$> pSym c
Selection re1 re2 -> re_to_fsm re1 <|> re_to_fsm re2
Sequence re1 re2 -> (++) <$> re_to_fsm re1 <*> re_to_fsm re2
Kleene re -> concat <$> pList (re_to_fsm re)
Optional re -> re_to_fsm re `opt` ""
End -> pure ""
t = re_to_fsm ((Kleene (Leaf 'a') `Sequence` Kleene (Leaf 'b')) `Selection` (Kleene (Leaf 'a') `Sequence` (Kleene (Leaf 'c') )))
t1 = run t "aaabbb"
t2 = run t "aaaaccccccc"
t3 = run t "aaddcc"
test = run (re_to_fsm (Kleene (Leaf 'a') `Sequence` Kleen (Left 'b')) "aaabbb"
-- > Result: "aaabbb"
-- > Result: "aaaaccccccc"
-- > Result: "aacc"
-- > Correcting steps:
-- > Deleted 'd' at position 2 expecting one of ['a', 'c', 'a', 'b']
-- > Deleted 'd' at position 3 expecting 'c'
On 22 jul 2010, at 20:51, Aaron Gray wrote:
> I am a Haskell newbie. I have coded a Regular Expression to Determinate Finite Automata translator. Algorithm from the Dragon Book.
> Would someone eyeball the code and give me suggestions please.
> I have not done anything on character classes yet though. And the parsing is a bit of a hack.
> What I am not sure about is having to have multiple versions of similar datatype, each with variations in order to enumerate and generate followPos set.
> Is there a better way of implementing this ?
> Many thanks in advance,
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe