The interpreter consists of the following phases:
- Parse a regular expression
- Create an NFA for it by walking the produced parse tree
- Convert the NFA to an equivalent DFA
- Compute the DFA's result by feeding it the input string
The following algorithms have been implemented:
- NFA creation: Thompson's construction algorithm
- NFA to DFA conversion: Powerset construction
The interpreter expects regular expressions to conform to the following syntax:
x, a single character."_", the empty symbol.".", any character.(r),ris a regular expression.r1 | r2,r1andr2are regular expressions (Union).r1r2,r1andr2are regular expressions (Concatenation).r*,ris a regular expression (Kleene star).
The following functions can be used after loading RegInterpreter.hs:
makeNfareceives a regular expression ([Char]) & returns an NFA (Fsa).nfaToDfareceives an NFA (Fsa) & returns a DFA (Fsa).regexFullMatchreceives a tuple(regex, string)& returnsTrueifstringis accepted byregex.regexPartMatchreceives a tuple(regex, string)& returns all prefixes instringaccepted byregex.
➜ Regex-Interpreter git:(main) ghci testSuite.hs
GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help
[1 of 3] Compiling RegParser ( RegParser.hs, interpreted )
[2 of 3] Compiling RegInterpreter ( RegInterpreter.hs, interpreted )
[3 of 3] Compiling Main ( testSuite.hs, interpreted )
Ok, three modules loaded.
ghci> testAll
True