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.

[Question] How do I combine these parsers?

See original GitHub issue

Let’s assume I have a Group 1 of parser a,b,c and d and Group 2 of parser w,x,y and z. I would like to combine these parses so that the resulting parser would parse any combination of parsers from group 1 and exactly 1 parser from group 2 in any order.

Is this possible?

It is quite possible that I’m attacking the problem from a wrong angle (XY-problem), here is what I’m really trying to solve.

I’d like to parse a command line switch that starts with - or / and then a number of options follows for example -abwdc among these options there could be as many as desired binary ones (that is a,b,c or d, that do not require a parameter to follow) but only one that requires a parameter (w,x,u or z). The options can be specified in any order.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
jzabroskicommented, Apr 16, 2020

@AndrewSav I’m pretty thankful for your stream of conciousness. I was looking to see if anyone had implemented a command-line parser library using SuperPower, as the whole tokenization feature seems like it would allow for people to extend it with varying grammars. And the parser combinator feature would allow for nesting grammars.

0reactions
AndrewSavcommented, Aug 15, 2020

@jzabroski

Try only makes sense for built-in parsers for identifiers, reserved symbols, operators, and reserved operators (e.g. for future use). Arbitrary lookahead is not a good default, because it increases run-time cost and makes analyzing performance of parser combinator-based grammars more difficult.

An example would be helpful, otherwise it’s a bit too abstract. I’m sure there are examples, I just did not come across any in anything I’ve done so far with Superpower.

The risk of looking lookahead with Or is you could end up with ambiguous parse forest.

Superpower is never ambiguous. It always prefers the first valid match.

To provide some lookahead, the parser combinators I mentioned are oftened called lexeme parsers because they only require one lookahead.

Which parser combinators did you mention? I must have missed that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

parsing - How to combine many parsers?
For simplicity, I use constant "else " and "elif " strings in the results, but these can be built from the partial results...
Read more >
Introduction to Parsers
We can combine these parsers together with some higher-order functions called combinators. Let's create a combinator called sequence that ...
Read more >
parsing - When to use a Parser Combinator? ...
to combine them : e.g. many1 attempts to match another parser as many times as possible (but at least once, or it itself...
Read more >
Unlocking the Power of Parser Combinators: A Beginner's ...
First, we will create a parser that only recognizes a single character, but by combining multiples of those, we'll be able to recognize...
Read more >
A Guide To Parsing: Algorithms And Terminology
An in-depth coverage of parsing terminology an issues, together with an explanation for each one of the major algorithms and when to use...
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