items and itemsets

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

Upload: dahlia

Post on 08-Jan-2016

23 views

Category:

Documents


2 download

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 the parse has gone in recognizing a string that matches this production - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Items and Itemsets

11

Items and ItemsetsItems and Itemsets

An itemset is merely a set of itemsAn itemset is merely a set of items In LR parsing terminology an itemIn LR parsing terminology an item

Looks like a production with a ‘.’ in itLooks like a production with a ‘.’ in it The ‘.’ indicates how far the parse has The ‘.’ indicates how far the parse has

gone in recognizing a string that matches gone in recognizing a string that matches this productionthis production

e.g. A -> aAb.BcC suggests that we’ve e.g. A -> aAb.BcC suggests that we’ve “seen” input that could replace aAb. If, “seen” input that could replace aAb. If, by following the rules we get A -> by following the rules we get A -> aAbBcC. we can reduce by A -> aAbBcCaAbBcC. we can reduce by A -> aAbBcC

Page 2: Items and Itemsets

22

Building LR(0) ItemsetsBuilding LR(0) Itemsets

Start with an augmented grammar; if S Start with an augmented grammar; if S is the grammar start symbol add S’ -> Sis the grammar start symbol add S’ -> S

The first set of items includes the The first set of items includes the closure of S’ -> Sclosure of S’ -> S

Itemset construction requires two Itemset construction requires two functionsfunctions ClosureClosure GotoGoto

Page 3: Items and Itemsets

33

Closure of LR(0) ItemsetClosure of LR(0) Itemset

If J is a set of items for Grammar G, If J is a set of items for Grammar G, then closure(J) is the set of items then closure(J) is the set of items constructed from G by two rulesconstructed from G by two rules

1) Each item in J is added to 1) Each item in J is added to closure(J)closure(J)

2) If A 2) If A αα.B.Bββ is in closure(J) and is in closure(J) and

B B φφ is a production, add is a production, add

B B . .φφ to closure(J) to closure(J)

Page 4: Items and Itemsets

44

Closure ExampleClosure ExampleGrammar:A aBCA aAB bBB bCC cCC λ

J

A a.BCA a.A

Closure(J)

A a.BCA-> a.AA .aBCA .aAB .bBB .bC

Page 5: Items and Itemsets

55

GoToGoTo

Goto(J,X) where J is a set of items Goto(J,X) where J is a set of items and X is a grammar symbol – either and X is a grammar symbol – either terminal or non-terminal is defined terminal or non-terminal is defined to be closure of to be closure of AA ααX.X.ββ for A for A αα.X.Xββ in J in J

So, in English, Goto(J,X) is the So, in English, Goto(J,X) is the closure of all items in J which have a closure of all items in J which have a ‘.’ immediately preceding X‘.’ immediately preceding X

Page 6: Items and Itemsets

66

Set of Items ConstructionSet of Items Construction

Procedure items(G’)Procedure items(G’)BeginBegin C = {closure({[S’ C = {closure({[S’ .S]})} .S]})} repeatrepeat for each set of items J in C and each for each set of items J in C and each grammar symbol X such that GoTo(J,X) grammar symbol X such that GoTo(J,X) is not empty and not in C dois not empty and not in C do add GoTo(J,X) to Cadd GoTo(J,X) to C until no more sets of items can be added to until no more sets of items can be added to CC

Page 7: Items and Itemsets

77

Build LR(0) Itemsets for:Build LR(0) Itemsets for:

{S {S (S), S (S), S λλ}} {S {S (S), S (S), S SS, S SS, S λλ}}

Page 8: Items and Itemsets

88

Building LR(0) Table from Building LR(0) Table from ItemsetsItemsets

One row for each ItemsetOne row for each Itemset One column for each terminal or non-One column for each terminal or non-

terminal symbol, and one for $terminal symbol, and one for $ Table [J][X] is:Table [J][X] is:

Rn if J includes A Rn if J includes A rhs., A rhs., A rhs is rule rhs is rule number n, and X is a terminalnumber n, and X is a terminal

Sn if Goto(J,X) is itemset nSn if Goto(J,X) is itemset n

Page 9: Items and Itemsets

99

LR(0) Parse Table for:LR(0) Parse Table for:

{S {S (S), S (S), S λλ}} {S {S (S), S (S), S SS, S SS, S λλ}}

Page 10: Items and Itemsets

1010

Building SLR Table from Building SLR Table from ItemsetsItemsets

One row for each ItemsetOne row for each Itemset One column for each terminal or non-One column for each terminal or non-

terminal symbol, and one for $terminal symbol, and one for $ Table [J][X] is:Table [J][X] is:

Rn if J includes A Rn if J includes A rhs., A rhs., A rhs is rule rhs is rule number n, X is a terminal, number n, X is a terminal, ANDAND

X is in Follow(A)X is in Follow(A) Sn if Goto(J,X) is itemset nSn if Goto(J,X) is itemset n

Page 11: Items and Itemsets

1111

LR(0) and LR(1) ItemsLR(0) and LR(1) Items

LR(0) item “is” a production with a ‘.’ in LR(0) item “is” a production with a ‘.’ in it.it.

LR(1) item has a “kernel” that looks like LR(1) item has a “kernel” that looks like LR(0), but also has a “lookahead” – e.g.LR(0), but also has a “lookahead” – e.g.

A A αα.X.Xββ, {terminals}, {terminals} A A αα.X.Xββ, a/b/c ≠ A , a/b/c ≠ A αα.X.Xββ, a/b/d, a/b/d

Page 12: Items and Itemsets

1212

Closure of LR(1) ItemsetClosure of LR(1) Itemset

If J is a set of LR(1) items for If J is a set of LR(1) items for Grammar G, then closure(J) includesGrammar G, then closure(J) includes

1) Each LR(1) item in J 1) Each LR(1) item in J

2) If A 2) If A αα.B.Bββ, a, a in closure(J) and in closure(J) and

B B φφ is a production, add is a production, add

B B . .φφ, First(, First(ββ,a),a) to closure(J) to closure(J)

Page 13: Items and Itemsets

1313

LR(1) Itemset ConstructionLR(1) Itemset Construction

Procedure items(G’)Procedure items(G’)BeginBegin C = {closure({[S’ C = {closure({[S’ .S, .S, $$]})}]})} repeatrepeat for each set of items J in C and each for each set of items J in C and each grammar symbol X such that GoTo(J,X) grammar symbol X such that GoTo(J,X) is not empty and not in C dois not empty and not in C do add GoTo(J,X) to Cadd GoTo(J,X) to C until no more sets of items can be added to until no more sets of items can be added to CC

Page 14: Items and Itemsets

1414

Build LR(1) Itemsets for:Build LR(1) Itemsets for:

{S {S (S), S (S), S SS, S SS, S λλ}}

Page 15: Items and Itemsets

1515

{S {S CC, S CC, S cC, C cC, C d}d}

Is this grammar Is this grammar LR(0)? LR(0)? SLR? SLR? LR(1)?LR(1)?

How can we tell?How can we tell?

Page 16: Items and Itemsets

1616

LR(1) Table from LR(1) LR(1) Table from LR(1) ItemsetsItemsets

One row for each ItemsetOne row for each Itemset One column for each terminal or non-One column for each terminal or non-

terminal symbol, and one for $terminal symbol, and one for $ Table [J][X] is:Table [J][X] is:

Rn if J includes A Rn if J includes A rhs. rhs., a, a; A ; A rhs is rhs is rule number n; rule number n; X = aX = a

Sn if Goto(J,X) in LR(1) itemset nSn if Goto(J,X) in LR(1) itemset n