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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:11+00:00 2026-05-27T23:04:11+00:00

I have a tricky question (at least in my perspective), regarding Scalas parser combinators

  • 0

I have a tricky question (at least in my perspective), regarding Scalas parser combinators and recursive parsing. I currently building a small parser which should be able to parse PL/1 structures like this:

  dcl 1 data,
    3 subData,
      5 tmp char(15),
      5 tmp1 char(15),
    3 subData2,
      5 tmp2 char(10),
      5 tmp3 char(5);

In this scenario I want to build a AST as follows:

Record(data)  -> (Record(subData),Record(subData2))

  Record(subData) -> (Char(tmp),Char(tmp1))

  Record(subData2) -> (Char(tmp2),Char(tmp3))  

Meaning that the parent element should be connected to its children. In my world this should result in a recursive parser in some way, however my problem is how to control when to stop going down in sublevels. For instance when parsing the the “3 subdata” record structure, it should stop when hitting a level number which is not lower in itself, in this case the line “3 subData2,”.

Can someone help with this issue, or point me in a good direction.
My current solution is to solved this problem after I have parsed an unconnected structure.

Thanks in advance.

Regards Stefan

  • 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-27T23:04:11+00:00Added an answer on May 27, 2026 at 11:04 pm

    Basically all you need is Parser.into (it has an alias >>), it creates a parser combinator that’s based on the result of the current one.

    I’ve prepared a simple REPLable example for you

    import util.parsing.combinator.RegexParsers
    
    case class Record(data: String, children: List[Record])
    
    object RecordParser extends RegexParsers {
      override def skipWhitespace = false
    
      def ws = "\\s*".r
      def numberGT(min: Int) = "\\d+".r ^^ {  _.toInt } ^? {
        case i if i > min => i
      }
    
      def subData(n: Int): Parser[Record] = ws ~> numberGT(n) ~ ws ~ ".*".r <~ "\n" >> {
        case sub ~ _ ~ data => rep(subData(sub)) ^^ { new Record(data, _) }
      }
    }
    
    val testData = """
    1 data
     2 subdata
      3 child1
      3 child2
     2 sub2
    """
    
    RecordParser.parse(RecordParser.subData(0),test)
    res7: RecordParser.ParseResult[Record] = [7.1] parsed: Record(data,List(Record(subdata,List(Record(child1,List()), Record(child2,List()))), Record(sub2,List())))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tricky question regarding shallow serialization of List of objects my entities
I have the following tricky problem: I have implemented a (rather complicated) class which
This sounds like a tricky question... let me ellaborate... I have a treeView. When
I have a tricky problem that I've been messing about with for a few
I have a tricky problem that has had me scratching my head for a
I have a very tricky situation (for my standards) in hand. I have a
I have a very tricky problem going on with content inside a textarea on
I have sort of a tricky problem I'm attempting to solve. First of all,
I have run into a bit of a tricky problem in some C++ code,
This is a little tricky so bear with me: I have a PHP script

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.