parse-dimacs 1.0.1 → 1.1
raw patch · 3 files changed
+42/−36 lines, 3 files
Files
- CHANGES +10/−0
- Language/CNF/Parse/ParseDIMACS.hs +28/−34
- parse-dimacs.cabal +4/−2
+ CHANGES view
@@ -0,0 +1,10 @@+-*- mode: outline -*-++* 1.1 (21 Mar 2008)+Parser allows clauses to be split over multiple lines.++* 1.0.1+Cabal description fix++* 1.0+Initial release
Language/CNF/Parse/ParseDIMACS.hs view
@@ -17,13 +17,14 @@ -- | A simple Parsec module for parsing CNF files in DIMACS format. module Language.CNF.Parse.ParseDIMACS- (parseCNF, CNF(..)) where+ ( parseCNF, CNF(..) ) where import Text.ParserCombinators.Parsec data CNF = CNF {numVars :: Int ,numClauses :: Int ,clauses :: [[Int]]}+ deriving Show parseCNF :: String -- ^ The filename. Used to report errors. -> String -- ^ The contents of the CNF file.@@ -33,30 +34,37 @@ Left parseError -> error $ "Parse error: " ++ show parseError Right cs -> cs +-- A DIMACS CNF file contains a header of the form "p cnf <numVars>+-- <numClauses>" and then a bunch of 0-terminated clauses. cnf :: Parser CNF cnf =- do skipComments- char 'p'- spaces- string "cnf"- spaces- numVars <- many1 digit- spaces+ do many comment+ char 'p' ; spaces+ string "cnf" ; spaces+ numVars <- many1 digit ; spaces numClauses <- many1 digit- manyTill space (try newline)- actualClauses <- manyClause- return $ CNF (read numVars) (read numClauses) (actualClauses)+ space `manyTill` newline+ actualClauses <- many1 clause+ return $ CNF (read numVars) (read numClauses) actualClauses -skipComments :: Parser ()-skipComments =- do try (do char 'c'- _ <- manyTill anyChar (try (char '\n'))- skipComments)- <|> return ()+comment :: Parser String+comment = do char 'c' ; manyTill anyChar (try newline) +clause :: Parser [Int]+clause = do many (space <|> newline)+ lits <- between (string "") (char '0') (many1 intSpaces)+ many (space <|> newline)+ return lits++-- Consume all whitespace after the int so the `between' in `clause' matches+-- on "0" at the end.+intSpaces = do i <- int + many1 (space <|> newline)+ return i+ int :: Parser Int int = do parity <- option '+' $ choice $ map char "+-"- first <- nonZeroDigit+ first <- posDigit rest <- many digit return . read $ case parity of@@ -64,20 +72,6 @@ '-' -> '-' : first : rest _ -> error $ "unknown parity syntax: " ++ [parity] -nonZeroDigit :: Parser Char-nonZeroDigit = oneOf ['1'..'9']--intSpaces = do i <- int- spaces- return i--clause :: Parser [Int]-clause =- do spaces- lits <- between (string "") (char '0') (many1 intSpaces)- newline- return $ lits--manyClause :: Parser [[Int]]-manyClause = many1 clause+posDigit :: Parser Char+posDigit = oneOf ['1'..'9']
parse-dimacs.cabal view
@@ -1,15 +1,17 @@ Name: parse-dimacs-Version: 1.0.1+Version: 1.1 Synopsis: DIMACS CNF parser library Description: A DIMACS CNF parser library, implemented with Parsec. Category: Parsing License: LGPL License-file: COPYING Author: Denis Bueno-Maintainer: <dbueno@gmail.com>+Maintainer: Denis Bueno <dbueno@gmail.com> Stability: provisional Build-Depends: base, parsec Build-type: Simple+Tested-with: GHC==6.8.2+Extra-source-files: CHANGES Exposed-modules: Language.CNF.Parse.ParseDIMACS Ghc-options: -W