嗨,我的正则表达式具有以下数据类型:
data RE a -- regular expressions over an alphabet defined by 'a'
= Empty -- empty regular expression
| Sym a -- match the given symbol
| RE a :+: RE a -- concatenation of two regular expressions
| RE a :|: RE a -- choice between two regular expressions
| Rep (RE a) -- zero or more repetitions of a regular expression
| Rep1 (RE a) -- one or more repetitions of a regular expression
deriving (Show)
,我需要设计一种方法来检查该正则表达式是否包含Empty字符串。例如(Rep(Sym'a':+:Sym'b'))为true,而(Sym'a')为false。 我将如何使该方法返回布尔值?
答案 0 :(得分:4)
计算Salomaa的“空词”属性(有时也称为“可空”)通常是通过将正则表达式放入正则表达式中来完成的,该正则表达式利用代数属性来删除多余的Kleene星,然后检查是否还有星运算符表达式,或者表达式是否具有或本身就是您定义的Empty
。这是example。
-- decide if the language defined by r contains ε, i.e.
-- nullable (r) ⇔ ε ∈ ℒ(r)
-- Also know as Salomaa's Empty Word Property (EWP)
nullable ∷ (Ord s) ⇒ RegExp s → Bool
nullable = nullable' . normalize
where nullable' Zero = False
nullable' One = True
nullable' (Lit _) = False
nullable' (α :| β) = nullable' α || nullable' β
nullable' (α :. β) = nullable' α && nullable' β
nullable' (Star _) = True
为Rep1
添加一个保护套看起来像nullable' (Rep1 α) = nullable' α
。