1 / 24

LR Parsing – The Tables

LR Parsing – The Tables. Lecture 11 Wed, Feb 16, 2005. The LR(0) Parsing Tables. There are two tables that we will construct The action table Contains shift and reduce actions to be taken upon processing terminals. The goto table Contains changes of state upon matching productions.

mpoole
Télécharger la présentation

LR Parsing – The Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005

  2. The LR(0) Parsing Tables • There are two tables that we will construct • The action table • Contains shift and reduce actions to be taken upon processing terminals. • The goto table • Contains changes of state upon matching productions.

  3. The Action Table • The action table contains one row for each state in the PDA and one column for each terminal and EOF ($). • The entries are • Shift n • Push the current token and move to state n. • Reduce n • Pop the symbols of the right side of production n and then push the nonterminal of the left side. • Then change state according to the goto table.

  4. Building the Action Table • The action table is built from items of the form [A  a] and [A  ]. • Items [A  a] produce shift operations. • Items [A  ] produce reduce operations. • The goto table is built from items of the form [A  B].

  5. a Ii Ij Building the Action Table • If [A  a] is in state Ii and the PDA transition on a is from state Ii to state Ij, then set the (i, a) entry to “shift j.” • In the table, we write “sj,” where j is the number of the destination state. • Thus, every transition on a terminal becomes a shift entry in the action table.

  6. Building the Action Table • If [A ] is in state Ii and AS', then set the (i, a) entry to “reduce A” for all a in FOLLOW(A). • In the table, we write “rk,” where k is the number of the production A. • If [S'S ] is in state Ii, then the (i, $) entry is “accept.” • In the table, we write “acc”.

  7. Building the Action Table • Thus, each item with the  at the end becomes a reduce entry in the action table. • All empty cells are labeled “error.”

  8. Shift/Reduce Conflicts • It is possible that a cell will contain both a shift operation and a reduce operation. • This is called a shift/reduce conflict. • To choose between “shift” and “reduce,” each case must be considered on its own merit. • Consider the case of EE + E | E * E and the inputs a + b * c and a * b + c.

  9. Reduce/Reduce Conflicts • It is possible that a cell will contain two different reduce operations. • This is called a reduce/reduce conflict. • This occurs when a sequence of tokens matches the right-hand sides of two different productions at the same time. • For each such conflict in the table, we must choose which reduction to apply.

  10. Example: FIRST and FOLLOW • To find the action table for our example, we need to know nullability, FIRST, and FOLLOW. E'E EE + T | T TT * F | F F (E) | id | num

  11. Example: FIRST and FOLLOW

  12. Example: The Action Table • Then number the productions, starting with 0 for the special production: 0. E' E 1. E  E + T 2. E  T 3. T  T * F 4. T  F 5. F  (E) 6. F  id 7. F  num

  13. Example: The Action Table

  14. The Goto Table • The goto table has one row for each state in the PDA and one column for each nonterminal except S'. • The entries are states of the PDA.

  15. B Ii Ij Building the Goto Table • If [A  B] is in state Ii and the PDA transition is then set the (i, B) entry to “goto j.” • In the goto table, we write “j.” • Thus, every transition on a nonterminal becomes an entry in the goto table.

  16. Example: The Goto Table

  17. Example: LR Parsing • Parse (id + num)*id.

  18. Example: LR Parsing

  19. Example: A Simplified Grammar • We may simplify our grammar to • EE + E • EE * E • E (E) • Eid • Enum • In this form, the precedence rules for + and * are not implicit. • They must be incorporated into the tables.

  20. The Action and Goto Tables

  21. Shift/Reduce Conflicts and Associativity • The shift/reduce conflict in cell (8, +) is between shifting a + and reducing by EE + E • If we choose “shift,” then we will make addition right associative. • If we choose “reduce,” then we will make addition left associative. • The case is similar in cell (9, *) regarding multiplication.

  22. Shift/Reduce Conflicts and Precedence • The shift/reduce conflict in cell (9, +) is between shifting a + and reducing by EE * E • If we choose “shift,” then we will give multiplication a higher precedence than addition. • If we choose “reduce,” then we will give addition a higher precedence than multiplication. • The case is similar in cell (8, *).

  23. The Action and Goto Tables

  24. Exercise • The grammar RRR | RR | R* | (R) | a | b generates all regular expressions on the alphabet {a, b}. • Determine nullability, FIRST, and FOLLOW for this grammar. • Find the items Ii. • Build the action and goto tables. • Parse the expression ab*(a | ).

More Related