regex - Haskell getArgs changing data type -
I am trying to create a main function for the Haskell module which will take a regular expression from the user and use it for SimplifyRegExp function But it wants the input type RegExp:
data RegExp sy = Empty | Epsilon | Literally c. | Or (RegExp SY). Then (RegExp sy).
How do I enable a string to type in RegExp?
If I load the program on GHCI, then I can call the method directly like the following:
* Language Hilix RegxP & gt; SimplifyRegExp (Star (Star A)) 'A' *
But I would like to do this so that I can pass the program in a logic at the command prompt and the result will print something like this ( Which does not work at all):
main = do n & lt; - GetArgs $ head print (simplifyRegExp (n))
Read
can define examples and use it
data regEx SY = ... deriving read
And then use the readMay
import text. Read ... main = Do regexp & lt; - (readMay Head) `fmap` getArgs case's regexp bus r - & gt; ... nothing - & gt; PutStrLn "Parse error!"
But it is slightly brittle in two ways. The first is that read
is a partial task! If regexp is sick, your program will blow up. Second, using your default read
example, your internal rendering on your users is recovered as regexs! If this is a serious project then you would be better off doing something really parsing.
Fortunately, there are some very awesome parsing libraries in Haskell, and some of the most famous are included.
An example of a parser parser can be
import text. Reader Import Text. Parasetting string import control. Functional parseStar :: Parsec (RegExp char) parseStar = Star & lt; $ & Gt; (Parsere & Lt; * Four '*') parseLiteral :: Parsec (RegExp Char) parseLiteral = Literal & lt; $ & Gt; NoneOf "* ()" parseOr :: Parsec (RegExp variable) parseOr = or & lt; $ & Gt; ParseRe & lt; * & Gt; (Four '|' * * Parsi) Pars: Then: Parsek (Reg Xp Charts) Parsche = Then & lt; $ & Gt; ParseRe & lt; * & Gt; ParseRe ....
Comments
Post a Comment