我是Haskell的新手,这只是我的第二个应用,所以如果我的问题的答案显而易见,请原谅我。
我试图实现Thompson的RegExp代码来实现正则表达式的NFA。到目前为止,我的应用程序有2个输入:1)第一行 - 要实现的表达式的数量(转换为Int),以及第二行 - 要实现的正则表达式。稍后我将在与要执行的正则表达式相同的行上输入第三个输入。这意味着我需要学习如何解析第二个输入字符串以获取字符串和整数。但我还是找不到如何解析IO String。我想这是以后的另一个问题,或者有人可以提供如何做到这一点的链接。输入将如下所示:
(int input)
regex1 (int input)
regex2 (int input)
regex3 (int input)
这里是相关的代码......还有很多,但只显示相关的代码。我现在得到的错误是"没有解析:读"当我为第二个输入(第二行)输入任何内容时。第二个输入现在没有使用,因为我暂时硬编码了第二个输入。我在Leksah,代码配置和编译。有人能告诉我为什么我在运行应用程序时遇到解析错误吗?
data Reg = Epsilon |
Literal Char |
Or Reg Reg |
Then Reg Reg |
Star Reg
deriving Eq
--------------------------------------------------------------------------
-- --
-- ImplementNfa.hs
--
--------------------------------------------------------------------------
trans :: Ord a => Nfa a -> String -> Set a
trans mach str = foldl step startset str
where
step set ch = onetrans mach ch set
startset = closure mach (sing (startstate mach))
--------------------------------------------------------------------------
-- Turn the result of trans into printable form. --
--------------------------------------------------------------------------
print_trans :: Nfa Int -> String -> [Char]
print_trans mach str = show (flatten (trans mach str))
buildRE::String -> Reg
buildRE r = regexp2 -- will build regex from "re"
-- hard coded for now until I
-- learn how to parse a string
re = "(ab|ba)" -- Hard coded for now until I can
-- learn how to parse an input string
a = Literal 'a'
b = Literal 'b'
regexp2 = Or (Then a b) (Then b a)
main::IO()
main = do input1 <- getLine
let t = (read input1::Int)
let t1 = t -1
-- Maybe I need a loop here for "t" but I don't know how
if (t1 == -1)
then return()
else
do input <- getLine
-- Need to figure out how to parse input
-- to get string 're' and an Int
let r = buildRE re
let nfa1 = build r
putStrLn $ (print_trans nfa1 re);