160 likes | 237 Views
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 N D
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
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
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)
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
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
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
Build LR(0) Itemsets for: • {S (S), S λ} • {S (S), S SS, S λ}
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
LR(0) Parse Table for: • {S (S), S λ} • {S (S), S SS, S λ}
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
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
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)
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
Build LR(1) Itemsets for: • {S (S), S SS, S λ}
{S CC, S cC, C d} Is this grammar • LR(0)? • SLR? • LR(1)? How can we tell?
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