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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:09:50+00:00 2026-05-30T10:09:50+00:00

Consider example of the parser like this: object TestParser extends RegexParsers { override protected

  • 0

Consider example of the parser like this:

object TestParser extends RegexParsers {
    override protected val whiteSpace = """[ \t]*""".r  

    def eol = """(\r?\n)+""".r
    def item = "[a-zA-Z][a-zA-Z0-9-]*".r
    def list = "items:" ~> rep1sep(item,",") 
    def constraints = "exclude:" ~> item

    def itemsDefinition = (rep1sep(list, eol) ~ repsep(constraints,eol))
}

If I try to parse this input (without two lines contains exclude works OK):

items: item1, item2, item3, item3, item4
items: item2, item3, item3, item5, item4    
items: item4, item5, item6, item10      
items: item1, item2, item3
exclude: item1
exclude: item2

I get following error:

[5.5] failure: `items:' expected but `e' found

       exclude: item1

       ^

The problem is obvious this line:

def itemsDefinition = (rep1sep(list, eol) ~ repsep(constraints,eol))

What is reason it doesn’t work. Does it have something to do with backtracking? What alternatives do I have to make it work?

  • 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-30T10:09:51+00:00Added an answer on May 30, 2026 at 10:09 am

    You would need an eol between your lists and your constraints

    (rep1sep(list, eol) <~ eol) ~ repsep(constraint,eol)
    

    Completing the answer:

    Your grammar specify eol as a separator between lists, not a terminator. It would accept an input where the first exclude appears just after the last item3 (with a whitespace, but not a new line).

    After your parser reach the unwanted eol, it looks for items, and find excludes instead. Which gives the displayed error message. Then, the parser DOES indeed backtrack, to the previous new line. It considers the possibility that the lists part stops there, and look for excludes. But if finds an eol instead. So another possible error message would be "excludes expected, eol found", which in this case would have been more helpful

    When there is a choice in the grammar, and no branch succeeds, the parser returns the error with the farthermost position, which is normally the right strategy. Suppose you grammar allows an "if" or a "for", and the input is "if !!!". On the if branch, the error would be something like "(" expected, "!" found. On the for branch, the message would be "for expected, if found". Clearly, the message from the if branch, which appears on the second token, is way better than the message from the for branch, on the first token and not relevant at all.

    On the question of separator/terminator, you may consider :

    • separator (; in Pascal) : repsep(item, separator)
    • terminator (; in C) : rep(item <~ terminator)
    • flexible : repsep(item, separator) <~ separator?

    the last one would allow for a single separator after no items at all. If this is undesirable, maybe (rep1sep(item, separator) <~ separator?)?.

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

Sidebar

Related Questions

Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {
Consider this code (prestuffed with an example): DateTime dt1 = DateTime.Parse(7/30/2010 9:33:29.1234567 AM); DateTime
Consider this example showing the YUI tree in action: http://developer.yahoo.com/yui/examples/treeview/tv_edit.html Select the title in
Let's consider this sample example text file: Level: 1 Level: 1 Level: 2 Level:

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.