1 / 28

CS 31003: Compilers

CS 31003: Compilers. Difference between SLR and LR(1) Construction of LR(1) parsing table LALR parser. Bandi Sumanth 11CS30006 Date : 9/10/2013. Differences between SLR and LR(1). SLR , Non SLR and LR(1) grammar. SLR grammar

ophira
Download Presentation

CS 31003: Compilers

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. CS 31003: Compilers Difference between SLR and LR(1) Construction of LR(1) parsing table LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013

  2. Differences between SLR and LR(1)

  3. SLR , Non SLR and LR(1) grammar • SLR grammar • A grammar is said to be an SLR grammar if the SLR parsing table for the grammar has no conflicting entries

  4. SLR , Non SLR and LR(1) grammar + A → B . + C A → B . A → B + . C Sj Si

  5. SLR , Non SLR and LR(1) grammar • LR(1) grammar • If the current state is Si and + is the next token, a shift is made leading to the state Sj • Reduction cannot be made because the lookahead symbol * and the next token + does not match + A → B . + C , = A → B . , * A → B + . C Sj Si

  6. Construction of LR(1) parsing table

  7. Constructing LR(1) parsing table Ś → S S → C C C → c C | d • Consider the following augmented grammar

  8. LR(1) sets of items

  9. LR(1) parsing table for the grammar

  10. LR(1) vs SLR parser • LR(1) parser is better than SLR parser because LR(1) accepts more languages than SLR • SLR(1) is weak because it has no lookahead information • But the cost is LR(1) has more states than SLR and thus it consumes more space • LR(1) is impractical because its lookaheadinformation makes the automaton too big

  11. Can we retain the LR(1) automaton's lookaheadinformation without all its states?

  12. Review of LR(1) • Each state in an LR(1) automaton is a combination of an LR(0) state and lookaheadinformation. • Two LR(1) items have the same core if they are identical except for lookahead.

  13. A Surprisingly Powerful Idea • In an LR(1) automaton, we have multiple states with the same core but different Lookahead • What if we merge all these states together? • This is called LALR parser Lookahead LR parser

  14. Advantages of LALR(1) Maintains lookahead information Keeps automaton small Resulting automaton has same size as LR(0) automaton

  15. The End

More Related