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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:08:07+00:00 2026-05-13T22:08:07+00:00

I am trying to parse F# type syntax. I started writing an [F]Parsec grammar

  • 0

I am trying to parse F# type syntax. I started writing an [F]Parsec grammar and ran into problems, so I simplified the grammar down to this:

type ::= identifier | type -> type
identifier ::= [A-Za-z0-9.`]+

After running into problems with FParsec, I switched to Parsec, since I have a full chapter of a book dedicated to explaining it. My code for this grammar is

typeP = choice [identP, arrowP]
identP = do
   id <- many1 (digit <|> letter <|> char '.' <|> char '`')
   -- more complicated code here later
   return id
arrowP = do
  domain <- typeP
  string "->"
  range <- typeP
  return $ "("++domain++" -> "++range++")"
run = parse (do t <- typeP
                eof
                return t) "F# type syntax"

The problem is that Parsec doesn’t backtrack by default, so

> run "int"
Right "int"
-- works! 
> run "int->int"
Left "F# type syntax"
unexpected "-"
expecting digit, letter, ".", "`" or end of input
-- doesn't work!

The first thing I tried was to reorder typeP:

typeP = choice [arrowP, identP]

But this just stack overflows because the grammar is left-recursive–typeP never gets to trying identP because it keeps trying arrowP over and over. Next I tried try in various places, for example:

typeP = choice [try identP, arrowP]

But nothing I do seems to change the basic behaviours of (1) stack overflow or (2) non-recognition of “->” following an identifier.

My mistake is probably obvious to anybody who has successfully written a Parsec grammar. Can somebody point it out?

  • 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-05-13T22:08:07+00:00Added an answer on May 13, 2026 at 10:08 pm

    I think the problem is that, and I’m making an assumption for F# (because I don’t know it), arrows are right associative. I’m not sure how precise the linked grammar is supposed to be, as I’m not well versed in different grammars. But if we can assume arrows are right associative that makes the problem easier.

    So with that assumption we can trivially do:

    identP = many1 (digit <|> letter <|> char '.' <|> char '`')
    
    typeP = try arrowP <|> identP
    
    arrowP = do
      i <- identP
      string "->"
      t <- typeP
      return $ "(" ++ i ++ " -> " ++ t ++ ")"
    
    run = flip parse "F# type syntax" $ do
            t <- typeP
            eof
            return t
    

    So:

    Haskell> run "int"
    Right "int"
    Haskell> run "int->int"
    Right "(int -> int)"
    Haskell> run "int->int->int->int"
    Right "(int -> (int -> (int -> int)))"
    

    Expanding further, what might be confusing you is that in that grammar it says type -> type, which means you could have an arrow on the left side. That’s fine, but it needs to be in parentheses. Which helps, maybe seeing the following in action is helpful. It helped me.

    typeP = try arrowP <|> parens typeP <|> identP
    
    arrowP = do
     i <- parens typeP <|> identP
     string "->"
     t <- typeP
     return $ "(" ++ i ++ " -> " ++ t ++ ")"
    
    parens p  = between (char '(') (char ')') p
    

    Now we can write arrows on the left or the right side of an arrow:

    Haskell> run "int->int->int"
    Right "(int -> (int -> int))"
    Haskell> run "(int->int)->int"
    Right "((int -> int) -> int)"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an example raw email I am trying to parse: MIME-version: 1.0 Content-type:
I am trying to parse this XML file with this type of layout: <POX>
I'm trying to parse this type of CSV file with FileHelpers: Tom,1,2,3,4,5,6,7,8,9,10 Steve,1,2,3 Bob,1,2,3,4,5,6
Trying to parse some XML but apparently this is too much for a lazy
I have a date of the type August 15, 2009 I'm trying to parse
While trying to parse an xml file into table format, jQuery keeps closing my
I am trying to parse a json string straight into a managed object. The
I'm trying to parse an RSS feed using LINQ to Xml This is the
I'm trying to parse the xml document returned from this link but I get
I'm trying to parse a large number of short strings into some logical parts.

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.