Parser Combinators

(t3.at)

30 points | by schindlabua 2 hours ago

8 comments

  • mrkeen 17 minutes ago
    Not a good starting type:

      type Parser<T> = (input: string) => [T, string]
    
    This implies that it accepts a string and will return a T. Parsers might return a T.

    The article also starts with the concept of a 'partial parser', from which other (non-partial?) parsers can be constructed?

      Or, and this is where the real power of partial parsers comes in: we wrap the chain up as a new parser of its own.
    
    Nope, there's no distinction. If you combine partial parsers, you end up with a partial parser. If you combine full parsers (whatever that means) you end up with full parsers.

    (Ironically there is a fitting definition of 'partial' here: the function isn't well defined on all inputs. For some inputs, T is returned, on other inputs, maybe an error is thrown, who knows..)

    • raddan 13 minutes ago
      Combinators are functional—they always return something. But if you want to return nothing, fine; T is any type. Just return something like unit (ie, the singleton type that has nothing as a value).
    • schindlabua 14 minutes ago
      Good input. Since we're working in typescript a partial parser can always throw an exception (It's what the parsimmon library does aswell I believe). I decided not to get into those technicalities too much!
  • mbaranturkmen 1 minute ago
    How does the parser decide _where_ to split the statement?
  • justinnk 15 minutes ago
    Very cool visual guide! I feel it is appropriate here to recite this little poem about parsers by Fritz Ruehr that stuck with me since my first semester of CS. With this, you will never forget the type signature of a parser.

    „A Parser for Things is a function from Strings to Lists of Pairs of Things and Strings!“

    https://people.willamette.edu/~fruehr/haskell/seuss.html

  • stevefan1999 8 minutes ago
    I love parser combinators so much. Those are possible because parsing itself exhibits monadic structure, it forms transition monoid, of its more well known name as the semiautomaton in the world of CS.
  • monster_truck 38 minutes ago
    I would suggest parsing my scroll wheel input and combinating it into page events
  • tikimcfee 53 minutes ago
    Love me some combinators. In my earlier days it helped bridge the academic gap of CS grammars and languages to something I could not readily apply to internship dev work. I seem to recall an attempt to map or generate the from regex and in reverse, but I might be crazy this early in the morning.

    Nice visuals btw. If I may be so bold: slightly smaller font and header would make scrolling through the bottom readable code snippets a little easier!

    • raddan 1 minute ago
      Yeah, the academic presentation of parsing complexity is not very useful for real-world parsing tasks. It is useful to know that regular expressions are limited (NFAs “can’t count”, so they’re not good for trying to match an unbounded number of balanced parentheses, for example). But beyond that, we largely choose capable algorithms either for ease of use or speed. Combinators, PEGs, parser generators (like lex/yacc, ANTLR) are mostly easy. For speed, almost everyone rolls a parser by hand.

      I like combinator style parsers because 1) it’s really easy to think of a discrete parsing task as a function 2) tokenizing is mostly irrelevant 3) for pedagogical reasons, they make it easier for students to grasp the point of curried functions and partial application.

      I even maintain a (mostly mature) parser combinator library for TypeScript for students in my research group to use.

      https://github.com/williams-cs/parsecco

  • lacoolj 21 minutes ago
    Did you use the new Claude Motion for this?
  • adrianhqs 30 minutes ago
    Nice design