07 16 96
play

* 07/16/96 Plan for Today Shift-reduce parsing The problem with - PowerPoint PPT Presentation

* 07/16/96 Plan for Today Shift-reduce parsing The problem with predictive top down parsing CS453 : Shift Reduce Parsing LR parsing: bottom up Unambiguous Grammars LR(0) and SLR Parse Tables Performs a right-most derivation in


  1. * 07/16/96 Plan for Today Shift-reduce parsing – The problem with predictive top down parsing CS453 : Shift Reduce Parsing – LR parsing: bottom up Unambiguous Grammars LR(0) and SLR Parse Tables – Performs a right-most derivation in reverse – Parsing unambiguous grammars – LR parsing table and parsing algorithm CS453 Shift-reduce Parsing 1 CS453 Shift-reduce Parsing 2 LL ¡vs ¡LR ¡ Shift-reduce parsing in an LR parser LL(k) must predict which production to use when seeing k LR(k) parser • tokens of the rhs: – Left-to-right parse – Right-most derivation S à SS | (S) | ε – K-token look ahead is it S à SS or (S) when I see ((((……. ? produces the parse tree TOP DOWN LR parsing algorithm – Performs a shift-reduce parse LR(k) postpones the decision until all tokens of the rhs plus k – Look at state at top of stack and input symbol to find action in table • more tokens have been seen. It therefore is more powerful. – 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 It does this by parsing BOTTOM UP – accept: stop and success • – error: stop and fail CS453 Shift-reduce Parsing 3 CS453 Shift-reduce Parsing 4 1

  2. * 07/16/96 LR ¡parsing ¡engine ¡ Example ¡LR ¡parse ¡ S à AB A à Aa | a B à Bb | b S’ à S $ 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 a aabb$ ß Aa abb$ ß Aa bb$ ß A b b$ ß ABb$ ß AB $ ß S$ ß S’ Shift push input token onto the stack Notice that this is the rightmost derivation • Reduce pop a rhs off the stack and push the corresponding lhs S’ à S$ à AB$ à ABb$ à Abb$ à Aabb$ à Aaabb$ à aaabb$ • on the stack in reverse! It accepts the string of tokens when the input is empty or LR(k) parsing scans the input Left to right and produces the $ (eof) and the stack contains the start symbol Rightmost derivation (looking k tokens ahead) in reverse. CS453 Shift-reduce Parsing 5 CS453 Shift-reduce Parsing 6 Simplified ¡example ¡LR ¡parsing ¡engine ¡ac5ons ¡ Shift reduce parsing applied to unambiguous grammars S à à AB A à à Aa | a B à à Bb | b S’ à à S $ [0] S à ( S ) Single parentheses nest [1] S ‘ à S $ Start symbol is S ’ Stack input action [2] S à ID aaabb$ S a aabb$ R : A à à a Stack input action A aabb$ S ((ID))$ S Aa abb$ R: A à à Aa ( (ID))$ S A abb$ S (( ID))$ S Aa bb$ R: A à à Aa ((ID ))$ R: S à Id A bb$ S ((S ))$ S Ab b$ R: B à à b ((S) )$ R: S à (S) AB b$ S (S )$ S ABb $ R: B à à Bb (S) $ R: S à (S) AB $ R: S à à AB S $ accept S $ accept CS453 Shift-reduce Parsing 7 CS453 Shift-reduce Parsing 8 2

  3. * 07/16/96 Table ¡driven ¡LR ¡parse ¡algorithm ¡ LR(0) SLR LALR LR(k) The stack contains grammar symbols and states: Given the shift reduce machinery described, there are various after a symbol is pushed, the next state is pushed algorithms for creating the parse table. The actions can now be formulated as: These algorithms get more and more sophisticated, starting – Sn : push input token; push n (ie goto state n) from simple LR(0) – Rk : rule k: lhs à rhs pop rhs of rule k off the stack, this exposes a state s Parser generators such as java-cup use the more sophisticated look up in row s and column lhs the next state gn to goto LALR parse table creation algorithm push lhs; push n (ie goto state n) – a : stop parsing, report success – _ : stop parsing, report error CS453 Shift-reduce Parsing 9 CS453 Shift-reduce Parsing 10 Table ¡Driven ¡LR ¡Parsing ¡ Example LR(0) Parse Table, Single Parentheses Nest [0] S -> ( S ) [1] S ’ -> S $ [2] S -> ID The ¡parsing ¡engine ¡is ¡a ¡DFA ¡plus ¡a ¡stack: ¡a ¡determinis7c ¡ pushdown ¡automaton. ¡The ¡DFA ¡is ¡described ¡by ¡a ¡transi7on ¡table: ¡ Action Goto ¡Rows ¡are ¡states ¡ State ( ) $ ID S ¡Column s ¡are ¡Grammar ¡symbols ¡(terminals, ¡non-­‑terminals) ¡ 0 s3 s1 2 ¡ ¡ ¡terminals: ¡ ¡shiD ¡or ¡reduce ¡and ¡go ¡to ¡next ¡state ¡ 1 r2 r2 r2 r2 ¡ ¡ ¡non-­‑terminals: ¡decide ¡which ¡state ¡to ¡go ¡to ¡aDer ¡a ¡reduc7on ¡ 2 accept ¡ ¡So ¡there ¡are ¡five ¡kinds ¡of ¡ac7ons ¡ 3 s3 s1 4 ¡ ¡ ¡ ¡sn: ¡ ¡ ¡shiD ¡input ¡and ¡go ¡to ¡state ¡ ¡n ¡ ¡ ¡ ¡ ¡rk: ¡ ¡ ¡reduce ¡according ¡to ¡rule ¡k, ¡then ¡gn: ¡go ¡to ¡state ¡n ¡ 4 s5 ¡ ¡ ¡ ¡ ¡ ¡a: ¡ ¡accept ¡ 5 r0 r0 r0 r0 ¡ ¡ ¡ ¡ ¡ ¡_: ¡ ¡error ¡ ¡(indicated ¡by ¡blank ¡state) ¡ In LR(0) parse tables, reduce actions happen for all terminals, as the parser does not look ahead . CS453 Shift-reduce Parsing 11 CS453 Shift-reduce Parsing 12 3

  4. * 07/16/96 LR(0) table construction Shift, reduce, goto actions in LR(0) table construction Example grammar for Lists: goto action: S’ à .S$ S’ à S.$ S à x. 0: S’ à à S$ 1: S à à ( L ) 2: S à à x 3: L à à S 4: L à à L , S Also in state 0 S à .x 3:V[S] 1:V[x] we could have S à .(L) We start with an empty stack and with a complete S$ sentence on input reduced to S 0:V[ ε ] We indicate this as follows: S’ à à . S$ S à (.L) shift action: this (a rule with a dot in it) is called an item, L à .L,S In state 1 we are at the end In state 0 we it indicates what is in the stack (left of .) of an item. This will give rise L à .S can shift an x and what is to be expected on input (right of .) S à .x to a reduce action or shift a ( The input can start with anything S can start with, eg an x or a ( S à .(L) We indicate this as follows: S’ à à .S$ 2:V[(] (we are making a DFA S à à .x Transitions: the shifts and goto-s explicitly connect the states. The throug another closure S à à .(S) reduces implicitly move to another state by popping the rhs off the remember the NFA à à DFA) stack, after which a goto with the lhs will produce a new next state We call this a state: state 0, the start state with an empty prefix (V[ ε ]) CS453 Shift-reduce Parsing 13 CS453 Shift-reduce Parsing 14 LR(0) Closure, Goto, State Diagram, Reduce LR(0) states and transitions Goto(I,X): // state I , symbol X Closure(I): // state I L à L,.S x x S if X==$ return {} // no gotos for $ repeat L à L,S. S’ à .S$ S à x. S à .x J = {} // new state for any item A à à α . X β S à .x 8:V[(L,S] 1:V[x] S à .(L) for any item A à à α . X β in I for any X à à γ S à .(L) 7:V[(L] , ( x J+= A à à α X . β I+= X à à . γ 0:V[ ε ] ( return Closure(J) // close it until I does not change S à (L.) S S à (.L) L List grammar State Diagram construction Reduce(T): ( L à L.,S L à .L,S 0: S’ à S$ T = Closure({ S’ à à .S$ } ); // states R = {} S’ à S.$ L à .S 1: S à ( L ) E = {} // edges (gotos and shifts) for each state I in T 3:V[S] 4:V[(L] S à .x 2: S à x repeat until no change in E or T for each item A à à α . S S à .(L) ) 3: L à S for each state I in T R += (I, A à à α ) 2:V[(] 4: L à L , S for each item A à à α . X β in I L à S. S à (L). J = Goto(I,X); 6:V[(S] 5:V[(L)] T+=J ; E += (X: (I,J)) // the edge (I,J) labeled X CS453 Shift-reduce Parsing 15 CS453 Shift-reduce Parsing 16 4

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