Alt backtracking (question)
See original GitHub issueHi! 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):
- Parser
string("a")
succeeds - Parser
string("b")
fails - Failure bubbles to
P.seq
and then toP.alt
- Alt backtracks (releasing parsed
"a"
char) - Parser
string("ac")
is tried and this one does match - !!!
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:
- Parser
P.string("a").sepBy1(P.string("+"))
fails - For some reasone failure doesn’t bubble to
P.alt
this time!!! - Alt does not backtrack or doesn’t have a chance to backtrack!!!
- Parsing fails despite
P.any.many()
parser could surely parse this string… - ???
Is this an expected behavior? Why sepBy1
causes this effect?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
likesepBy1(...).lookahead(REAL_END_OF_THE_LIST)
but it’s not always possible or desirable.@wavebeem thanks for the help!
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:
is actually succeeding, which takes you out of the
P.alt
call immediately. but then.tryParse(...)
is failing because there’s still leftover input.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