CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR - - PowerPoint PPT Presentation

cs453 shift reduce parsing unambiguous grammars lr 0 and
SMART_READER_LITE
LIVE PREVIEW

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables

by Wim Bohm and Michelle Strout

CS453 Shift-reduce Parsing 1

slide-2
SLIDE 2

CS453 Shift-reduce Parsing 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

  • n 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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

CS453 Shift-reduce Parsing 8

Shift reduce parsing applied to unambiguous grammars

[0] S à ( S ) [1] S‘ à S $ [2] S à ID

Single parentheses nest 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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

CS453 Shift-reduce Parsing 12

Example LR(0) Parse Table, Single Parentheses Nest

[0] S -> ( S ) [1] S’ -> S $ [2] S -> ID

Action Goto State ( ) $ ID S 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.

slide-13
SLIDE 13

Parse table for List grammar

0: S’ à à S$ 1: Sà à( L ) 2: Sà àx 3: Là àS 4: Là àL , S

CS453 Shift-reduce Parsing 13

parse (x,(x))$

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