Can the built-in scala parser combinators parse binary (not text) files?
It can parse anything, that's why type Elem is abstract on Parsers.
Related
I'm looking at the following approach to using parser combinators in Haskell. The author gives the following example of Parser Combinators:
windSpeed :: String -> Maybe Int
windSpeed windInfo =
parseMaybe windSpeedParser windInfo
windSpeedParser :: ReadP Int
windSpeedParser = do
direction <- numbers 3
speed <- numbers 2 <|> numbers 3
unit <- string "KT" <|> string "MPS"
return speed
The author gives the following reasons for this approach:
easy to read (I agree with this)
similar format to the specification ie the parser itself is basically a description of what it parses (I agree with this)
I can't help but feel I'm missing some of the reasons for choosing parser combinators. Some benefit of either using Haskell, compile-time guarantees, elimination of runtime errors. Or some subsequent benefit when you starting parsing DSLs and using free monads.
My question is: What are the reasons for using parser combinators?
I see several benefits of using parser combinators:
Parser combinators are a generalization of hand-written top-down parsers. In the case that you hand-write a parser, use parser combinators to abstract away common patterns.
Unlike parser generators, parser combinators are potentially dynamic, allowing for decisions during runtime. This aspect may be useful if the language's grammar may be redefined based on the input.
Parsers are first-class objects.
Is there a parser that can parse ambiguous grammars (ideally in Haskell)?
Parsec's paper (http://research.microsoft.com/en-us/um/people/daan/download/papers/parsec-paper.pdf) states the following:
"Ambiguous grammars have more than one parse tree for a sentence in the language. Only parser combinators that can return more than one value can handle ambiguous grammars. Such combinators use a list as their reply type."
But I haven't found any such parser combinators. Do they exist?
I've written some parsers using Attoparsec but only now realised that I don't always want them to backtrack on failure, but attoparsec parsers always backtrack on failure.
Is there a way to force a parser not to backtrack?
For example, this attoparsec parser will succeed when given the input "for":
string "foo" <|> string "for"
A parsec parser would not succeed on that input and I want to emulate this behaviour using an attoparsec parser.
I can't seem to find the parsing expression grammar (PEG) of PEG itself.
How to parse a parsing expression grammar?
Note that this question is not about how to construct a recursive decent parser from a PEG, but rather for parsing a PEG.
The PEG Paper ("Parsing Expression Grammars: A Recognition-Based Syntactic Foundation") includes the grammar for PEGs.
I was wondering whether the standard Scala parser combinators contain a parser that accepts the same identifiers that the Scala language itself also accepts (as specified in the Scala Language Specification, Section 1.1).
The StdTokenParsers trait has an ident parser, but it rejects identifiers like empty_?.
(If there is indeed no such parser, I could also just instantiate the Scala parser itself, but that wouldn't be as lightweight anymore.)
Not a standard parser combinator, but there are canonical tools for testing Scala id-ness in scala.tools.nsc.util.Chars. No need to instantiate either Global or a Scala scanner.