0% found this document useful (0 votes)
215 views3 pages

Shift Reduce Parser

The document discusses shift-reduce parsing. It explains that shift-reduce parsing constructs a parse tree from the bottom up, like bottom-up parsing. It requires an input buffer and a stack. The basic operations are shift, which moves symbols onto the stack; reduce, which uses production rules to pop symbols off the stack; and accept, which occurs when only the start symbol remains on the stack and the input buffer is empty. It provides an example parsing the input string "id + id + id" using the grammar S -> S + S | S * S | id.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views3 pages

Shift Reduce Parser

The document discusses shift-reduce parsing. It explains that shift-reduce parsing constructs a parse tree from the bottom up, like bottom-up parsing. It requires an input buffer and a stack. The basic operations are shift, which moves symbols onto the stack; reduce, which uses production rules to pop symbols off the stack; and accept, which occurs when only the start symbol remains on the stack and the input buffer is empty. It provides an example parsing the input string "id + id + id" using the grammar S -> S + S | S * S | id.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Shift Reduce parser attempts for the construction of parse in a similar

manner as done in bottom-up parsing i.e. the parse tree is constructed from
leaves(bottom) to the root(up). A more general form of the shift-reduce
parser is the LR parser.
This parser requires some data structures i.e.
 An input buffer for storing the input string.
 A stack for storing and accessing the production rules.
Basic Operations –
 Shift: This involves moving symbols from the input buffer onto the stack.
 Reduce: If the handle appears on top of the stack then, its reduction by
using appropriate production rule is done i.e. RHS of a production rule is
popped out of a stack and LHS of a production rule is pushed onto the
stack.
 Accept: If only the start symbol is present in the stack and the input
buffer is empty then, the parsing action is called accept. When accepted
action is obtained, it is means successful parsing is done.
 Error: This is the situation in which the parser can neither perform shift
action nor reduce action and not even accept action.
Example 1 – Consider the grammar
S –> S + S
S –> S * S
S –> id
Perform Shift Reduce parsing for input string “id + id + id”.

Example 2 – Consider the grammar


E –> 2E2
E –> 3E3
E –> 4
Perform Shift Reduce parsing for input string “32423”.
Example 3 – Consider the grammar
S –> ( L ) | a
L –> L , S | S
Perform Shift Reduce parsing for input string “( a, ( a, a ) ) “.

Stack Input Buffer Parsing Action

$ (a,(a,a))$ Shift

$( a,(a,a))$ Shift

$(a ,(a,a))$ Reduce S → a

$(S ,(a,a))$ Reduce L → S

$(L ,(a,a))$ Shift

$(L, (a,a))$ Shift

$(L,( a,a))$ Shift


Stack Input Buffer Parsing Action

$(L,(a ,a))$ Reduce S → a

$(L,(S ,a))$ Reduce L → S

$(L,(L ,a))$ Shift

$(L,(L, a))$ Shift

$(L,(L,a ))$ Reduce S → a

$ ( L, ( L, S ))$ Reduce L →L, S

$ ( L, ( L ))$ Shift

$ ( L, ( L ) )$ Reduce S → (L)

$ ( L, S )$ Reduce L → L, S

$(L )$ Shift

$(L) $ Reduce S → (L)

$S $ Accept

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy