0% found this document useful (0 votes)
192 views102 pages

LR (0) and SLR

The document provides an overview of LR(0) and SLR(1) parsers, which are types of bottom-up parsers used in compiler construction. It explains the concepts of shift-reduce parsing, the structure of LR parsing tables, and the process of constructing LR(0) items and parsing tables. Additionally, it discusses the advantages of LR parsers and includes examples to illustrate the parsing process.

Uploaded by

anju.j3511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views102 pages

LR (0) and SLR

The document provides an overview of LR(0) and SLR(1) parsers, which are types of bottom-up parsers used in compiler construction. It explains the concepts of shift-reduce parsing, the structure of LR parsing tables, and the process of constructing LR(0) items and parsing tables. Additionally, it discusses the advantages of LR parsers and includes examples to illustrate the parsing process.

Uploaded by

anju.j3511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

LR(0) and SLR(1) Parsers

1
Contents
• Introduction to bottom up parsers
• LR(0) Parser
• Example problem
• Gate Questions and solutions
• SLR(1) Parser
• Example problem
• Gate Questions and solutions
Bottom up Parser
• Construction of parse tree for any given input string
beginning at the bottom and working towards the root is
called bottom up parser
For example the given input string : id*id

E -> E + T | T id*id F * id T * id T*F T E


T -> T * F | F
F -> (E) | id
id F F id T*F T

id id F id T*F

id F id

id
Shift-Reduce Parsing
• The general idea is to shift some symbols of input to the
stack until a reduction can be applied
• At each reduction step, if a specific substring is matched
then the body of a production is replaced by the Non
Terminal at the head of the production A—>ac/b
• The key decisions during bottom-up parsing are about when
to reduce and what production should apply
• A reduction is a reverse of a step in a derivation
• The goal of a bottom-up parser is to construct a derivation in
reverse:
– E=>T=>T*F=>T*id=>F*id=>id*id
LR Parsers
LR(k)
LR k

Left-to-Right Rightmost Derivation In Number Of Input


scan Reverse Symbols Of Look
Ahead

5
❖ Types of LR Parsers
1.LR(0) Parser
2.Simple LR-Parser (SLR)
3.Canonical LR Parser (CLR)
4.LALR Parser.

6
Comparison of LL & LR Methods

LL(1)

LR(1) LALR SLR LR(0) LL(0)

7
▪ Advantages of LR Parsers
▪ LR parsers are constructed to recognize all
Programming Languages

▪ The LR-parsing is Non-Backtracking Shift-


Reduce Parser

▪ An LR parser can detect a syntactic errors

▪ It scans input string from left-to-right and use


left most derivation in reverse

8
•The LR Parsing Algorithm
Input

a1 a2 … ai … an $ Scanner

sm LR Parsing Engine
Xm
sm-1
Xm-1
Compiler Construction

s0 Parser
Action Goto Grammar
Generator
Stack
LR Parsing Tables

9
❖ The LR parser consists of 1) Input 2)Output
3)Stack 4) Driver Program 5) Parsing Table

❖ The Driver Program is same for all LR Parsers.

❖ Only the Parsing Table changes from one parser


to the other.

❖ In CLR method the stack holds the states from the


LR(0)automation and canonical LR and LALR
methods are same

10
❖ The Driver Program uses the Stack to store a
string

s0X1s1X2…Xmsm

✓ Where sm is the Top of the Stack.

✓ The Sk‘s are State Symbols

✓ The Xi‘s are Grammar Symbols.

✓ Together State and Grammar Symbols


determine a Shift-reduce Parsing Decision.

11
❖ The Parsing Program reads characters from an
Input Buffer one at a time

❖ The Current Input Symbols are used to index the


parsing table and determine the shift-reduce
parsing decision

❖ In an implementation, the grammar symbols need


not appear on the stack

12
Parse Table
❖ The LR Shift-Reduce Parsers can be efficiently
implemented by computing a table to guide the
processing

