Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:10:59+00:00 2026-06-07T06:10:59+00:00

Say I have two languages (A & B). My goal is to write some

  • 0

Say I have two languages (A & B). My goal is to write some type of program to convert the syntax found in A to the equivalent of B. Currently my solution has been to use Haskell’s Parsec to perform this task. As someone who is new to Haskell and functional programming for that matter however, finding just a simple example in Parsec has been quite difficult. The examples I have found on the web are either incomplete examples (frustrating for a new Haskell programmer) or too much removed from my goal.

So can someone provide me with an amazingly trivial and explicit example of using Parsec for something related to what I’d like to achieve? Or perhaps even some tutorial that parallels my goal as well.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:11:00+00:00Added an answer on June 7, 2026 at 6:11 am

    Consider the following simple grammar of a CSV document (In ABNF):

    csv   = *crow
    crow  = *(ccell ',') ccell CR
    ccell = "'" *(ALPHA / DIGIT) "'"
    

    We want to write a converter that converts this grammar into a TSV (tabulator separated values) document:

    tsv   = *trow
    trow  = *(tcell HTAB) tcell CR
    tcell = DQUOTE *(ALPHA / DIGIT) DQUOTE
    

    First of all, let’s create an algebraic data type that descibes our abstract syntax tree. Type synonyms are included to ease understandment:

    data XSV  = [Row]
    type Row  = [Cell]
    type Cell = String
    

    Writing a parser for this grammar is pretty simple. We write a parser as if we would describe the ABNF:

    csv :: Parser XSV
    csv = XSV <$> many crow
    
    crow :: Parser Row
    crow = do cells <- ccell `sepBy` (char ',')
              newline
              return cells
    
    ccell :: Parser Cell
    ccell = do char '\''
               content <- many (digit <|> letter)
               char '\''
               return content
    

    This parser uses do-notation. After a do, a sequence of statements follows. For parsers, these statements are simply other parsers. One can use <- to bind the result of a parser. This way, one builds a big parser by chaining multiple smaller parsers. To obtain interesting effects, one can also combine parser using special combinators (such as a <|> b, which parses either a or b or many a, which parses as many as as possible). Please be aware that Parsec does not backtrack by default. If a parser might fail after consuming characters, prefix it with try to enable backtracking for one instance. try slows down parsing.

    The result is a parser csv that parses our CSV document into an abstract syntax tree. Now it is easy to turn that into another language (such as TSV):

    xsvToTSV :: XSV -> String
    xsvToTSV xst = unlines (map toLines xst) where
      toLines = intersperse '\t'
    

    Connecting these two things one gets a conversion function:

    csvToTSV :: String -> Maybe String
    csvToTSV document = case parse csv "" document of
      Left _    -> Nothing
      Right xsv -> xsvToTSV xsv
    

    And that is all! Parsec has lots of other functions to build up extremely sophisticated parsers. The book Real World Haskell has a nice chapter about parsers, but it’s a little bit outdated. Most of that is still true, though. If you have further questions, feel free to ask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have localized a Form say BanksForm into two languages by setting the Localizable
Say we have two activities, Activity1 and Activity2. In Activity1's onClick() method, we have
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port
Say I have two dates: minDate:2012-08-29 12:22:17 +0000 maxDate:2011-12-01 18:14:38 +0000 I have a
Say we have two tables in an MS Access db: Service Users: | ID
Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
Say you have two classes, A and B. Is it possible to instantiate both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.