7 votes

Topic deleted by author

3 comments

  1. [3]
    vektor
    Link
    Just briefly scrolling through this, I can't seem to find where it answers my perpetual question when it comes to parsers: How do I actually get the result? I usually end up writing my own,...

    Just briefly scrolling through this, I can't seem to find where it answers my perpetual question when it comes to parsers: How do I actually get the result? I usually end up writing my own, because I can't figure out how [hot parser library] gives me the resulting values. I don't care much whether this is malformed JSON, I want to find out what's in it.

    1 vote
    1. [3]
      Comment deleted by author
      Link Parent
      1. [2]
        vektor
        Link Parent
        Thanks for the detailed explanation! Oh, I have met parser combinators before, and they are a neat tool, absolutely. I wasn't so much confused about the modalities of building one, but more about...

        Thanks for the detailed explanation!

        Oh, I have met parser combinators before, and they are a neat tool, absolutely. I wasn't so much confused about the modalities of building one, but more about how to use this parser - or any parser, really - to extract a nugget of information out of a string of some kind of formal language. Maybe this is because all of my interactions with parsers and regexes are always so touch and go: Use it for this one case you need them, build it, test it, forget it. See you in half a year. The know-how just won't stick. Talking about regexes, maybe that contributes to my confusion, because what you usually do with them is just match them. Regexes are in a way as modular as parser combinators (just they did it decades earlier), but I'm not even sure you can use them to yield results that you're interested in.

        2 votes
        1. skybrian
          Link Parent
          There are a couple of different ways to go, depending on what you want the output to look like. Sometimes you're building something like an abstract syntax tree, in which case each parse function...

          There are a couple of different ways to go, depending on what you want the output to look like.

          Sometimes you're building something like an abstract syntax tree, in which case each parse function can construct and return the AST node for the part of the tree that it parsed.

          Alternatively, you are building some kind of table. In this case the table gets passed in as an argument, or in an object-oriented language, it's typically a field in the class that all the parse methods belong to. Each parse method adds entries to the table as it parses. This is how a list of errors can be collected, for example.

          At least, that's how it's done for hand-written parsers in non-functional languages. I assume functional languages have their own tricks.