CS 31003: Compilers
description
Transcript of CS 31003: Compilers
![Page 1: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/1.jpg)
CS 31003: Compilers
Difference between SLR and LR(1) Construction of LR(1) parsing table LALR parser
Bandi Sumanth11CS30006Date : 9/10/2013
![Page 2: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/2.jpg)
Differences between SLR and LR(1)
![Page 3: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/3.jpg)
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
![Page 4: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/4.jpg)
SLR , Non SLR and LR(1) grammar
A → B + . C A → B . + C A → B .
+
Si
Sj
![Page 5: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/5.jpg)
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 . + C , = A → B . , *
+
Si
Sj
![Page 6: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/6.jpg)
Construction of LR(1) parsing table
![Page 7: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/7.jpg)
Constructing LR(1) parsing table
● Consider the following augmented grammar
Ś → SS → C CC → c C | d
![Page 8: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/8.jpg)
LR(1) sets of items
![Page 9: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/9.jpg)
LR(1) parsing table for the grammar
![Page 10: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/10.jpg)
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 lookahead information makes the automaton too big
![Page 11: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/11.jpg)
Can we retain the LR(1) automaton's lookahead information without all its states?
![Page 12: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/12.jpg)
Review of LR(1)
● Each state in an LR(1) automaton is a combination of an LR(0) state and lookahead information.● Two LR(1) items have the same core if they are identical except for lookahead.
![Page 13: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/13.jpg)
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
![Page 14: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/14.jpg)
![Page 15: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/15.jpg)
![Page 16: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/16.jpg)
![Page 17: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/17.jpg)
![Page 18: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/18.jpg)
![Page 19: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/19.jpg)
![Page 20: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/20.jpg)
![Page 21: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/21.jpg)
![Page 22: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/22.jpg)
![Page 23: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/23.jpg)
![Page 24: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/24.jpg)
![Page 25: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/25.jpg)
![Page 26: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/26.jpg)
![Page 27: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/27.jpg)
Advantages of LALR(1)
● Maintains lookahead information
● Keeps automaton small
● Resulting automaton has same size as LR(0) automaton
![Page 28: CS 31003: Compilers](https://reader035.fdocuments.net/reader035/viewer/2022062222/56815b17550346895dc8c851/html5/thumbnails/28.jpg)
The End