0% found this document useful (0 votes)
6 views17 pages

Module 2-F CLR Parsing

This document discusses augmented grammar and LR(1) items in compiler design, detailing the structure of augmented grammars and the concept of LR(1) items. It explains the closure operation for sets of items and provides an example grammar with its corresponding augmented grammar and collection of canonical LR(1) sets of items. The document is intended for educational purposes in the field of computer science and engineering.

Uploaded by

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

Module 2-F CLR Parsing

This document discusses augmented grammar and LR(1) items in compiler design, detailing the structure of augmented grammars and the concept of LR(1) items. It explains the closure operation for sets of items and provides an example grammar with its corresponding augmented grammar and collection of canonical LR(1) sets of items. The document is intended for educational purposes in the field of computer science and engineering.

Uploaded by

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

BCSE307L

Compiler Design
MODULE - 2
Dr.D.Jeya Mala
Professor
School of Computer Science and Engineering (SCOPE)
VIT Chennai
[email protected]
Augmented grammar
• If G is a grammar with start symbol S, then G’, the augmented
grammar for G, is G with a new start symbol S’ and production S’  S.
If G = (N, T, P, S), then G’ = (N U {S’} , T, PU {S’  S} , S’)

• The purpose of this new starting production is to indicate to the


parser when it should stop parsing and announce acceptance of the
input.
LR(1) Items
• Let G = (N, T, P ,S) be a CFG.
[ A  w1 . w2 , u ] is called LR(k) item if A  w1 . w2 is a production
from P, and u is a sequence of terminals, the length of which is less
or equal to k.
• LR(0) items should not contain sequence of terminals i.e.,
[ A  w1 . w 2 ]
• LR(1) item is of the form, [ A  w1 . w2, a] where a is a terminal.
Closure
• If I is a set of items for a grammar G, then the Closure(I) is the set of
items constructed from I by the following rules:
1. Every item in I is added to closure(I)
2. If [A α.Bβ , a] is in closure(I) and each production B  γ,
and each terminal b in FIRST (βa ) such that [B  .γ, b] is not
in I, do add [B  .γ, b] to I .
3. Repeat the step (2) until no more new items can be added to
closure(I)
Example: Given grammar G
S → CC (1)
C → cC (2)
C → d (3)
Augmented grammar G’
S’  S
S → CC
C → cC
C→d
Collection of Canonical LR(1) Sets of
Items
I0 : S’  .S , $ goto(I0 , C) = I2
S → .CC , $ I2 : S → C.C , $
C → .cC , c/d C →.cC , $
C → .d , c/d C →.d , $

goto(I0 , S) = I1 goto(I0 , c) = I3

I1 : S’  S. , $ I3 : C → c.C , c/d
C →.cC , c/d
goto(I0 , d) = I4 goto(I2 , d) = I7 goto(I3 , d) = I4
I4: C →d. , c/d I7: C →d. , $

goto(I2 , C) = I5 goto(I3 , C) = I8 goto(I6 , C) = I9

I5: S → CC. , $ I8: C → cC. , c/d I9: C → cC. , $

goto(I2 , c) = I6 goto(I3 , c) = I3 goto(I6 , c) = I6

I6: C → c.C , $ I3: C → c.C , c/d goto(I6 , d) = I7


C →.cC , $ C →.cC , c/d
C →.d , $ C →.d , c/d
S

C C

d
c C

c C

You might also like