[Haskell-beginners] Consuming Rule Based Parsing

Christopher Howard christopher.howard at frigidcode.com
Sat Nov 17 04:53:50 CET 2012


On 11/16/2012 06:04 PM, Karl Voelker wrote:
> On Fri, Nov 16, 2012 at 5:13 PM, Christopher Howard
> <christopher.howard at frigidcode.com
> <mailto:christopher.howard at frigidcode.com>> wrote:
> 
>     Thank you for your help. Can you elaborate a little more on your
>     explanation? So, would "a" be some data type representing the atoms of
>     the grammar? Or some kind of recursive data type that could represent
>     any kind of valid structure? Or...?
> 
> 
> The type parameter "a" is the output of that particular parser. So, a
> parser for an integer might have type String -> Integer, while a parser
> for some complicated data type Foo would have type String -> Foo.
> 
>     I'll wait until I'm clear on that point before asking about how I would
>     combine parsers. (Presumably, two combined parsers would both have to
>     have the same "a" type.)
> 
> 
> Since the parsers in this scheme are just functions, there are endless
> ways they could be combined, and the input and output types may or may
> not match.
> 
> Some combinators you will probably want:
> 
> andThen :: Parser a -> (a -> Parser b) -> Parser b
> orElse :: Parser a -> Parser a -> Parser a
> 
> And you might also want:
> 
> succeedWith :: a -> Parser a
> 
> -Karl

Maybe I'm thinking about this all wrong... But it isn't quite clear to
me how I make a generic function or operator that combines two Parsers
of one (or two) types to make another Parser of a separate type. Say,
for instance, I have a grammar which consists of atomic Nouns, atomic
Verbs, and Sentences which are a combination of the two. So naturally
I'd expect to have something like:

data Noun = Noun String
data Verb = Verb String
data Sentence = Sentence Noun Verb

nounParser :: Parser Noun
nounParser = ...

verbParser :: Parser Verb
verbParser = ...

sentenceParser :: Parser Sentence
sentenceParser = nounParser <+> verbParser

(<+>) :: ?
(<+>) f g = ?

-- 
frigidcode.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121116/93fd193f/attachment.pgp>


More information about the Beginners mailing list