❖ The Parsing Table consists of two parts:

1. A Parsing Action Function and

2. A GOTO function.

13
❖ The Action Table
❖ The Action Table specifies the actions of the parser
(e.g., shift or reduce), for the given parse state and
the next token

✓ Rows are State Names;

✓ Columns are Terminals

14
LR Driver Program
❖ The LR driver Program determines Sm, the state on
top of the stack and ai, the Current Input symbol.

❖ It then consults Action[ Sm, ai ] which can take one


of four values:

✓ Shift

✓ Reduce

✓ Accept

✓ Error

15
❖ If Action[ Sm, ai ] = Shift S

✓ Where S is a State, then the Parser pushes


ai and S on to the Stack.

❖ If Action[ Sm, ai ] = Reduce A → β,

✓ Then ai and Sm are replaced by A

✓ if S was the state appearing below ai in the


Stack, then GOTO[S, A] is consulted and the
state pushed onto the stack.

16
❖ If Action[ Sm, ai ] = Accept,

✓ Parsing is completed

❖ If Action[ Sm, ai ] = Error,

✓ The Parser discovered an Error.

17
❖ GOTO Table
❖ The GOTO table specifies which state to put on
top of the stack after a reduce

✓Rows are State Names;

✓Columns are Non-Terminals

18
❖ The GOTO Table is important to find out the next
state after every reduction.

❖ The GOTO Table is indexed by a state of the


parser and a Non Terminal (Grammar Symbol)

ex : GOTO[S, A]

❖ The GOTO Table simply indicates what the next


state of the parser if it has recognized a certain
Non Terminal. 19
❖ Right Sentential Form is a Sentential Form in a
Rightmost Derivation

Example: (S)S , ( (S)S)

❖ Viable Prefix is a sequence of symbols on the


parsing stack

Example:

