CT - Lecture 6
CT - Lecture 6
Lecture 6
LR parser
that is used by computer programming language compilers and other associated tools.
▪ LR parser scans the input stream from left to right and produces a right-most derivation.
▪ k refers to the number of unconsumed “look ahead symbol” input symbols that are
used in making parser decisions. Typically, k is 1 and is often omitted.
▪ A context-free grammar is called LR (k) if the LR (k) parser exists for it.
This first reduces the sequence of tokens to the left.
DESCRIPTION OF LR PARSER :
▪ LR(1):
▪ LR parser
▪ Slow construction
DESCRIPTION OF LR PARSER :
▪ SLR(1):
▪ simple LR parser
▪ LALR(1):
LR Parsing structure is the same for all the parser, but the parsing table is
different for each parser. It consists following components as follows.
Input Buffer –
Stack –
The combination of state symbol and current input symbol is used to refer
to the parsing table to take the parsing decisions.
DESCRIPTION OF LR PARSER :
Parsing Table :
▪ Parsing table is divided into two parts- The action table and the Go-To
table.
▪ The action table gives a grammar rule for implementing the current state
and terminal in the input stream. There are four cases used in the action
table as follows.
DESCRIPTION OF LR PARSER :
No action -
DESCRIPTION OF LR PARSER :
The symbol m mentioned in the left-hand side of rule m says that state is
The symbol m mentioned in the left-hand side of rule m says that a new
state is looked up in the go to table and made the new current state by
▪ It helps the parser to identify when to stop the parsing and announce the
▪Example
▪ Given grammar
▪ S → AA
▪ A → aA | b
▪ The Augment grammar G` is represented by
▪ S`→ S
▪ S → AA
▪ A → aA | b
CANONICAL COLLECTION OF LR(0) ITEMS
▪ Example
▪ Given grammar
▪ S → AA
▪ A → aA | b
▪ Add Augment Production and insert '•' symbol at the first
position for every production in G
▪ S` → •S
▪ S → •AA
▪ A → •aA
▪ A → •b
CANONICAL COLLECTION OF LR(0) ITEMS
▪ I0 State:
▪ Add Augment production to the I0 State and Compute the Closure
▪ I0 = Closure (S` → •S)
▪ Add all productions starting with S in to I0 State because "•" is followed
by the non-terminal. So, the I0 State becomes
▪ I0 = S` → •S
▪ S → •AA
▪ Add all productions starting with "A" in modified I0 State because "•" is
followed by the non-terminal. So, the I0 State becomes.
CANONICAL COLLECTION OF LR(0) ITEMS
▪ I0 State:
▪ Add all productions starting with "A" in modified I0 State
because "•" is followed by the non-terminal. So, the I0 State
becomes.
▪ I0= S` → •S
▪ S → •AA
▪ A → •aA
▪ A → •b
CANONICAL COLLECTION OF LR(0) ITEMS
▪ I1 State:
▪ I1= Go to (I0, S) = closure (S` → S•) = S` → S•
▪ I1= S` → S•
CANONICAL COLLECTION OF LR(0) ITEMS
▪ I2 State:
▪ I2= Go to (I0, A) = closure (S → A•A)
▪ Add all productions starting with A in to I2 State because "•" is followed
by the non-terminal. So, the I2 State becomes
▪ I2 =S→A•A
▪ A → •aA
▪ A → •b
▪ Go to (I2,a) = Closure (A → a•A) = (same as I3)
▪ Go to (I2, b) = Closure (A → b•) = (same as I4)
CANONICAL COLLECTION OF LR(0) ITEMS
▪ I3 State:
▪ I3= Go to (I0,a) = Closure (A → a•A)
▪ Add productions starting with A in I3.
▪ A → a•A
▪ A → •aA
▪ A → •b
▪ Go to (I3, a) = Closure (A → a•A) = (same as I3)
▪ Go to (I3, b) = Closure (A → b•) = (same as I4)
CANONICAL COLLECTION OF LR(0) ITEMS
▪I4,I5,I6 States:
▪ I4= Go to (I0, b) = closure (A → b•) = A → b•