0

I am writing a shift reduce parser in c#. I looked at some articles explaining it, but none of them go into much detail. Could someone point me to the direction of detailed explanations of shift reduce parsers, like how does it know when to reduce?

82.dev
  • 1
  • 1
  • [The Wikipedia article](https://en.wikipedia.org/wiki/Shift-reduce_parser) says that a shift-reduce parser uses its own table of actions to know when to reduce or not based on the current state of the incomplete parse tree. If you are having trouble understanding that then please clarify your question to be more specific about which parts of the article you don't understand. – Dai Mar 28 '21 at 11:04
  • Does it have a dictionary with stack arrangments that it uses to reduce? – 82.dev Mar 28 '21 at 11:37
  • 1
    What do you mean by “dictionary”? If you mean a hashtable, then *yes*, but that’s just *one* way of implementing it; there are still many other alternative approaches to implementing the same idea. – Dai Mar 28 '21 at 12:02
  • What are the other alternatives? – 82.dev Mar 28 '21 at 12:04
  • There are effectively an infinite number of alternative ways to implement a lookup subroutine based on program state. Why are you asking, though? – Dai Mar 28 '21 at 12:12
  • What would be the most efficient though? – 82.dev Mar 28 '21 at 12:14
  • That's subjective and depends on the application. – Dai Mar 28 '21 at 12:17

1 Answers1

0

The parser is a state machine. Each state has a action table which maps the next input symbol ("token") into an action (shift, reduce a production, error, or accept). For shift actions, there is a transition table which maps the input symbol to the next state. These two tables are usually combined, since there is room in the action table entry for a state number.

Since there are a relatively small number of possible tokens, the tokens are usually represented as small integers and the action table is a two-dimensional array indexed by current state and by input token. If space is a concern, it's possible to compress the tables.

rici
  • 234,347
  • 28
  • 237
  • 341