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

  • SEARCH
  • Home
  • 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 7721527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:56:50+00:00 2026-06-01T03:56:50+00:00

I decided to teach myself how to use Parsec , and I’ve hit a

  • 0

I decided to teach myself how to use Parsec, and I’ve hit a bit of a road block with the toy project I assigned myself.

I’m trying to parse HTML, specifically:

<html>
  <head>
    <title>Insert Clever Title</title>
  </head>
  <body>
    What don't you like?
    <select id="some stuff">
      <option name="first" font="green">boilerplate</option>
      <option selected name="second" font="blue">parsing HTML with regexes</option>
      <option name="third" font="red">closing tags for option elements
    </select>
    That was short.
  </body>
</html>

My code is:

{-# LANGUAGE FlexibleContexts, RankNTypes #-}
module Main where

import System.Environment (getArgs)
import Data.Map hiding (null)
import Text.Parsec hiding ((<|>), label, many, optional)
import Text.Parsec.Token
import Control.Applicative

data HTML = Element { tag :: String, attributes :: Map String (Maybe String), children :: [HTML] }
          | Text { contents :: String }
  deriving (Show, Eq)

type HTMLParser a = forall s u m. Stream s m Char => ParsecT s u m a

htmlDoc :: HTMLParser HTML
htmlDoc = do
  spaces
  doc <- html
  spaces >> eof
  return doc

html :: HTMLParser HTML
html = text <|> element

text  :: HTMLParser HTML
text = Text <$> (many1 $ noneOf "<")

label :: HTMLParser String
label = many1 . oneOf $ ['a' .. 'z']  ++ ['A' .. 'Z']

value :: HTMLParser String
value = between (char '"') (char '"') (many anyChar) <|> label

attribute :: HTMLParser (String, Maybe String)
attribute = (,) <$> label <*> (optionMaybe $ spaces >> char '=' >> spaces >> value)

element :: HTMLParser HTML
element = do
  char '<' >> spaces
  tag <- label
  -- at least one space between each attribute and what was before
  attributes <- fromList <$> many (space >> spaces >> attribute)
  spaces >> char '>' 
  -- nested html
  children <- many html
  optional $ string "</" >> spaces >> string tag >> spaces >> char '>'
  return $ Element tag attributes children

main = do
  source : _ <- getArgs
  result <- parse htmlDoc source <$> readFile source
  print result

The problem seems to be that my parser doesn’t like closing tags – it seems to be greedily assuming < always means an opening tag (as far as I can tell):

% HTMLParser temp.html
Left "temp.html" (line 3, column 32):
unexpected "/"
expecting white space

I’ve been playing around with it for a bit, and I’m not sure why it’s not backtracking past the char '<' match.

  • 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-01T03:56:51+00:00Added an answer on June 1, 2026 at 3:56 am

    Like ehird said, I needed to use try:

    attribute = (,) <$> label <*> (optionMaybe . try $ spaces >> char '=' >> spaces >> value) 
    --...
    attributes <- fromList <$> many (try $ space >> spaces >> attribute)
    --...
    children <- many $ try html
    optional . try $ string "</" >> spaces >> string tag >> spaces >> char '>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to teach myself Regexes in python and I decided to
I am trying to teach myself LINQ to SQL and have decided to do
I'm currently trying to understand how the stack works, so I've decided teach myself
In trying to teach myself ASP.NET and OO design in general, I decided to
Decided to use Apache's Common Configuration package to parse an XML File. I decided
We've just newly decided to use NServiceBus on our project and we're experiencing some
I've decided to teach myself Haskell and I have never been so frustrated in
I decided to use Berkeley DB's compaction feature to optimize the size and cache-hit
I decided to teach myself assembly language. I have realized that my program will
I've decided to teach myself how to program android apps, and so got 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.