Professional Documents
Culture Documents
Haskell 04
Haskell 04
-- Task 1 -----------------------------------------
-- G = (VN, VT, P, S) VN = {S, A}, VT = {a, b},
-- P = {S -> aSbAa, S -> b, A -> baAS, A -> a}
analyseG :: String -> Bool
analyseG st1 = case s st1 of
Just st2 -> null st2
Nothing -> False
-- Task 3 -----------------------------------------
-- G1 = ({E,F,A}, VT, P1, E)
-- P1 = {E -> FA, A -> +FA, A -> -FA, A -> *FA, A -> ε,
-- F -> (E), F -> 0,..., F -> 9 }
-- Task 4 -----------------------------------------
-- G1 = ({E,F,A}, VT, P1, E)
-- P1 = {
-- E -> FA, A -> +FA, A -> -FA,
-- A -> *FA, A -> ε, F -> (E), F -> 0,..., F -> 9
-- }
evalLeft :: String -> Maybe Int
evalLeft st1 = case le st1 of
Just (v,st2) | null st2 -> Just v
_ -> Nothing
-- Task 5 -----------------------------------------
-- G2 = ({E,F,A}, VT, P2, E)
-- P2 = {
-- E -> FA, A -> +E, A -> -E, A -> *E, A -> ε,
-- F -> (E), F -> 0,..., F -> 9
-- }
evalRigth :: String -> Maybe Int
evalRigth st1 = case re st1 of
Just (v,st2) | null st2 -> Just v
_ -> Nothing
re :: String -> Maybe (Int,String)
re st1 = case rf st1 of
Just (v1,st2) -> ra (v1, st2)
Nothing -> Nothing
-- Task 6 -----------------------------------------
-- G3 = ({E,T,A,F,B}, VT, P3, E)
-- P3 = {
-- E -> TA, A -> +TA, A -> -TA, A -> ε , T -> FB,
-- B -> *FB, B -> ε, F -> (E), F -> 0,..., F -> 9
-- }
evalPrior :: String -> Maybe Int
evalPrior st1 = case pe st1 of
Just (v,st2) | null st2 -> Just v
_ -> Nothing
------------------------------------------------------
match :: Char -> Maybe String -> Maybe String
match c (Just (t:st)) | c==t = Just st
match _ _ = Nothing