4.7.5 efficient construction of LALR parsing Tables

Source: Internet
Author: User

4.7.5 efficient construction of LALR parsing Tables

There is several modifications we can make to algorithm 4.59 to avoid constructing the full collection of sets of LR (1) I TEMs in the process of creating an LALR (1) parsing table.

    • First, we can represent any set of LR (0) or LR (1) Items I by it kernel, that's, by those items, which is either the Initi AL Item--[s ' →@s] or [s ' →@s,$]--or that has the dot somewhere other than at the beginning of the production body.

    • We can construct the LALR (1)-item kernels from the LR (0)-item kernels by a process of propagation and spontaneous Generati On the lookaheads, that we shall describe shortly.

    • If we have the LALR (1) kernels, we can generate the LALR (1) parsing table by closing each kernel, using the function Closu RE of Fig. 4.40, and then computing table entries by algorithm 4.56, as if the LALR (1) sets of items were canonical LR (1) Sets of items.

Example 4.61: We shall use as a example of the efficient LALR (1) Table-construction method The NON-SLR grammar from Example 4.48, which We reproduce below in its augmented form:

S ' →s

S→l = R | R

L→*r | Id

R→l

The complete sets of LR (0) items for this grammar were shown in Fig. 4.39. The kernels of these items is shown in Fig. 4.44.

I0:

S ' →@s

I5:

L→[email protected]

I1:

S ' →[email protected]

I6:

S→l = @R

I2:

S→[email protected] = R

R→[email protected]

I7:

L→*[email protected]

I3:

S→[email protected]

I8:

R→[email protected]

I4:

L→*@r

I9:

s→l = [email protected]

Figure 4.44:kernels of the sets of LR (0) Items for grammar (4.49)

-

Now we must attach the proper lookaheads to the LR (0) items in the kernels, to create the kernels of the sets of LALR (1) I TEMs. There is ways a lookahead B can get attached to an LR (0) Item B→γ@δin Some set of LALR (1) Items J:

1. There is a set of items I, with a kernel item a→α@β, a, and J=goto (I, X), and the construction of

GOTO (CLOSURE ({[A→α@β, A]}), X)

As given in Fig. 4.40, contains [B→γ@δ, B], regardless of a. Such A lookahead B is said to being generated spontaneously for b→γ@δ. As a special case, lookahead $ was generated spontaneously for the item S ' →@s in the initial set of items.

2. All are as in (1), but a = B, and GOTO (CLOSURE ({[A→α@β, A]}), X), as given in Fig. 4.40, contains [b→γ@δ, b] only Becaus E A→α@βhas B as one of its associated lookaheads. In such a case, we say this lookaheads propagate from a→α@βin the kernel of I to b→γ@δin the kernel of J. Note that prop Agation does not depend on the particular lookahead symbol; Either all lookaheads propagate from one item to another, or none.

We need to determine the spontaneously generated lookaheads for each set of LR (0) items, and also to determine which items Propagate lookaheads from which. The test is actually quite simple. Let # is a symbol not in the grammar at hand. Let A→α@βbe A kernel LR (0) of item in Set I. Compute, for each X, J = GOTO (CLOSURE ({[A→α@β, #]}), X).

For each kernel item in J, we examine its set of lookaheads. If # is a lookahead, then lookaheads propagate to that item from a→α@β. Any and lookahead is spontaneously generated. These ideas is made precise in the following algorithm, which also makes use of the fact, the only kernel items in J Must has X immediately to the left of the dot; That's, they must be of the form B→γ[email protected]δ.

algorithm 4.62: Determining Lookaheads.

Input:the kernel K of a set of LR (0) Items I and a grammar symbol X.

Output:the lookaheads spontaneously generated by items in I for kernel items in GOTO (I, X) and the items in I from which Lookaheads is propagated to kernel items in GOTO (I, X).

Method:the algorithm is given in Fig. 4.45. -

for (each item a→α@βin K) {

J: = CLOSURE ({[A→α@β, #]});

if ([B→γ[email protected]δ, A] is in J, and A are not #)

Conclude that lookahead A is generated spontaneously for item

B→γ[email protected]δin GOTO (I, X);

if ([B→γ[email protected]δ, #] is in J)

Conclude that lookaheads propagate from a→α@β in I to

B→γ@δin GOTO (I, X);

}

Figure 4.45:discovering propagated and spontaneous lookaheads

We is now attach lookaheads to the kernels of the sets of LR (0) items to form the sets of LALR (1) items. First, we know that $ was a lookahead for S 0→s in the initial set of LR (0) items. Algorithm 4.62 gives us all the lookaheads generated spontaneously. After listing all those lookaheads, we must allow them to propagate until no further propagation are possible. There is many different approaches, all of which on some sense keep track of "new" lookaheads that has propagated into a n item but which has not yet propagated out. The next algorithm describes one technique to propagate lookaheads to all items.

algorithm 4.63: Efficient computation of the kernels of the LALR (1) collection of sets of items.

Input:an augmented Grammar G '.

Output:the Kernels of the LALR (1) collection of sets of items for G '.

METHOD:

1. Construct the kernels of the sets of LR (0) items for G. If space is not at a premium, the simplest-to-construct the LR (0) sets of items, as in section 4.6.2, and then Remo ve the Nonkernel items. If space is severely constrained, we could wish instead to store only the kernel items for each set, and compute GOTO for a Set of items I by first computing the closure of I.

2. Apply algorithm 4.62 to the kernel of each set of LR (0) Items and grammar symbol X to determine which lookaheads is SP Ontaneously generated for kernel items in GOTO (I, X), and from which items in I lookaheads is propagated to kernel items In GOTO (I, X).

3. Initialize a table that gives, for each kernel item in each set of items, the associated lookaheads. Initially, each item have associated with it only those lookaheads that we determined in step (2) were generated Spontaneou Sly.

4. Make repeated passes through the kernel items in all sets. When we visit a item I, we lo ok up the kernel items to which I propagates it lookaheads, using information tabulated in Step (2). The current set of lookaheads for I am added to those already associated with each of the items to which I propagates its Lookaheads. We continue making passes over the kernel items until no more new lookaheads is propagated.

-

Example 4.64: Let us construct the kernels of the LALR (1) items for the grammar of Example 4.61. The kernels of the LR (0) items were shown in Fig. 4.44. When we apply algorithm 4.62 to the kernel of set of items I0, we first compute CLOSURE ({[S ' →@s, #]}), which is

S ' →@s, #

S→@l = R, #

S→@r, #

L→@*r, #/=

l→ @id, #/=

R→@l, #

Among the items in the closure, we see both where the lookahead = has been generated spontaneously. The first of these is l→@*r. This item, with the-right of the dot, gives rise to [l→@*r, =]. That is, = are a spontaneously generated lookahead for l→@*r, which are in set of items I4. Similarly, [l→ @id, =] tells us that = was a spontaneously generated lookahead for L→[email protected] in I5.

As # is a lookahead for all six items on the closure, we determine that the item S ' →@s in I0 propagates lookaheads to the Following six items:

S ' →[email protected] in I1

S→[email protected] = R in I2

S→[email protected] in I3

L→*@r in I4

L→[email protected] in I5

R→[email protected] in I2

From

To

I0:s ' →@s

I1:s ' →[email protected]

I2:s→[email protected] = R

I2:r→[email protected]

I3:s→[email protected]

I4:l→*@r

I5:l→[email protected]

I2:s→[email protected] = R

I6:s→l = @R

I4:l→*@r

I4:l→*@r

I5:l→[email protected]

I7:l→*[email protected]

I8:r→[email protected]

I6:s→l = @R

I4:l→*@r

I5:l→[email protected]

I8:r→[email protected]

i9:s→l = [email protected]

Figure 4.46:propagation of Lookaheads

In Fig. 4.47, we show Steps (3) and (4) of algorithm 4.63. The column labeled INIT shows the spontaneously generated lookaheads for each kernel item. These is only the occurrences of = discussed earlier, and the spontaneous lookahead $ for the initial item S ' →@s.

On the first pass, the lookahead $ propagates from S ' →@s in I0 to the six items listed in Fig. 4.46. The lookahead = propagates from l→*@r on I4 to items L→*[email protected] in I7 and R→[email protected] in I8. It also propagates to itself and to l→[email protected] in I5, but these lookaheads is already present. In the second and third passes, the only new lookahead propagated are $, discovered for the successors of I2 and I4 on pass 2 and for the successor of I6 on Pass 3. No new lookaheads is propagated on pass 4, so the final set of Lookaheads was shown in the rightmost column of Fig. 4.47.

Note that the shift/reduce conflict found in Example 4.48 using the SLR method have disappeared with the LALR technique. The reason is this only lookahead $ are associated with R→[email protected] in I2, so there are no conflict with the parsing Action of shift on = generated by item S→[email protected]=r in I2.

-

SET

ITEM

Lookaheads

Init

PASS 1

PASS 2

PASS 3

I0:

S ' →@s

$

$

$

$

I1:

S ' →[email protected]

$

$

$

I2:

S→[email protected] = R

$

$

$

R→[email protected]

$

$

$

I3:

S→[email protected]

$

$

$

I4:

L→*@r

=

=/$

=/$

=/$

I5:

L→[email protected]

=

=/$

=/$

=/$

I6:

S→l = @R

$

$

I7:

L→*[email protected]

=

=/$

=/$

I8:

R→[email protected]

=

=/$

=/$

I9:

s→l = [email protected]

$

Figure 4.47:computation of Lookaheads

4.7.5 efficient construction of LALR parsing Tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.