News for hackers. Stuff that matters.
Re:When to use generics by pansa2 on Wednesday April 13, 2022 @12:22AM (#31011177)
`Token` isn’t a type that a parser has to define - it comes from the previous stage (the lexer), or could even just be `string`. When writing a parser it’s already defined - so do you start the parser by working with the types you already have, or by immediately defining more types?

I’d write a parser by first writing code to open a file (for example) to get a collection of tokens, then loop through them and decide what to do for each one. I’d use those decisions to guide my design of the output types.

Others, it seems, start by designing the output types up front and use that to guide the design of the program logic.

Re:When to use generics by eru on Wednesday April 13, 2022 @12:25AM (#31011189)
In something like Haskell, you often write your parsers via parser combinators. So you wouldn't be writing loops (or their Haskell equivalent) at all.