question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alt backtracking (question)

See original GitHub issue

Hi! I have a question about alt which behavior surprises me.

Case 1 – “expected”

let p = P.alt(
  P.seq(P.string("a"), P.string("b")).tie(),
  P.string("ac"),
)

console.log(p.tryParse("ac")) // "ac"

My understanding of what’s going on (may help to answer the next case):

  1. Parser string("a") succeeds
  2. Parser string("b") fails
  3. Failure bubbles to P.seq and then to P.alt
  4. Alt backtracks (releasing parsed "a" char)
  5. Parser string("ac") is tried and this one does match
  6. !!!

Case 2 – “unexpected”

let p = P.alt(
  P.string("a").sepBy1(P.string("+")), // try to comment this line
  P.any.many(),                        // this one is never tried!
)

console.log(p.tryParse(`a+a-a`)) // exception

My understanding of what’s going on:

  1. Parser P.string("a").sepBy1(P.string("+")) fails
  2. For some reasone failure doesn’t bubble to P.alt this time!!!
  3. Alt does not backtrack or doesn’t have a chance to backtrack!!!
  4. Parsing fails despite P.any.many() parser could surely parse this string…
  5. ???

Is this an expected behavior? Why sepBy1 causes this effect?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ivan-kleshnincommented, Jun 18, 2018

Yep, totally makes sense. My real case was much more complicated so I kinda stuck with it mentally. But it reduced to the same thing: sepBy1 which succeeds once it found at least one element.

Note for the future readers: a syntax error in the 1-st item of some list and in the 2-nd+ may lead to different parsing results. This is expected, one way to prevent that is to add more conditions after sepBy1 like sepBy1(...).lookahead(REAL_END_OF_THE_LIST) but it’s not always possible or desirable.

@wavebeem thanks for the help!

0reactions
wavebeemcommented, Jun 18, 2018

wow, ok that took some digging, but I actually don’t think this is a bug

it is weird though! i was just as confused as you for hours haha

so, the general problem here is that:

  P.seq(P.string("a"), P.string("b")).tie(),

is actually succeeding, which takes you out of the P.alt call immediately. but then .tryParse(...) is failing because there’s still leftover input.

_.parse = function(input) {
  if (typeof input !== "string" && !isBuffer(input)) {
    throw new Error(
      ".parse must be called with a string or Buffer as its argument"
    );
  }
  //
  //
  //               vvvvvvvvvv
  var result = this.skip(eof)._(input, 0);
  //               ^^^^^^^^^^
  //
  //
  if (result.status) {
    return {
      status: true,
      value: result.value
    };
  }
  return {
    status: false,
    index: makeLineColumnIndex(input, result.furthest),
    expected: result.expected
  };
};

please reference this RunKit example to see what I mean from your code sample: https://runkit.com/wavebeem/parsimmon-issue-255

does that explanation make sense? it’s a little confusing what’s going on here, but i’m not really sure there’s anything i could change to make parsimmon work better for this case

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 20 Backtracking Algorithm Interview Questions
Top 20 Backtracking Algorithm Interview Questions · N Queens Problem · Warnsdorff's Algorithm · Word Break Problem · Remove Invalid Parenthesis ...
Read more >
47 backtracking interview questions and solutions - IGotAnOffer
47 backtracking interview questions, all with links to high-quality solutions, plus an interview preparation guide.
Read more >
Recursion and Backtracking - PepCoding
Magnets hard. Abbreviations using Backtracking · Max Score · N Queens Branch and Bound · Josephus Problem · Permutations - 1 · Lexicographical...
Read more >
Backtracking: Practice Problems and Interview Questions
In this post, we have listed out common problems that can be solved using the backtracking technique: Print all possible solutions to N–Queens...
Read more >
Dependency Parsing with Backtracking using Deep ...
One way to overcome this problem is to allow the algorithm to backtrack and explore an alternative solution in cases where new evidence ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found