Spandan
Spandan

Reputation: 199

Operator Precedence Parser: Where do we keep reduced tokens?

An Op Precedence Parser has a stack and an input buffer.

I believe that after popping, an "id" token is reduced to a variable "E".

For every pop of an "id" token from the stack after seeing a mathematical operator in the input, where is the popped token kept?

If the input is like id+id*id$ , by the time $ is reached, all the "id"s have already been popped. Where are they kept?

Upvotes: 1

Views: 76

Answers (1)

rici
rici

Reputation: 241691

Assuming that your goal is to build an AST, the ids (and other operand tokens such as literal constants) are put into the AST node created by the reduction.

If you're directly evaluating or generating three-address code or ..., then the answer will differ slightly. However, the broad outline is the same: operator-precedence parsing is a bottom-up parse algorithm in which the right-hand side of a production is reduced to the corresponding non-terminal (on the left-hand side of the production) when it's last input symbol has been read (and the lookahead indicates that the correct action is reduction).

Upvotes: 1

Related Questions