4.7.6 compaction of LR parsing Tables

Source: Internet
Author: User

4.7.6 compaction of LR parsing Tables

A typical programming language grammar with the terminals and the productions may has an LALR parsing table with SE Veral hundred states. The action function may easily has 20,000 entries, each requiring at least 8 bits to encode. On small devices, a to efficient encoding than a two-dimensional array may be important. We shall mention briefly a few techniques that has been used to compress the ACTION and GOTO fields of an LR parsing tabl E.

One useful technique for compacting the action field was to recognize, usually many rows of the action table is IDE Ntical. For example, in Fig. 4.42, states 0 and 3 has identical action entries, and so does 2 and 6. We can therefore save considerable space, at little cost in time, if we create a pointer for each state into a one-dimensi Onal Array. Pointers for states and the same actions point to the same location. To access information from this array, we assign each terminal a number from zero to one less than the number of terminals , and we use this integer as a offset from the pointer value to each state. In a-given state, the parsing action for the ith terminal would be found I locations past the pointer value for this state.

Further space efficiency can be achieved on the expense of a somewhat slower parser by creating a list for the actions of Each state. The list consists of (Terminal-symbol, action) pairs. The most frequent action is placed at the end of the list, and in place of a terminal we can use the Notat Ion "Any," meaning that if the current input symbol have not been found so far on the list, we should does that action no mat ter What's the input is. Moreover, error entries can safely is replaced by reduce actions, for further uniformity along a row. The errors'll be detected later, before a shift move.

Example 4.65:consider The parsing table of Fig. 4.37. First, note that the actions for states 0, 4, 6, and 7 agree. We can represent them all by the list

SYMBOL

ACTION

Id

S5

(

S4

Any

Error

State 1 have a similar list:

SYMBOL

ACTION

+

S6

$

Acc

Any

Error

In state 2, we can replace the error entries by R2, so reduction by production 2 would occur on any input but *. Thus the list for state 2 is

SYMBOL

ACTION

*

S7

Any

R2

State 3 have only error and R4 entries. We can replace the former by the latter, so the list is 3 consists of only the pair (any, R4). States 5, ten, and one can be treated similarly. The list for state 8 is

SYMBOL

ACTION

+

S6

)

S11

Any

Error

and for State 9

SYMBOL

ACTION

*

S7

Any

R1

-

We can also encode the GOTO table by a list, but here it apps ears more efficient to make a list of pairs for each nontermi NAL A. Each pair on the list for A is of the form (currentstate, nextstate), indicating

GOTO [CurrentState, A] = nextstate

This technique was useful because there tend to being rather few states in any one column of the GOTO table. The reason is and the GOTO on nonterminal A can are only a state derivable from a set of items in which some items has a Immediately to the left of a dot. No set has items with X and Y immediately to the left of a dot if x≠y. Thus, each of the state apps ears in the most one GOTO column.

For more space reduction, we note that the error entries in the Goto table is never consulted. We can therefore replace each error entry by the very common Non-error entry in its column. This entry becomes the default; It is a represented in the list for each column by one pair with an all in place of CurrentState.

Example 4.66:consider Fig. 4.37 again. The column for F have entry for state 7, and all other entries is either 3 or error. We may replace error by 3 and create for column F the list

CurrentState

Nextstate

7

10

Any

3

Similarly, a suitable list for column T is

CurrentState

Nextstate

6

9

Any

2

For column E We could choose either 1 or 8 to be the default; The entries is necessary in the either case. For example, we might create for column E the list

CurrentState

Nextstate

4

8

Any

1

-

This space savings in these small examples is misleading, because the total number of entries in the lists created in This example and the previous one together with the pointers from States to action lists and from Nonterminals to Next-sta TE lists, result in unimpressive space savings over the matrix implementation of Fig. 4.37. For practical grammars, the space needed for the list representation are typically less than ten percent of that needed for The matrix representation. The table-compression methods for finite automata, were discussed in section 3.9.8 can also being used to represent LR PA rsing tables.

4.7.6 compaction of LR 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.