✓ (S)S, (S), (S, (,

✓ ((S)S, ((S), ((S , ((, (

20
LR(0) Parser
❖ The LR Parser is a Shift-reduce Parser that makes
use of a Deterministic Finite Automata, recognizing
the Set Of All Viable Prefixes by reading the stack
from Bottom To Top.

❖ if a Finite-State Machine that recognizes viable


prefixes of the right sentential forms is constructed,
it can be used to guide the handle selection in the
Shift-reduce Parser.

21
❖ Handle: Handle is a substring that matches the
body of a production
❖ Handle is a Right Sentential Form + position
where reduction can be performed + production
used for reduction

Example

( S ) S. With S → Є

( S ) .S With S → S

((S)S.) With S → ( S ) S

22
• Handle pruning : Handle pruning specifies that the reduction
represents one step along the reverse of a rightmost
derivation

Right sentential form Handle Reducing production

id*id id F->id
F*id F T->F
T*id id F->id

T*F T*F E->T*F


Augmented Grammar
❖ If G is a Grammar with Start Symbol S, the
Augmented Grammar G’ is G with a New Start
Symbol S`, and New Production S` →S$.
.

❖ The Purpose of the Augmented Grammar is to


indicate to the parser when it should stop parsing
and announce acceptance of the input

24
LR(0) Items
An LR(0) Item of a Grammar G is a Production of G
with a Dot ( ) at some position of the right side.

.Production A → XYZ yields the Four items:

1. A→•XYZ We hope to see a string derivable


from XYZ next on the input.

2. A→X•YZ We have just seen on the input a


string derivable from X and that we hope next
to see a string derivable from YZ next on the
input. 25
3. A→XY•Z

4. A→XYZ•

❖ The production A→ generates only one item,


A→•.

❖ Each of this item is a Viable prefixes

❖ Closure Item : An Item created by the closure


operation on a state.

❖ Complete Item : An Item where the Item Dot is


at the end of the RHS.
26
LR(0) Example

Context Free Grammar:


S→E rule1: (S→S’)
E→T Augumented Grammar
E→E+T
T→i S → •E
T→(E) E → •T
E → •E+T
T → •i
T → •(E)
27
Construction of LR(0) Closure Items
S6
E → T•

T T S7
S0
S → •E$ T → (•E)
E → •T E → •T
( E → •E+T
E → •E+T
T → •i T → •i
T → •(E) S5 T → •(E) (
i
i

E T → i• E
( S8
S1
S → E•$ T → (E•)
E → E•+T i E → E•+T

+ +
)
$ E → E+•T
S3 T → •i
S2 T → •(E) S9

S → E$• T → (E)•
T
S4
28
E → E+T•
Construction of LR(0) Items

I0: I1:Goto(I0,E) I2 :Goto(I1,$ ) I3 :Goto(I1,+ ) I4 :Goto(I3,T )


S → •E$ S→E•$ S→E$• E →E+T •
E→E+•T
E→•T E→E•+T
T→•i
E→•E+T T → • (E)
I5 :Goto(I0,i) I5 :Goto(I3,i) I7 :Goto(I0,( )
T→•i T →( •E)
T→i• T→i•
T → • (E) I6 :Goto(I0,T) E→•T
E→T• E→•E+T
I8 :Goto(I7,E) I9 :Goto(I7, ) )
T→•i
T →( E•) T →( E )•
T → • (E)
T → E•+ T

29
Construction of DFA for LR(0) Items
$
I1 I2
+
E
T
i I3 I4
I5
i

I0 T i (
I6
T
( I9

I7 I8 S
E

30
Construction of LR(0) Parsing Table
Input
States Action Part (Terminals) Goto Part
(Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce 31
String Acceptance
Input
States Action Part (Terminals) Goto Part (Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce

Stack Input Action


S0 i + (i + i) $ Shift
S0 i S5 + (i + i) $ Reduce by T→i
S0 T S6 + (i + i) $ Reduce by E→T
S0 E S1 + (i + i) $ Shift
32
S0 E S1 + S3 (i + i) $ Shift
String Acceptance
Input
States Action Part (Terminals) Goto Part (Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce

Stack Input Action


S0 E S1 + S3 ( i + i) $ shift
S0 E S1 + S3 ( S7 i + i) $ shift
S0 E S1 + S3 ( S7iS5 + i)$ reduce by T→i
S0 E S1 + S3 ( S7TS6 + i)$ reduce by E→T
S0 E S1 + S3 ( S7ES8 shift 33
String Acceptance
Input
States Action Part (Terminals) Goto Part (Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce

Stack Input Action


S0 E S1 + S3 ( S7ES8 +i)$ shift
S0 E S1 + S3 ( S7ES8+S3 i)$ shift
S0 E S1 + S3 ( S7ES8+S3iS5 ) $ reduce by T→i
S0 E S1 + S3 ( S7ES8+S3TS4 ) $ reduce by E→E+T
S0 E S1 + S3 ( S7ES )$ shift 34
String Acceptance
Input
States Action Part (Terminals) Goto Part (Non-
Terminals)
i + ( ) $ E T
0 5 7 1 6 Shift
1 3 2 Shift
2 S→E$ Reduce
3 5 7 4 Shift
4 E→E+T Reduce
5 T→i Reduce
6 E→T Reduce
7 5 7 8 6 Shift
8 3 9 Shift
9 T→(E) Reduce

Stack Input Action


S0 E S1 + S3 ( S7ES8 )$ shift
S0 E S1 + S3 ( S7ES8)S9 $ reduce by T→(E)
S0 E S1 + S3TS4 $ reduce by E→E+T
S0 E S1 $ shift
S0 E S1$S2 reduce by S→E$
S0 S accept 35
Draw backs of LR(0) Parser
❖ LR(0) is the Simplest Technique in the LR family.

❖ LR(0) Parsers are too weak to be of practical use

❖ LR(0) accepts only small class of LR(0) grammar


because if conflicts occurs.

❖ The Fundamental Limitation of LR(0) is that no look


ahead tokens are used.

❖ LR(0) Parsing is the weakest and it is not used


much in practice because of its limitations.
36
GATE QUESTIONS AND SOLUTIONS
Question 1
Consider the grammar
E→E+n|E×n|n
For a sentence n + n × n, the handles in the right-sentential form
of the reductions are ? (GATE 2005)

A. n, E + n and E + n × n
B. n, E + n and E + E × n
C. n, n + n and n + n × n
D. n, E + n and E × n
Explanation

• E → E * n {Applying E → E * n }
• E→ E + n * n {Applying E → E + n }
• E→ n + n * n {Applying E → n } Hence, the handles in right
sentential form is n, E + n and E × n.
• Hence Option D is the right choice
Question 2
A bottom-up parser generates:

A. Left-most derivation in reverse


B. Right-most derivation in reverse
C. Left-most derivation
D. Right-most derivation
Explanation

• A bottom-up parser generates right-most derivation in reverse


• Option (B) is correct.
Question 3
Consider the following statements related to compiler
construction :
I. Lexical Analysis is specified by context-free grammars and
implemented by pushdown automata.
II. Syntax Analysis is specified by regular expressions and
implemented by finite-state machine.
Which of the above statement(s) is/are correct ?

A. Only I
B. Only II
C. Both I and II
D. Neither I nor II
Explanation

• Both statements are wrong for detailed information on lexical


analysis and syntax analysis
• option (D) is correct.
Question 4
Which of these is true about LR parsing?

A. Is most general non-backtracking shift-reduce parsing


B. It is still efficient
C. Both a and b
D. None of the mentioned
Explanation

• LR parsers are a type of bottom-up parsers that efficiently


handle deterministic context-free languages in guaranteed
linear time.
• option (C) is correct.
Question 5
Which of the following is incorrect for the actions of A LR-Parser
I) shift s
ii) reduce A->ß
iii) Accept
iv) reject?

A. Only I)
B. I) and ii)
C. I), ii) and iii)
D. I), ii) , iii) and iv)
Explanation

• Only reject out of the following is a correct LR parser action


• Option C is correct
Question 6
If a state does not know whether it will make a shift operation or
reduction for a terminal is called

A. Shift/reduce conflict
B. Reduce /shift conflict
C. Shift conflict
D. Reduce conflict
Explanation

• As the name suggests that the conflict is between shift and


reduce hence it is called shift reduce conflict
• Option A is correct
Question 7
When there is a reduce/reduce conflict?

A. If a state does not know whether it will make a shift operation


using the production rule i or j for a terminal.
B. If a state does not know whether it will make a shift or
reduction operation using the production rule i or j for a
terminal.
C. If a state does not know whether it will make a reduction
operation using the production rule i or j for a terminal.
D. None of the mentioned
Explanation

• It occurs when If a state does not know whether it will make a


reduction operation using the production rule i or j for a
terminal.
• Option C is correct
SLR(1) Parser
❖ We will find that it allows for a much larger class of
grammars to be parsed.
❖ SLR(1) Parser is used for accepting the certain
grammar which is not accepted by LR(0) parser
❖ The letters “SLR” stand for “Simple”, “Left” and
“Right”.
✓ “Left” indicates that the input is read from left
to right and
✓ The “Right” indicates that a right-derivation is
built. 52
❖ SLR(1) Parser stands for Simple LR(1).

❖ SLR(1) parsers use the same LR(0) Configurating


Sets and have the Same Table Structure and Parser
Operation, So everything you've already learned
about LR(0) applies here.

❖ The difference in SLR(1) Parser with LR(0) Parser


comes in Assigning Table Actions,

53
❖ SLR(1) Parsers are going to use one token of
lookahead to eliminate the conflicts.

❖ In LR(0) parsing, Reduce Actions that cause the


problem

❖ The Simple Improvement that SLR(1) makes on the


basic LR(0) parser is to reduce only if the next input
token is a member of the Follow Set of the non-
terminal being reduced.

54
❖ SLR(1) parser will perform a reduce action for
configuration B→a• if the lookahead symbol is in
the set Follow(B)

❖ A Grammar is an SLR(1) grammar if there is no


conflict in the grammar.
❖ Clearly SLR(1) is a proper superset of LR(0)

SLR(1) LR(0)

55 55
Example: SLR(1) Parser
❖ Construct the SLR(1) Parser for the Following
Grammar

Context Free Grammar:


E→E+T
E→T
T→T * F
T→F
F→(E)
F → id
56
Step 1: Define a Augmented Grammar
Context Free Grammar: Augmented Grammar:
E→E+T E’ → E#
E→T E→E+T
T→T * F E→T
.
T→F T→T * F
F→(E) T→F
F → id F→(E)
F → id

57
Step2 : Constructing SLR(1) Automaton

Context Free Grammar: Adding the SLR(1) Item

E’ → E# E’ →.E#
E → .E + T
E→E+T
E→T

E → .T
T→T * F
T→F

T → .T * F
F→(E)
F → id

T → .F

F → .( E )

F → . id 58
E → E+.T S9
S1 E’ → E.# +
T → .T*F T E → E + T.
E → E.+T T → .F T → T.* F
F F → .(E) S6
F → .id
E
S3 id
( *
S5
S0 E’ →.E# T S4
S
E →.E+T E2→ T. S10
E →.T T→ T. * F * T → T*F.
F
T →.T*F S7
T →.F T → T*.F
F →.(E) F →.(E)
T F →.id
F →.id (
S11
id ( id
S4 F → (E).
S5 F →(.E) S5
F F → id. id E →.E+T
)
E →.T
F T →.T*F E
T →.F F → (E.)
S3 F →.(E) E → E.+T
T → F. F →.id S8
(
+ 59
S6
Constructing the SLR(1) Items
I0: I1:Goto(I0,E ) I6:Goto(I1,+) I3:Goto(I4,F) I4:Goto(I7,( )
E→ E.# T→ F.
E’ →.E# E →E+.T
E→ E.+T I5:Goto(I7,id)
E →.E + T T→.T * F I4:Goto(I4,( )
E →.T I2:Goto(I0,T) T→.F
E→ T. F→.( E ) I5:Goto(I4,id) I3:Goto(I4,F)
T →.T * F
E→ T.*F F →.id
T →.F I9:Goto(I6,T) I7:Goto(I9,* )
F →.( E ) I3:Goto(I0,F) I7:Goto(I2,*)
E →E+T.
F →.id T→ F. E→ T*.F I6:Goto(I8,+)
T→ T.* F
F→ .( E )
I4:Goto(I0,( ) I5:Goto(I0,id) I4:Goto(I6,( )
F→ .id
T→id.
F→(. E)
I10:Goto(I7,F) I5:Goto(I6,id)
E→.E+T I8:Goto(I4,E)
T→T * F . T→id.
E→.T E→ (E.)
T→.T * F E →E.+T I3:Goto(I6,F)
I11:Goto(I8,) )
T→.F T→ F.
I2:Goto(I4,T) T→(E).
F→.( E ) E→ T. 60
F →.id E→ T.*F
Construction of DFA for SLR(1) Items

+ T
S6 S9 *
S1
( S7
F
id
E S3
S4
S2 S5
T *
T ( F
( S10
S0 S7
S4 S3 (
id id
F S4
id E
F S5
S5
S8 )
S11
+
S3
S 61
Construction of Follow Function
E’ →.E# Follow (E) = { # , + , ) }
E →.E + T ( r1 )
Follow (T) = { * , # , + , ) }
E →.T ( r2 )
T →.T * F ( r3 ) Follow (F) = { * , # , + , ) }
T →.F ( r4 )
F →.( E ) ( r5 )

F →.id ( r6 )

62
Constructing the SLR(1) Parsing Table
Input
States Action Part Goto Part

id + * ( ) $ E T F
0 S5 S4 1 2 3
1 S6 Acc
2 r2 S7 r2 r2
3 r4 r4 r4 r4
4 S5 S4 8 2 3
5 r6 r6 r6 r6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 r1 S7 r1 r1
10 r3 r3 r3 r3 63

11 r5 r5 r5 r5
Input Acceptance

64
String Acceptance
id * id + id id * id + id$

In S0 with input
symbol id, shift the
symbol onto the stack
and enter state S5

65
String Acceptance
id * id + id id * id + id$

S5 with input symbol *

Reduce
using grammar
production6

66
String Acceptance
id * id + id id * id + id$

S5 with input symbol *

Reduce
using grammar
production6

67
String Acceptance
id * id + id id * id + id$

T2

Reduction exposes S0
and goto of S0 gives
next state for the
leading non-terminal,
T. The next state is S2

68
String Acceptance
id * id + id id * id + id$

69
String Acceptance
id * id + id id * id + id$

70
String Acceptance
id * id + id id * id + id$

71
String Acceptance
id * id + id id * id + id$

(7) 0T2*7 F 10

Reduction exposes S7
and goto of S7 gives
next state for the
leading non-terminal,
F. The next state is S10

72
String Acceptance
id * id + id id * id + id$

(7) 0 T 2 * 7 F 10 + id $ Reduce T → T*F

(8) 0T2 + id $

73
String Acceptance
id * id + id id * id + id$

(8) 0T 2 + id $ Reduce E → T

74
String Acceptance
id * id + id id * id + id$

(8) 0T + id $ Reduce E → T
(9) 0 E 1 + id $

75
String Acceptance
id * id + id id * id + id$

(9) 0E 1 + id $ Shift
(10) 0E1+6 id $

76
String Acceptance
id * id + id id * id + id$

(10) 0E1+6 id $ Shift


(11) 0 E 1 + 6 id 5 $

77
String Acceptance
id * id + id id * id + id$

(11) 0 E 1 + 6 id 5 $ Reduce F → id
(12) 0E1+6 F $

78
String Acceptance
id * id + id id * id + id$

(12) 0E1+6 F 3 $

(12) 0E1+6 F3 $

79
String Acceptance
id * id + id id * id + id$

(12) 0E1+6 F3 $ Reduce T → F

(13) 0E1+6 T $

80
String Acceptance
id * id + id id * id + id$

(13) 0E1+6 T 9 $

81
String Acceptance
id * id + id id * id + id$

(13) 0E1+6 T9 $ Reduce E→E+T

(14) 0E1 $

82
String Acceptance
id * id + id id * id + id$

(13) 0E1 $ Accept

83
String Acceptance id * id + id$
STACK INPUT ACTION

(1) 0 id * id + id$ shift


(2) 0 id 5 * id + id$ reduced by F → id
(3) 0 F5 * id + id$ reduced by T→F
(4) 0 T2 * id + id$ shift
(5) 0 T2*7 id + id$ shift
(6) 0 T 2 * 7 id 5 + id$ reduced by F → id
(7) 0 T 2 * 7 F 10 + id$ reduced by T → T*F
(8) 0 T2 + id$ reduced by E→T
(9) 0 E1 + id$ shift
(10) 0 E1+6 id$ shift
(11) 0 E 1 + 6 id 5 $ reduced by F → id
(12) 0 E1+6F3 $ reduced by T→F
(13) 0 E1+6T9 $ E→E+T
(14) 0 E1 $Y 84
Observation
❖ the Shift/Reduce Conflict arises from the fact that
the SLR parser construction method is not powerful
enough to remember enough left context to decide
what action the parser should take on input = has
seen a string reducible to L.

❖ That is “R=“ cannot be a part of any Right


Sentential Form. So when “L” appears on the top of
stack and “=“ is the current character of the input
buffer , we can not reduce “L” into “R”.
85
❖ Every SLR Grammar is Unambiguous, but Every
Unambiguous Grammar is not a SLR grammar.

❖ If the SLR parsing table of a grammar G has a Conflict,


we say that Grammar is not SLR Grammar.

86
Drawbacks
❖ SLR(1) parsers cannot parse some LR grammars.

❖ The Main Problem of SLR(1) Parser is that Lookahead


Information is added to LR(0) parser at the end of
construction based on FOLLOW sets

❖ In SLR(1) parsing, we reduce A→α for ANY lookahead a


Є FOLLOW(A), which is too general such that
sometimes a reduction cannot occur for some a Є
FOLLOW(A)
87
Drawbacks
❖ if the Grammar contains Reduce/Reduce Conflict then,
we say that Grammar is not SLR(1) Grammar.

88
GATE QUESTIONS AND SOLUTIONS
Question 1
• Consider the following two statements: P: Every regular
grammar is LL(1) Q: Every regular set has a LR(1) grammar
Which of the following is TRUE? (GATE 2007)

A. Both P and Q are true


B. P is true and Q is false
C. P is false and Q is true
D. Both P and Q are false
Explanation
• A regular grammar can also be ambiguous also For example,
consider the following grammar, S → aA/a A → aA/ε In above
grammar, string 'a' has two leftmost derivations.
• (1) S → aA (2) S → a S->a (using A->ε) And LL(1) parses only
unambiguous grammar, so statement P is False.
• Statement Q is true is for every regular set, we can have a
regular grammar which is unambiguous so it can be parse by
LR parser.
• So option C is correct choice
Question 2
A canonical set of items is given below
S → L. > R
Q → R.
On input symbol < the set has? (GATE 2014)

A. a shift-reduce conflict and a reduce-reduce conflict.


B. a shift-reduce conflict but not a reduce-reduce conflict.
C. a reduce-reduce conflict but not a shift-reduce conflict.
D. neither a shift-reduce nor a reduce-reduce conflict
Explanation
• The question is asked with respect to the symbol ' < ' which is
not present in the given canonical set of items.
• Hence it is neither a shift-reduce conflict nor a reduce-reduce
conflict on symbol '<‘.
• So option D is correct choice
• But if the question would have asked with respect to the
symbol ' > ' then it would have been a shift-reduce conflict.
Question 3
Which of the following is true?

A. Canonical LR parser is LR (1) parser with single look ahead


terminal
B. All LR(K) parsers with K > 1 can be transformed into LR(1)
parsers.
C. Both (A) and (B)
D. None of the above
Explanation

• Canonical LR parser is LR (1) parser with single look ahead


terminal. All LR(K) parsers with K > 1 can be transformed into
LR(1) parsers.
• Option (C) is correct.
Question 4
The construction of the canonical collection of the sets of LR (1)
items are similar to the construction of the canonical collection of
the sets of LR (0) items. Which is an exception?

A. Closure and goto operations work a little bit different


B. Closure and goto operations work similarly
C. Closure and additive operations work a little bit different
D. Closure and associatively operations work a little bit different
Explanation

• Closure and goto do work differently in case of LR (0) and LR


(1)
• Option (A) is correct.
Question 5
When ß ( in the LR(1) item A -> ß.a,a ) is not empty, the look-
head

A. Will be affecting.
B. Does not have any affect.
C. Shift will take place.
D. Reduction will take place.
Explanation

• There is no terminal before the non terminal beta


• Option (B) is correct.
Question 6
When ß is empty (A -> ß.,a ), the reduction by A-> a is done

A. If next symbol is a terminal


B. Only If the next input symbol is a
C. Only If the next input symbol is A
D. Only if the next input symbol is a
Explanation

• The next token is considered in this case it’s a


• Option (D) is correct.
Thank U

102

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy