cs453 shift reduce parsing unambiguous grammars lr 0 and
play

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


  1. CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout CS453 Shift-reduce Parsing 1

  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 CS453 Shift-reduce Parsing 2

  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

  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

  5. Example ¡LR ¡parse ¡ S à AB A à Aa | a B à Bb | b S ’ à S $ a aabb$ ß Aa abb$ ß Aa bb$ ß A b b$ ß 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

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

  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

  8. Shift reduce parsing applied to unambiguous grammars [0] S à ( S ) Single parentheses nest [1] S ‘ à S $ Start symbol is S ’ [2] S à ID 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

  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

  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

  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 ¡ ¡Column s ¡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

  12. 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

  13. parse (x,(x))$ Parse table for List grammar stack input action 0: S ’ à à S$ 1: S à à ( L ) 2: S à à x 0 (x,(x))$ s2 3: L à à S 4: L à à L , S 0(2 x,(x))$ s1 0(2x1 ,(x))$ r2: S à x ( ) x , $ S L 0(2S6 ,(x))$ r3: L à S 0 s2 s1 g3 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) 0(2L4,7S8 )$ r4: L à L,S 7 s2 s1 g8 0(2L4 )$ s5 8 r4 r4 r4 r4 r4 0(2L4)5 $ r1:S à (L) 03S $ a CS453 Shift-reduce Parsing 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend