Download presentation
Presentation is loading. Please wait.
Published byTapani Alanen Modified over 5 years ago
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 -> aAb.BcC suggests that we’ve “seen” input that could replace aAb. If, by following the rules we get A -> aAbBcC. we can reduce by A -> aAbBcC
2
Building LR(0) Itemsets
Start with an augmented grammar; if S is the grammar start symbol add S’ -> S The first set of items includes the closure of S’ -> 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 α.Bβ is in closure(J) and B φ is a production, add B .φ to closure(J)
4
Closure Example Grammar: A aBC A aA B bB B bC C cC C λ
Closure(J) A a.BC A-> 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 αX.β for 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 [J][X] 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 [J][X] 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 α.Xβ, {terminals} A α.Xβ, a/b/c ≠ 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 α.Bβ, a in closure(J) and B φ is a production, add B .φ, 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 [J][X] 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.