I realized the other day that I’ve only made one post in the “coding” category. I don’t spend as much of my free time writing code as I used to, but I figured I still do enough that it deserves more than one post. So here’s a quick write up about the latest mini-project I’ve been working on.
I’ve been implementing a (very) simple regular expression library in Python. It doesn’t really compare to the standard library’s ‘re’ module or to Perl’s regular expressions, but I’m mainly doing it to learn the underlying algorithms, and those are similar in the more powerful implementations.
To be honest, the project is mostly a rewrite of Haskell code I wrote and forgot about a few years back. I wanted to add new features, but I didn’t want to use Haskell, so I thought I’d rewrite the whole thing in Python. I got a little ahead of myself, though, and started adding new regex syntax before all of the old features were done. The table below (from the Github project Wiki) shows the supported regex syntax in both version:
|Zero or one||a?||X|
|Closure (zero or more)||a*||X||X|
|One or more||a+||X||X|
|POSIX named character classes||[:alpha:]||X|
|Negative character classes||[^a-z]|
For most people, the only remotely interesting part of the project are probably the neat finite automata graphs it can produce using GraphViz:
The Wikipedia page for finite automata does a pretty good job explaining what the graph actually means.
There’s only one part of the Haskell code I still need to rewrite: the NFA to DFA conversion routine. I’m embarrassed to admit it, but I’ve been putting it off because my original Haskell code is so bad. I’m tempted to say it’s the worst code I’ve ever written:
-- Convert an Nfa to a Dfa using the algorithms from section 3.7.1 of the "Dragon book"toDfa :: Nfa -> DfatoDfa nfa =letdstrt = e_closure nfa (n_start nfa)init = Map.fromList [(dstrt, genStates dstrt)]dfa = getps initstateNames = genStateMap dfapartial = Map.mapKeys (\x -> lookupDefault x 0 stateNames) dfadfaTrans = Map.map (\x -> Map.map (\y -> (lookupDefault y 0 stateNames)) x) partialaccepting = Set.fromList(Map.elems(Map.filterWithKey(\k x ->(Set.size (Set.intersection (n_accepting nfa) k)) > 0)stateNames))inDfa dfaTrans (n_start nfa) accepting (n_rgx nfa)wheregetps old =if (new == old)then newelse (getps new)wherenew = generateProductions oldgenerateProductions :: Map.Map (StateSet) (Map.Map NfaChar (StateSet))-> Map.Map (StateSet) (Map.Map NfaChar (StateSet))generateProductions fad = foldl(\acc x ->letgt = (genStates x)inMap.insert x gt acc) fad (getProductionStates fad)genStateMap :: Map.Map (StateSet) (Map.Map NfaChar (StateSet))-> Map.Map (StateSet) IntgenStateMap dfa = foldl(\acc x ->let(y, sm) = mapState acc xinsm) Map.empty (Map.keys dfa)getProductionStates fad = (concat(map(\x -> Map.elems x)(Map.elems fad)))mapState sm set = case Map.lookup set sm ofJust y -> (y, sm)Nothing -> let ms = Map.size smin (ms, Map.insert set ms sm)genStates set = Map.fromList (filter (\(x,y) -> not (Set.null y))(Set.toList(Set.map(\ch -> (ch, e_closure_set nfa (nfa_move nfa set ch)))inSyms)))inSyms = Set.delete Epsilon (Set.fromList (Map.fold (\x acc -> acc ++ Map.keys x)  (n_transitions nfa)))
I’m actually a bit disappointed in myself for turning the compiler book’s 10 lines of psuedo-code into that mess. All I know is that when I finally get around to writing the Python version, it will be nothing like that.
The latest code is available at GitHub.