Compilation
As long as we have implemented a sufficient level of optimization, every regular expression has only finitely many different derivatives. We can use this fact to build a DFA from any regular expression (where the minimality of the DFA depends on how much optimization we do to our regular expressions).
To Do
In
src/Compilation.hs, implement theallDerivativesfunction.The output of
allDerivativesshould always include the given regular expression (which is considered to be the derivative of the regular expression with respect to the empty string). Then you can take all its character derivatives, all their character derivatives, and so on, until no new regular expressions appear.One good way to solve this problem efficiently is with a so-called “worklist” algorithm, where you have a recursive helper function with an accumulator argument (all the derivatives you’ve found so far) and an argument containing a stack/queue/list of work (e.g., regular expression derivatives waiting to be possibly added to the accumulator). The helper can repeatedly take one item off the worklist and see if it generates any further work: if you haven’t seen this regular expression before, add all its derivatives to the worklist to be investigated further. Repeat until the worklist is empty (e.g., everything on the worklist had been seen before, so you didn’t need to add any new derivatives back to the worklist).
Test your function, perhaps using these example calls:
allDerivatives ['a','b'] (Concat [Letter 'a', Letter 'b']) allDerivatives ['a','b'] regex1 allDerivatives ['a','b'] regex2 allDerivatives ['a','b'] regex3If your code seems to be hanging, then either it is repeatedly computing the same derivative(s) more than once, or there is a bug in your derivative building / optimization code.
At this point, you should be able to use the provided function
generatePdfto draw DFAs for any given regular expression. This function takes a boolean (whether to simply number the states in the drawing, or to leave the potentially large regular expressions) and the alphabet and the regular expression, and creates a filedfa.pdfwith a drawing of the derivative-based DFA, e.g.,generatePdf False ['a','b'] astar_b generatePdf False ['a','b'] notabstar_or_ab generatePdf True ['a','b'] notabstar_or_ab generatePdf False['a','b'] r3 generatePdf True ['a','b'] r3ImportantThe
generatePdffunction relies on your environment having thedotprogram from the Graphviz package (in your path). You can download Graphviz from graphviz.org or install via your favorite package manager (e.g.,brew install graphvizon macOS).To view the generated PDFs, you can open them on your machine. Alternatively, you can view them in VSCode with the vscode-pdf extension.