logoalt Hacker News

thaumasiotes • today at 4:13 AM • 2 replies • view on HN

I would usually think of a 'parser' as simply something that tells you how to produce the input given a set of production rules.

For a simple ambiguous arithmetical grammar, the output of a parser fed "1 + 2 * 3" might be something like "[Mul [Add 1 2] 3]", or "AddExpr(1, MulExpr(2, 3))", or "1 2 3 Mul Add", or "1. Start with 1; 2. Add 2; 3. Multiply by 3", or (more likely in practice) in-memory data structures corresponding to one of those. The form of the representation doesn't matter; it's just an indication of the sequence of steps that gets you from 'nothing' to whatever input string was provided.

But there's not really a concept of 'actions' to go with parsing. Parsing tells you how some string was constructed, or how it might have been constructed. What you do with that information is up to you.

In the example above, the parser's interest in a MulExpr is that it can produce a string containing the token '*'. The fact that mathematically two times three equals six doesn't come up.


Replies

nagaiaida • today at 8:12 AM

for another perspective, raku lets you carry around reusable grammars, but each time you parse something with them you have the option to specify an actions class, the methods of which are invoked and passed the corresponding matches from the grammar (this lets you reuse the actual grammar matched against; sometimes you just want to typecheck cli arguments to main against a grammar and aren't actually going to use the match as anything other than an ornate boolean, but you might as well keep the actual grammar in sync between that nop/bool parser and another parser that actually builds something useful later).

so that's another community's vote for actions as something belonging to The Parser over and above the strict grammar by which that parsing is done.

Jtsummers • today at 4:38 AM

> But there's not really a concept of 'actions' to go with parsing.

I'll appeal to authority here and quote from the Dragon Book:

>> Program fragments embedded within production bodies are called semantic actions.

In CS, particularly around compilers, it's an old concept.

➕ show 1 reply