Páginas: 2 (395 palavras) Publicado: 12 de abril de 2013
limport Char import Prelude hiding ((>>=)) --importa a biblioteca prelude e oculta os termos >>= --

type Parser a = String -> [(a, String)] --declaracao dos tipos--

analisa

:: Parser a ->String -> [(a,String)]

analisa p inp = p inp

(>>=) :: Parser a -> (a -> Parser b) -> Parser b tupla e a ordem de prioridades -p >>= f = \inp -> case analisa p inp of [] -> []

[(v,out)] -> analisa (f v) out

(+++) :: Parser a -> Parser a -> Parser a p +++ q = \inp -> case analisa p inp of [] -> analisa q inp

[(v,out)] -> [(v,out)]

sat :: (Char -> Bool)-> Parser Char sat p = item >>= \x -> if p x then mretorno x else falha

digito :: Parser Char digito = sat isDigit

--funcoes para verificar as entradas --

lowercase :: Parser Char lowercase =sat isLower

uppercase :: Parser Char uppercase = sat isUpper

letra :: Parser Char letra = sat isAlpha

alphanum :: Parser Char alphanum = sat isAlphaNum

char

:: Char -> Parser Charchar x = sat (== x)

string string []

:: String -> Parser String = mretorno []

string (x:xs) = char x >>= \_ -> string xs >>= \_ -> mretorno (x:xs)

many :: Parser a -> Parser [a] many p =many1 p +++ mretorno [] many1 :: Parser a -> Parser [a] many1 p = p >>= \v ->

many p >>= \vs ->

mretorno (v:vs)

mretorno :: a -> Parser a -- as funcoes parser pega as strings e retorna arepresentacao correspondente -mretorno v = \inp -> [(v,inp)]

falha :: Parser a falha = \inp -> []

--verificacao--

item :: Parser Char item = \inp -> case inp of [] -> []

--entrada na fila --(x:xs) -> [(x,xs)]

ident :: Parser String ident = lowercase >>= \x ->

many alphanum >>= \xs -> mretorno (x:xs) nat :: Parser Int nat = many1 digito >>= \xs -> mretorno (read xs) space :: Parser() space = many (sat isSpace) >>= \_ -> mretorno ()

token :: Parser a -> Parser a token p = space >>= \_ -> p >>= \v ->

space >>= \_ -> mretorno v

identifier :: Parser String identifier =...

Por favor, assinar para o acesso.