# Class 14: Parsing, concluded

Held Wednesday, September 30, 1998

Notes

• Don't forget that assignment 6 is due today at 5pm. I'll be available after class for questions.
• Those of you who need a free extension until next Wednesday may have that extension.
• Extra credit for those who turn it in by class time on Friday.
• Your answers to assignment 4 suggested that English is a particularly bad language for describing languages.
• ``Strings of a,b,c with first a before first b'' -- What happens if there are no a's?
• ``Binary numbers that are multiples of 4'' -- Can binary numbers start with 0? Is 0 a multiple of 4?
• ``Numbers with an integer solution of an + bn = cn'' -- Can a,b, and c be 0? Negative?

## LR parsing, concluded

### Construction of LR(0) automata, revisited

• Recall that we build LR(0) automata using the following guidelines
• A state in an LR(0) automaton is a collection of LR(0) items
• An LR(0) item is a production augmented by a ``position marker'' that indicates where in the production we might be.
• The grammar is augmented with a new start state, S', and production,
```  S' ::= S \$
```
• The initial state is the closure of `S' ::= . S \$`
• The closure of a state fills in the other possible items. For example, if we're waiting to see an N (indicated by a position marker before N in some item), then we need to include all the items for ``how to parse an N, given that you've seen nothing of the right-hand side''.
• The edge labels of the automaton are given by the nonterminals and terminals of the grammar.
• The edges of the automaton correspond to seeing one more symbol in the right-hand sides. After moving the position marker appropriately, one computes the closure of the new items.
• The final states are the states in which the position marker has reached the end of the right-hand side.
• When reaching a final state in some contexts, one does the reduction given by the final state, returns to the state in effect when we started matching the right-hand side, and restarts the automaton.
• A stack keeps track of the state history.

### Conflicts in LR automata

• At times, there are conflicts in LR automata. What kinds of conflicts?
• A state may include a ``final'' item (one in which the position marker is at the end) and a nonfinal item.
• This is called a shift-reduce conflict
• A state may include two different ``final'' items.
• This is called a reduce-reduce conflict
• Can we have reduce-reduce conflicts in unambiguous grammars?
• Can we have a shift-shift conflict?

### SLR Automata

• You may have noted (e.g., from our example yesterday) that LR(0) automata can be overly aggressive in choosing to reduce.
• Such automata typically reduce whenever we've reached the end of a right-hand side.
• SLR automata only reduce when the next token is in `Follow` of the left-hand side of the reduction.

### LR(1) Automata

• LR(1) automata require a more complicated construction process, one that involves lookahead.
• In effect, instead of using the Follow table (as SLR automata do), LR(1) automata build more specific follow tables that correspond to the possible follow symbols according to a particular context.
• Each LR(1) item contains not just an augmented production, but also a token that can follow the nonterminal when we've reached the current state.
• The tokens are inserted by the closure routine.
• If we have N ::= alpha . M beta then when we insert the M items, we indicate that each of them can be followed by the tokens in first(beta)
• If beta is nullable, then the M items can also be followed by whatever can follow N (in the given LR(1) item).
• See the book and the errata sheets for more details.

History

• Created Monday, September 28, 1998 (no content).
• Selected text on SLR automata copied from the previous outline and was added on Wednesday, September 30, 1998.
• The attribute grammar for expressions is slightly modified from one I wrote for CS302 last semester. It was added on Wednesday, September 30, 1998. The original was developed on our near May 7, 1998.
• On Friday, October 2, I moved all of the attribute materials to the next outline.

Back to Shift-reduce parsing. On to Semantic actions.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Fri Oct 2 09:39:51 1998.

This page generated on Fri Oct 2 09:40:43 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu