0% found this document useful (0 votes)
25 views

07 Shift Reduce

This document discusses shift-reduce parsing and LR parsing. It begins with an overview of shift-reduce parsing in LR parsers using parsing tables. It then provides more details on LR parsing, including how it performs a bottom-up, right-most derivation in reverse using a stack. Examples are given of shift and reduce actions in an LR parsing engine and a simplified example is shown of an LR parsing table.

Uploaded by

Sara Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

07 Shift Reduce

This document discusses shift-reduce parsing and LR parsing. It begins with an overview of shift-reduce parsing in LR parsers using parsing tables. It then provides more details on LR parsing, including how it performs a bottom-up, right-most derivation in reverse using a stack. Examples are given of shift and reduce actions in an LR parsing engine and a simplified example is shown of an LR parsing table.

Uploaded by

Sara Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CS453 : Shift Reduce Parsing

Unambiguous Grammars
LR(0) and SLR Parse Tables
by Wim Bohm and Michelle Strout

CS453 Shift-reduce Parsing 1


Plan for Today

Finish PA1 this week


– Friday recitation: help with PA1
– Also on Thursday and Friday, form groups for PA2 and on

Shift-reduce parsing, LR(1)


– The problem with predictive top down parsing
– LR parsing: bottom up
– Performs a right-most derivation in reverse
– Parsing unambiguous grammars
– LR parsing table and parsing algorithm

CS453 Shift-reduce Parsing 2


LL vs LR

LL(k) must predict which production looking ahead k:


S à SS | (S) | ε
is it S à SS or (S) when I see ((((……. ?
produces the parse tree TOP DOWN, with a left to right derivation

LR(k) postpones the decision until all tokens of the rhs plus k more tokens
have been seen. It therefore is more powerful.

It does this by parsing BOTTOM UP

CS453 Shift-reduce Parsing 3


Shift-reduce parsing in an LR parser

LR(k) parser
– Left-to-right parse
– Right-most derivation
– K-token look ahead
LR parsing algorithm
– Performs a shift-reduce parse with an explicit stack
stack contains grammar symbols (T or V) mixed with states
– Look at state at top of stack and input symbol to find action in table
– shift(n): advance input, push state n on stack
– reduce(k): pop rhs of grammar rule k, look up state on top of stack
and lhs for goto n, push lhs(k) and n onto stack
– accept: stop and success
– error: stop and fail

CS453 Shift-reduce Parsing 4


Example  LR  parse  
S à AB Aà Aa | a B à Bb | b S’ à S $

aaabb$ßAaabb$ßAabb$ßAbb$ß ABb$ ßAB$ßS$ßS’

Notice that this is the rightmost derivation


S’àS$àAB$àABb$àAbb$àAabb$àAaabb$àaaabb$
in reverse!
It does not start with the start symbol, it ends with it

LR(k) parsing scans the input Left to right and produces the
Rightmost derivation (looking k tokens ahead) in reverse.

CS453 Shift-reduce Parsing 5


LR parsing engine

The LR parsing engine …


– uses an Explicit Stack (as opposed to the implicit stack of LL)
– and the input token string
– and performs two kinds of actions

Shift -- push input token onto the stack


Reduce -- pop a rhs off the stack and push the corresponding lhs
on the stack

It accepts the string of tokens when the input is empty or $/(EOF) and the
stack contains the start symbol.

CS453 Shift-reduce Parsing 6


Simplified  example  LR  parsing  engine  ac4ons  
S à AB Aà Aa | a B à Bb | b S’ à S $

Stack input action


aaabb$ shift
a aabb$ reduce : Aàa
A aabb$ shift
Aa abb$ reduce: AàAa
A abb$ shift
Aa bb$ reduce: AàAa
A bb$ shift
Ab b$ reduce: Bàb
AB b$ shift
ABb $ reduce: BàBb
AB $ reduce: SàAB
S $ accept

CS453 Shift-reduce Parsing 7


Shift reduce parsing applied to unambiguous grammars
[0] S à ( S ) Single parentheses nest
[1] S‘ à S $
[2] S à ID Start symbol is S’

Stack input action


((ID))$ shift
( (ID))$ shift
(( ID))$ shift
((ID ))$ reduce: SàId
((S ))$ shift
((S) )$ reduce: Sà(S)
(S )$ shift
(S) $ reduce: Sà(S)
S $ accept

CS453 Shift-reduce Parsing 8


Table  driven  LR  parse  algorithm  

The stack contains grammar symbols and states:


after a symbol is pushed, the next state is pushed

The actions can now be formulated as:


– Sn: push input token; push n (ie goto state n)
– Rk: rule k: lhs à rhs
pop rhs of rule k off the stack, this exposes a state s
look up in row s and column lhs the next state gn to goto
push lhs; push n (ie goto state n)
– a: stop parsing, report success
– _: stop parsing, report error

CS453 Shift-reduce Parsing 9


LR(0) SLR LALR LR(k)

Given the shift reduce machinery described, there are various


algorithms for creating the parse table.

These algorithms get more and more sophisticated, starting from


simple LR(0)

Parser generators such as java-cup use the more sophisticated


LALR parse table creation algorithm

Today the goal is to understand how to do a shift and reduce


parse given a table. Later we will discuss how to get the table.

CS453 Shift-reduce Parsing 10


Table  Driven  LR  Parsing  
The  parsing  engine  is  a  DFA  plus  a  stack:  a  determinis7c  pushdown  automaton.  
The  DFA  is  described  by  a  transi7on  table:  
 Rows  are  states  
 Columns  are  Grammar  symbols  (terminals,  non-­‐terminals)  
     terminals:    shiD  or  reduce  and  go  to  next  state  
     non-­‐terminals:  decide  which  state  to  go  to  aDer  a  reduc7on  
   So  there  are  four  kinds  of  ac7ons  
       sn:      shiD  input  and  go  to  state    n  
       rk:      reduce  according  to  rule  k:  lhs  à  rhs,  this  exposes  a  state  s  
       look  up  in  row  s  and  column  lhs  the  next  state  gn  to  goto    
       push  lhs;  push  n  (ie  goto  state  n)  
           a:    accept  
           _:    error    (indicated  by  blank  state)  

CS453 Shift-reduce Parsing 11


Example LR(0) Parse Table, Single Parentheses Nest
[0] S -> ( S ) [1] S’ -> S $ [2] S -> ID

Action Goto

State ( ) $ ID S

0 s3 s1 2
1 r2 r2 r2 r2
2 a

3 s3 s1 4
4 s5
5 r0 r0 r0 r0
In LR(0) parse tables, reduce actions happen for all
terminals, as the parser does not look ahead.

CS453 Shift-reduce Parsing 12


Parse table for List grammar parse (x,(x))$

0: S’ à S$ 1: Sà( L ) 2: Sàx stack input action


0 (x,(x))$ s2
3: LàS 4: LàL , S
0(2 x,(x))$ s1
( ) x , $ S L 0(2x1 ,(x))$ r2: Sàx
0 s2 s1 g3 0(2S6 ,(x))$ r3: LàS
0(2L4 ,(x))$ s7
1 r2 r2 r2 r2 r2
0(2L4,7 (x))$ s2
2 s2 s1 g6 g4
0(2L4,7(2 x))$ s1
3 a
0(2L4,7(2x1 ))$ r2: Sàx
4 s5 s7 0(2L4,7(2S6 ))$ r3: LàS
5 r1 r1 r1 r1 r1 0(2L4,7(2L4 ))$ s5
6 r3 r3 r3 r3 r3 0(2L4,7(2L4)5 )$ r1: S à(L)
7 s2 s1 g8 0(2L4,7S8 )$ r4: LàL,S
8 r4 r4 r4 r4 r4 0(2L4 )$ s5
0(2L4)5 $ r1:Sà(L)
03S $ a

CS453 Shift-reduce Parsing 13

You might also like