Canonical Collection - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Canonical collection, The closure procedure, Look ahead symbol, Set of LR, Parser state, Eventual parser DFA, Parser intial state, Grammar and production, Core of state are the points from this lecture. You can find series of lecture notes for compiler construction here.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

asim.amjid
asim.amjid 🇵🇰

4.4

(47)

41 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes Set:2-1
1
L
Le
ec
ct
tu
ur
re
e
2
24
4
An LR(1) item is a pair [X αβ, a] where X αβ is a production and aT
(terminals) is look-ahead symbol. The model uses a set of LR(1) items to represent each
parser state. The model is called the canonical collection (
CC
) of set of LR(1) items.
Canonical Collection
Each set in
CC
represents a state in the eventual parser DFA. The construction of CC
begins by building a model of parser’s initial state. The initial state consists of the set of
LR(1) items that represent the parser’s initial state, along with any items that must also
hold in the initial state. To simplify the task of building this initial state, the construction
requires that the grammar have a unique goal symbol. The convention is to add a new
start symbol S to grammar and a production
S E
This leads to the augmented grammar
S E
E E + (E) | int
The Closure Procedure
The item [S E, $] describes the parser’s initial state. It represents a configuration in
which recognizing S followed by $ would be a valid parse. This item, i.e., [S E, $]
becomes the core of the first state in CC, labeled I0. If the grammar has several distinct
productions for the start symbol, each of them generates an item in this initial core of I0.
The procedure closure does this.
closure(s) =
repeat
for each [X αYβ, a] s
for each production Y α
for each b FIRST(βa)
s s [Y γ, b]
until s is unchanged
Let’s apply this procedure to the augmented grammar.
The first set is I0 = closure({[S E, $] }). Equating the terms in the procedure,
s = {[S E, $]}, [X α Yβ, a] [S E, $], X = S, α = ε, Y = E,
β = ε, a = $, Y γ E E + (E) and E intFIRST(βa) = FIRST($) = $.
pf2

Partial preview of the text

Download Canonical Collection - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:2-

Le Leccttuurree 2 244

An LR(1) item is a pair [X → α•β, a] where X → αβ is a production and a∈ T (terminals) is look-ahead symbol. The model uses a set of LR(1) items to represent each

parser state. The model is called the canonical collection ( CC ) of set of LR(1) items.

Canonical Collection

Each set in CC represents a state in the eventual parser DFA. The construction of CC

begins by building a model of parser’s initial state. The initial state consists of the set of LR(1) items that represent the parser’s initial state, along with any items that must also hold in the initial state. To simplify the task of building this initial state, the construction requires that the grammar have a unique goal symbol. The convention is to add a new start symbol S to grammar and a production

S → E

This leads to the augmented grammar

S → E E → E + (E) | int

The Closure Procedure

The item [S → • E, $] describes the parser’s initial state. It represents a configuration in which recognizing S followed by $ would be a valid parse. This item, i.e., [S → • E, $]

becomes the core of the first state in CC, labeled I 0. If the grammar has several distinct productions for the start symbol, each of them generates an item in this initial core of I 0. The procedure closure does this.

closure( s ) = repeat for each [X → α•Yβ, a] ∈ s for each production Y → α for each b ∈ FIRST(βa) ss ∪ [Y → • γ, b] until s is unchanged

Let’s apply this procedure to the augmented grammar.

The first set is I 0 = closure ({[S → • E, $] }). Equating the terms in the procedure, s = {[S → • E, $]}, [X → α • Yβ, a ] ⇔ [S → • E, $], X = S, α = ε, Y = E, β = ε, a = $, Y → γ E → E + (E) and E → int      FIRST(βa) = FIRST($) = $.

Sohail Aslam Compiler Construction Notes Set:2-

This leads to expansion of s.

s = { [S → • E, $] } ∪ { [E → • E + (E), $] } ∪ { [E → • int, $] } = { [S → • E, $] , [E → • E + (E), $] , [E → • int, $] }

The set s changed so we repeat. The item [S → • E, $] is already processed. The for loop considers [X → α•Yβ, a] ⇔ [E→•E+(E),$], which leads to the match up X = E, α = ε, Y = E, β = +(E), a = $, Y → γ E → E + (E), ⇔ E → int  FIRST(βa) = FIRST(+(E)$) = +. The set s is extended

s = s ∪ { [E → • E+(E), +] } ∪ { [E → • int, +] }