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 8896807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:10:19+00:00 2026-06-15T00:10:19+00:00

The code below outputs Right [1<!>2<!>3] , but I need Right [1, 2, 3]

  • 0

The code below outputs Right ["1<!>2<!>3"], but I need Right ["1", "2", "3"].

import Text.ParserCombinators.Parsec

response = contents :: CharParser () [String]
  where
    contents = sepBy content contentDelimiter
    contentDelimiter = string "<!>"
    content = many anyChar

main = do
  putStrLn $ show $ parse response "Response" "1<!>2<!>3"

I suppose the problem here is that the content parser consumes all the input before sepBy gets to test the delimiter. So, my questions are:

  1. Am I correct with my assumption? If not, what is the mistake I’ve made?

  2. What solution would you recommend for such a problem? (Using Parsec)

* content has to match any string not containing the delimiter. The 1<!>2<!>3 is just an example it can be dslkf\n><!>dsf<!>3 or whatever

  • 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-15T00:10:20+00:00Added an answer on June 15, 2026 at 12:10 am

    For your first example, you would replace

    content = many anyChar
    

    with

    content = many digit
    

    So that the parser of the content doesn’t erroneously match the separator.

    Maybe you want to match more than just digits but even so, I advise you to think carefully about what is valid between <!>s and write a parser that does that.

    Why?
    Once you’ve got a really good parser for content, your definition for response will be perfect. This way your content can include mystring = "hello<!>mum" without being chopped by the top level parser – the low level stringLiteral parser will eat the whole "hello<!>mum" and the top level parser will never see the <!> correctly and innocently included inside it.

    Generally,…
    In most parsing situations it’s best to be really clear what’s allowed in your content, and parse only that, for three reasons:

    • reusability (you can then use this within a larger parser)
    • correctness
    • usually efficiency – if you avoid too much lookahead, your parser runs faster.

    Reusability is important. At the moment, if you use a parser that just splits on <!> and eats everything else, it’s guaranteed to eat the whole input, and you won’t be able to do any more parsing.

    Bottom-up
    Your parsers should work from the ground up – you described this very well in your comment as “stacking the parsers from specific to general”.

    It’s easiest to write them in that order for ease of testing, so first write one that matches a stringChar then stringLiteral before member before array before object before json before content then response. You can have them calling each other recursively along the way. You can then use parseTest to test each little one as you do along; typing parseTest response "1<!>2<!>3" into ghci is quicker than rewriting main and compiling.

    Top-down?
    It’s not wrong to write your parser top-down, just harder. You can write

    response = many $ content `sepBy` contentSeparator
    content = json <|> somethingElse
    json = object <|> array
    array = ...
    

    but nothing is testable until you’ve written the very smallest parser.

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

Sidebar

Related Questions

I wrote the code below to output the figure but still need to figure
Right now the code below produces the output below it, but how would I
I want to get output like 0 1 , but the code below just
I am newbie to C. I googled alot regarding the output of below code.But
For the code below, what do I need to change (in my regex?) so
The following code outputs 0,1,32,33. Which is counter intuitive to say the least. But
How can I get the code below to output node names that I define?
I'm having some problems trying to get the code below to output the data
In the code below i was expecting another output! : #include <stdio.h> #include <stdlib.h>
In the code below, the output is : S1S2. Why do we get the

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.