Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser zero_or_one() is not greedy #12

Closed
douwe opened this issue Apr 10, 2024 · 1 comment
Closed

Parser zero_or_one() is not greedy #12

douwe opened this issue Apr 10, 2024 · 1 comment

Comments

@douwe
Copy link
Member

douwe commented Apr 10, 2024

Parser zero_or_one is not greedy, meaning that it sometimes does not parse input that it should be able to parse.

The reason for this bug is that zero_or_one(p) is defined as exactly(0,p) %or% exactly(1,p) which first tries to parse 0 times, and if it does, continues, even though it could have parsed once as well.

Example. We define a parser ABblock

ABblock <- function() {zero_or_one(literal("A")) %then% literal("B")}

and we apply this one or more times on the input c("A","B","B")

one_or_more(ABblock())(c("A","B","B"))

which leads to parse failure, whereas it should parse because we have: [one A then B] then [zero A then B]

@douwe
Copy link
Member Author

douwe commented Apr 12, 2024

This is resolved by defining zero_or_one as exactly(1,p) %or% exactly(0,p), i.e. by first testing whether p can parse once. Additionally, the example that fails has been added as a test in the package.

@douwe douwe closed this as completed Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant