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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:51:31+00:00 2026-05-16T18:51:31+00:00

So I am working on constructing a lexer/parser pair using parser combinators which leaves

  • 0

So I am working on constructing a lexer/parser pair using parser combinators which leaves me with some interesting problems. Now the specific problem this question is regarding I have actually solved but I am not completely happy with my solution.

module Program =            

    type Token = { value:string; line:int; column:int; }

    let lineTerminators = set ['\u000A'; '\u000D'; '\u2028'; '\u2029']

    let main () =

        let token = { value = "/*\r\n *\r\n *\r\n \n */"; line = 1; column = 1; }

        let chars = token.value.ToCharArray()

        let totalLines =
            chars
            |> Array.mapi(
                fun i c ->
                    if not (lineTerminators.Contains c)  then 
                        0 
                    else
                        if c <> '\n' || i = 0 || chars.[i - 1] <> '\r' then 
                            1 
                        else 
                            0
            )
            |> Array.sum

        let nextLine = token.line + totalLines

        let nextColumn =
            if totalLines = 0 then 
                token.column + token.value.Length 
            else
                1 + (chars 
                     |> Array.rev 
                     |> Array.findIndex lineTerminators.Contains)


        System.Console.ReadKey true |> ignore

    main()
  • 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-16T18:51:31+00:00Added an answer on May 16, 2026 at 6:51 pm

    One problem with your implementation is that it originally seems to expect that all line terminators are just single characters, but that’s actually not the case – if you treat "\r\n" as a single line terminator (composed from 2 characters), then the situation should be more clear. For example, I would declare terminators like this:

    let terminators = [ ['\r'; '\n']; ['\r']; ['\n'] ]
    

    The order is significant – if we find "\r\n" first then we want to skip 2 characters (so that we don’t count the next ‘\n’ char as the next terminator). Unfortunately "skip 2 characters" is a bit tricky – it cannot be done using mapi function, which calls the function for each element.

    A direct implementation using a recursive function could look like this:

    let input = "aaa\nbbb\r\nccc" |> List.ofSeq
    
    // Returns Some() if input starts with token (and the returned
    // value is the rest of the input with the starting token removed)
    let rec equalStart input token = 
      match input, token with
      | _, [] -> Some(input) // End of recursion
      | a::input, b::token when a = b -> equalStart input token // Recursive call
      | _ -> None // Mismatch
    
    // Counts the number of lines...
    let rec countLines count input = 
      // Pick first terminator that matches with the beginning of the input (if any)
      let term = terminators |> List.tryPick (equalStart input)
      match term, input with 
      | None, _::input -> countLines count input  // Nothing found - continue
      | None, [] -> count + 1 // At the end, add the last line & return
      | Some(rest), _ -> countLines (count + 1) rest  // Found - add line & continue
    

    If you were using some parser combinator library (such as FParsec), then you could use built-in parsers for most of the things. I didn’t actually try this, but here is a rough sketch – you could store the list of terminators as a list of strings and generate parser for each of the string:

    let terminators = [ "\r\n"; "\r"; "\n" ]
    let parsers = [ for t in terminators -> 
                      parser { let! _ = pstring t  
                               return 1 } ] // Return '1' because we found next line
    

    This gives you a list of parsers that return 1 when there is a terminator at the end – now you could aggregate all of them using <|> (or combinator) and then run the composed parser. If that fails, you can skip the first character (combine this with another parser) and continue recursively. The only problem is that parser combinators usually return all possible derivations ("\r\n" can be interpreted as two line breaks..), so you would need to get just the first result…

    (From your question, it wasn’t clear whether you actually want to use some parser combinator library or not, so I didn’t elaborate on that topic – if you’re interested, you can ask for more details…)

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

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's like this (without metamodel): Map<String, Object> params = ...;… May 17, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer What was the ALLUSERS property set to? You probably did… May 17, 2026 at 2:06 am
  • Editorial Team
    Editorial Team added an answer That's not really a "reference", but instead a declaration. It… May 17, 2026 at 2:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am working on constructing and saving a bitmap, and i have a loop
When working with activerecord I notice that while constructing associations, the reciprocal association is
I'm working on a project constructing an online collaboration tool. Looking at Wikipedia, I
I am working with Moles and mocking a System.Data.Linq.Table. I got it constructing fine,
Im working on a small project which I need to send a couple of
I am trying to get my Django app (NOT using Google app engine) retrieve
I'm working with UK address data and also International address data. I need to
I'm working on a python plugin for Google Quick Search Box, and it's doing
I'm constructing a SQL query for a business report. I need to have both
A project I'm working on at the moment involves refactoring a C# Com Object

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.