html5-img
1 / 10

Introduction to LR Parsing

Introduction to LR Parsing. Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-1155 Storrs, CT 06269. aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias. Example. Consider: S  a AB e

daxia
Download Presentation

Introduction to LR Parsing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-1155 Storrs, CT 06269 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

  2. Example Consider: S  aABe A  Abc | b B  d Rightmost Derivation of the string abbcde: S  aABe  aAde  aAbcde  abbcde The (unique) handle is underlined for each step. A viable prefix is (1) a string that equals a prefix of a right-sentential form up to (and including) its unique handle. (2) any prefix of a string that satisfies (1) Examples: a, aA, aAd, aAbc, ab, aAb,… Not viable prefixes: aAde, Abc, aAA,…

  3. Shift/Reduce Parser STACK INPUT Remark $ abbcde$ SHIFT $a bbcde$ SHIFT $ab bcde$ REDUCE $aA bcde$ SHIFT $aAb cde$ SHIFT (?) $aAbc de$ REDUCE $aA de$ SHIFT $aAd e$ REDUCE $aAB e$ SHIFT $aABe$ REDUCE $S$ ACCEPT Observe: all Strings in the stack are viableprefixes

  4. When to shift? When to Reduce? • Sometimes on top of the stack something appears to be a handle (i.e., matches the RHS of a production). • But: maybe we have not shifted enough elements to identify the handle. • Observe the correct sequence of Shift and Reducesteps preserves the property that the stack IS a viable prefix. Example $aAb cde$ Shift or Reduce? • If we shift we obtain aAbc in the stack. • Recall that Abc is a handle. • Instead if we reduce we obtain aAA in the stack. (this is NOT a viable prefix!!!)

  5. When to Shift? When to Reduce? II • In order to make shift/reduce decisions: • We need to look to perhaps a few elements inside the stack. • We need to make sure that the way we modify the stack preserves the “viable prefix condition.” • For our previous example: • Any b appearing to the right of “A” should not be reduced. • In fact we can come up with heuristic decisions based on the grammar structure: • A “b” is reduced only if it is to the right of “a” • PROBLEM: what kind of information do we need to store inside the stack so that we can make decisions as above just by looking at the top element?

  6. LR Parsing • LR (left-to-right, rightmost derivation). • LR(1) = 1 lookahead symbol. • Use stack • Stack contains “more information” (in a compressed form) compared to a Top-Down Table-driven parser. • LR(1): • Decisions are taken looking at the top of the stack + 1 input element.

  7. Anatomy of an LR parser a + b $ s X s X Input (String + terminator) “States” Stack NT + T symbols of CFG LR Parsing Program Output What actions parser should take based on stack / input Parsing Table action[.,.] goto[.,.] • General parser behavior: s : top of stack a : current input • 1. If action[s,a]=“accept” halt, accept, success • If action[s,a]=“reduce by production A ” do the following:2a. Pop 2*|| elements from the stack.2b. Push A2c. Push goto[s*,A] • If action[s,a]=“shift and goto state s*”Shift; push s*

  8. Example 1. S  aABe 2. A  Abc 3. A  b 4. B  d action goto

  9. Example, II STACK INPUT Remark $0 abbcde$

  10. Interesting Fact + LR Parsing Table Construction Methods HOW TO CONSTRUCT SUCH TABLES? • The set of all viable prefixes is Regular. • It is possible to write a DFA that recognizes it! • Use the DFA as an aid to construction of the table. Design Methodologies: • SLR (simple LR) “short table but limited methodology.” • Canonical LR“general methodology but big table.” • LALR (lookahead LR)“in between”

More Related