A problem with a simple csv file parser

Hi all - I'm just trying to get the simple csv file parser from "Real World Haskell" working. However, I've got a problem. Here's the code - -- A simple csv parser Module parseCSV where import Text.ParserCombinators.Parsec csvFile = endBy line eol line = sepBy cell (char ',') cell = many (noneOf ",\n") eol = char '\n' parseCSV :: String -> Either ParseError [[String]] parseCSV input = parse csvFile "(unknown)" input ( Btw, I'm running Ubuntu 10.04, GHC 6.12.1 ) When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get the error - parseCSV.hs:4:16: parse error on input `where' This has me really puzzled - as far as I can see, the first line looks identical to a number of similar programs that I've found (while searching for a solution to this). So, if anyone can let me know what is causing this, that'd be great! Many thanks in advance - - Andy

Hi Andy, If I remember right, runhaskell is expecting a full fledged haskell program with a "main" defined. Instead, you've sent it a module, which it isn't expecting, so you're getting a parse error on the "where". If you remove the "Module parseCSV where" from the top of your file, and try again, you'll get a "not in scope 'main' " error. So you can test this a couple of ways. An easy way would be to open a terminal window and type "ghci", to open up ghc's Interpreter. Then, at the prompt type: :l parseCSV.hs (or whatever the name of your file is) Now, ghci, has loaded up all the functions defined in your file. So now, you can invoke your functions: *Main> parseCSV "a,b,c,d\n" Right [["a","b","c","d"]] If you're really adamant about using runhaskell, what you need to do is define a main function that opens up a file, use hGetContents to read the file as a String, and then invoke parseCSV on that String. In order to take command line arguments, you'll want this: http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-Enviro... Good Luck, Jimmy On 10/16/10 12:23 AM, Andy Elvey wrote:
Hi all -
I'm just trying to get the simple csv file parser from "Real World Haskell" working. However, I've got a problem. Here's the code -
-- A simple csv parser Module parseCSV where import Text.ParserCombinators.Parsec
csvFile = endBy line eol line = sepBy cell (char ',') cell = many (noneOf ",\n") eol = char '\n'
parseCSV :: String -> Either ParseError [[String]] parseCSV input = parse csvFile "(unknown)" input
( Btw, I'm running Ubuntu 10.04, GHC 6.12.1 )
When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get the error - parseCSV.hs:4:16: parse error on input `where'
This has me really puzzled - as far as I can see, the first line looks identical to a number of similar programs that I've found (while searching for a solution to this). So, if anyone can let me know what is causing this, that'd be great! Many thanks in advance - - Andy
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Jimmy - Thanks very much for that! I'll try that - it sounds good to me! Bye for now - - Andy Jimmy Wylie wrote:
Hi Andy,
If I remember right, runhaskell is expecting a full fledged haskell program with a "main" defined. Instead, you've sent it a module, which it isn't expecting, so you're getting a parse error on the "where". If you remove the "Module parseCSV where" from the top of your file, and try again, you'll get a "not in scope 'main' " error.
So you can test this a couple of ways. An easy way would be to open a terminal window and type "ghci", to open up ghc's Interpreter. Then, at the prompt type:
:l parseCSV.hs (or whatever the name of your file is)
Now, ghci, has loaded up all the functions defined in your file. So now, you can invoke your functions:
*Main> parseCSV "a,b,c,d\n" Right [["a","b","c","d"]]
If you're really adamant about using runhaskell, what you need to do is define a main function that opens up a file, use hGetContents to read the file as a String, and then invoke parseCSV on that String.
In order to take command line arguments, you'll want this: http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-Enviro...
Good Luck, Jimmy
On 10/16/10 12:23 AM, Andy Elvey wrote:
Hi all -
I'm just trying to get the simple csv file parser from "Real World Haskell" working. However, I've got a problem. Here's the code -
-- A simple csv parser Module parseCSV where import Text.ParserCombinators.Parsec
csvFile = endBy line eol line = sepBy cell (char ',') cell = many (noneOf ",\n") eol = char '\n'
parseCSV :: String -> Either ParseError [[String]] parseCSV input = parse csvFile "(unknown)" input
( Btw, I'm running Ubuntu 10.04, GHC 6.12.1 )
When I try to run this (using "runhaskell parseCSV.hs test.csv" ) I get the error - parseCSV.hs:4:16: parse error on input `where'
This has me really puzzled - as far as I can see, the first line looks identical to a number of similar programs that I've found (while searching for a solution to this). So, if anyone can let me know what is causing this, that'd be great! Many thanks in advance - - Andy
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sat, 16 Oct 2010 07:23:04 +0200, Andy Elvey
-- A simple csv parser Module parseCSV where import Text.ParserCombinators.Parsec
That's your problem. I'm assuming the newline got chomped up, so your source file reads --A simple csv parser Module parseCSV where That's wrong. Module is *not* to be capitalized, the module *name*, however, *is*. Change it to: module ParseCsv where Runhaskell should be absolutely fine running it that way, no need to remove the module name. GHC (the compiler itself) on the other hand, will just not produce a binary as long as your module name isn't "Main." % runhaskell Test.hs ~ "Hello" % ghc --make -O2 -o Test Test.hs ~ Warning: output was redirected with -o, but no output will be generated because there is no Main module. % cat Test.hs ~ module Test where main = print "Hello" Most programs will typically have a File "Main.hs" which does all the duties of a main executable. Best wishes, Aleks

On Saturday 16 October 2010 10:59:10, Aleksandar Dimitrov wrote:
. GHC (the compiler itself) on the other hand, will just not produce a binary as long as your module name isn't "Main."
If your module name isn't Main, you have to tell GHC what to regard as the main module by passing the "-main-is" flag on the command line $ ghc -O2 --make -main-is Test -o Test Test.hs works. And your main function may also have a different name, e.g. $ ghc -O2 --make -main-is MultiMain.main2 -o main2 MultiMain.hs with module MultiMain where main1 :: IO () main1 = putStrLn "main1" main2 :: IO () main2 = putStrLn "You chose main two."

Hi again all - Thanks very much for that Daniel (and thanks too to Aleksandar!). Your replies have been very helpful! Everything's now working with the parser! Parsec is truly a great library to use. It really does make parsing a joy..... :) Bye for now, and thanks again - - Andy Daniel Fischer wrote:
On Saturday 16 October 2010 10:59:10, Aleksandar Dimitrov wrote:
. GHC (the compiler itself) on the other hand, will just not produce a binary as long as your module name isn't "Main."
If your module name isn't Main, you have to tell GHC what to regard as the main module by passing the "-main-is" flag on the command line
$ ghc -O2 --make -main-is Test -o Test Test.hs
works. And your main function may also have a different name, e.g.
$ ghc -O2 --make -main-is MultiMain.main2 -o main2 MultiMain.hs
with
module MultiMain where
main1 :: IO () main1 = putStrLn "main1"
main2 :: IO () main2 = putStrLn "You chose main two."
participants (4)
-
Aleksandar Dimitrov
-
Andy Elvey
-
Daniel Fischer
-
Jimmy Wylie