Items and Itemsets - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Items and Itemsets

Description:

Items and Itemsets An itemset is merely a set of items In LR parsing terminology an item Looks like a production with a . in it The . indicates how far ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 17
Provided by: JohnG293
Learn more at: https://www.cse.unt.edu
Category:

less

Transcript and Presenter's Notes

Title: Items and Itemsets


1
Items and Itemsets
  • An itemset is merely a set of items
  • In LR parsing terminology an item
  • Looks like a production with a . in it
  • The . indicates how far the parse has gone in
    recognizing a string that matches this production
  • e.g. A -gt aAb.BcC suggests that weve seen
    input that could replace aAb. If, by following
    the rules we get A -gt aAbBcC. we can reduce by A
    -gt aAbBcC

2
Building LR(0) Itemsets
  • Start with an augmented grammar if S is the
    grammar start symbol add S -gt S
  • The first set of items includes the closure of S
    -gt S
  • Itemset construction requires two functions
  • Closure
  • Goto

3
Closure of LR(0) Itemset
  • If J is a set of items for Grammar G, then
    closure(J) is the set of items constructed from G
    by two rules
  • 1) Each item in J is added to closure(J)
  • 2) If A ? a.Bß is in closure(J) and
  • B ? f is a production, add
  • B ? .f to closure(J)

4
Closure Example
Grammar A ? aBC A ? aA B ? bB B? bC C? cC C ? ?
Closure(J) A ? a.BC A-gt
a.A A ? .aBC A ? .aA B ? .bB B ? .bC
J A ? a.BC A? a.A
5
GoTo
  • Goto(J,X) where J is a set of items and X is a
    grammar symbol either terminal or non-terminal
    is defined to be closure of
  • A? aX.ß for A ? a.Xß in J
  • So, in English, Goto(J,X) is the closure of all
    items in J which have a . immediately preceding
    X

6
Set of Items Construction
  • Procedure items(G)
  • Begin
  • C closure(S ? .S)
  • repeat
  • for each set of items J in C and each
  • grammar symbol X such that GoTo(J,X)
  • is not empty and not in C do
  • add GoTo(J,X) to C
  • until no more sets of items can be added to C

7
Build LR(0) Itemsets for
  • S ? (S), S ? ?
  • S ? (S), S ? SS, S ? ?

8
Building LR(0) Table from Itemsets
  • One row for each Itemset
  • One column for each terminal or non-terminal
    symbol, and one for
  • Table JX is
  • Rn if J includes A ? rhs., A ? rhs is rule
    number n, and X is a terminal
  • Sn if Goto(J,X) is itemset n

9
LR(0) Parse Table for
  • S ? (S), S ? ?
  • S ? (S), S ? SS, S ? ?

10
Building SLR Table from Itemsets
  • One row for each Itemset
  • One column for each terminal or non-terminal
    symbol, and one for
  • Table JX is
  • Rn if J includes A ? rhs., A ? rhs is rule
    number n, X is a terminal, AND
  • X is in Follow(A)
  • Sn if Goto(J,X) is itemset n

11
LR(0) and LR(1) Items
  • LR(0) item is a production with a . in it.
  • LR(1) item has a kernel that looks like LR(0),
    but also has a lookahead e.g.
  • A ? a.Xß, terminals
  • A ? a.Xß, a/b/c ? A ? a.Xß, a/b/d

12
Closure of LR(1) Itemset
  • If J is a set of LR(1) items for Grammar G, then
    closure(J) includes
  • 1) Each LR(1) item in J
  • 2) If A ? a.Bß, a in closure(J) and
  • B ? f is a production, add
  • B ? .f, First(ß,a) to closure(J)

13
LR(1) Itemset Construction
  • Procedure items(G)
  • Begin
  • C closure(S ? .S, )
  • repeat
  • for each set of items J in C and each
  • grammar symbol X such that GoTo(J,X)
  • is not empty and not in C do
  • add GoTo(J,X) to C
  • until no more sets of items can be added to C

14
Build LR(1) Itemsets for
  • S ? (S), S ? SS, S ? ?

15
S ? CC, S ? cC, C ?d
  • Is this grammar
  • LR(0)?
  • SLR?
  • LR(1)?
  • How can we tell?

16
LR(1) Table from LR(1) Itemsets
  • One row for each Itemset
  • One column for each terminal or non-terminal
    symbol, and one for
  • Table JX is
  • Rn if J includes A ? rhs., a A ? rhs is rule
    number n X a
  • Sn if Goto(J,X) in LR(1) itemset n
Write a Comment
User Comments (0)
About PowerShow.com