Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created February 21, 2012 16:48
Show Gist options
  • Save MgaMPKAy/1877328 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/1877328 to your computer and use it in GitHub Desktop.
try to implement VBF.Compilers.Scanners.RegularExpression;
{-# LANGUAGE OverloadedStrings #-}
module Regex where
import GHC.Exts(IsString(..))
data Regex
= Empty
| Concatention Regex Regex
| Alternation Regex Regex
| KleeneStar Regex
| Symbol String
instance IsString Regex where
fromString = Symbol
instance Show Regex where
show Empty = ""
show (Concatention r1 r2) = show r1 ++ show r2
show (Alternation r1 r2) = show r1 ++ "|" ++ show r2
show (KleeneStar r) = "[" ++ show r ++ "]*"
show (Symbol s) = s
infix 5 <+>
(<+>) :: Regex -> Regex -> Regex
a <+> b = Concatention a b
infix 4 <|>
(<|>) :: Regex -> Regex -> Regex
a <|> b = Alternation a b
many :: Regex -> Regex
many = KleeneStar
re :: Regex
re = many $ "a" <|> (many $ "b" <+> "a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment