parsing - Haskell parsec gives <<loop>> error -


i've been trying write parser typed lambda calculus using parsec keeps getting stuck in loop resulting in <> error. seems fine me; have misunderstood parsec.

{-# language unicodesyntax #-}  module parser  import semantics ( nmterm(..)                  , ty(..))  import text.parsercombinators.parsec (parser(..)                                      , parseerror                                      , try                                      , oneof                                      , char                                      , digit                                      , satisfy                                      , many1                                      , choice                                      , chainl1                                      , alphanum                                      , eof                                      , letter                                      , parse)  import qualified text.parsec.token t import qualified text.parsec.language l import qualified text.parsec.expr e import control.applicative ((<|>))  ------------ -- lexing -- ------------  lexer ∷ t.tokenparser () lexer = t.maketokenparser         $ l.emptydef { t.identstart      = letter                      , t.identletter     = alphanum                      , t.reservedopnames = ["lambda", ".", ":", "->"]                      , t.reservednames   = ["true", "false", "bool"]                      , t.opletter        = oneof ".:"                      }  parens ∷ parser → parser parens = t.parens lexer  natural ∷ parser integer natural = t.natural lexer  reserved ∷ string → parser () reserved = t.reserved lexer  reservedop ∷ string → parser () reservedop = t.reservedop lexer  identifier ∷ parser string identifier = t.identifier lexer  whitespace ∷ parser () whitespace = t.whitespace lexer  ------------------------------------------------------------------------------- -------------------------------------- parsing -------------------------------- ------------------------------------------------------------------------------- variable ∷ parser nmterm variable = identifier >>= \x → return $ nmvar x  true ∷ parser nmterm true = reserved "true" >> return nmtrue  false ∷ parser nmterm false = reserved "false" >> return nmfalse  bool ∷ parser nmterm bool = true <|> false  boolty ∷ parser ty boolty = reserved "bool" >> return tybool  arrty ∷ parser ty arrty =   τ₁ ← anytype   whitespace   reservedop "->"   whitespace   τ₂ ← anytype   return $ tyarr τ₁ τ₂  anytype ∷ parser ty anytype = arrty <|> boolty  abstraction ∷ parser nmterm abstraction =   reservedop "lambda"   whitespace   x ← identifier   reservedop ":"   τ ← anytype   reservedop "."   whitespace   body ← expr   return $ nmabs x τ body  expr ∷ parser nmterm expr =  abstraction     <|> variable     <|> bool  parseexpr ∷ string → nmterm parseexpr t = case parse expr "" t of                 left err  → error $ show err                 right ast → ast 

it might if more specific error message. suspect problem have left-recursive grammar: example, arrty can start anytype, can arrty.

when implemented directly in top-down parser (which includes combinator parsers such parsec), kind of feature cause infinite loop. parsec provides various facilities work around problem; however, convenient way solve particular problem may require re-work of grammar.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -