CSCE 434 Lecture 21

From Notes
Jump to navigation Jump to search

« previous | Friday, October 11, 2013 | next »

Lecture Slides

Exam on Friday will cover Chapters 1-4
Work on Gradience Homeworks for Practice!

Resolving Parse Conflicts

Happens when certain LR items are found in the same state

Parser may choose between LR items using lookahead; resolves such conflicts

  Shift-Reduce

Reduce-Reduce

LR(0) conflict conflict
SLR(1)
LALR(1)


Example

LALR(1) Parsing

Same number of states as SLR(1) (fewer than LR(1)) but have more power because of lookahead.

core of set of LR(1) items:

If two sets of LR(1) items, and have the same core, we can merge the states that represent them in the ACTION and GOTO tables

Approach 1

Build LR(1) sets and merge


LALR(1) Table Construction

More space-efficient

LALR derived from LR with no shift-reduce conflict will also have no shift-reduce conflict

LALR may create reduce-reduce conflicts not in LR from which LALR is derived. Face-sad.svg Perhaps combined states really weren't that identical.

Order of inclusiveness from low power to high power

  1. SLR(1)
  2. LALR(1)
  3. LR(1)
  4. LR(k)

However, all recognize the same language

Precedence

An approach to solve S/R conflicts is to use operator precedence:

Given , there are three possible precedence relations between and :

  1. in handle, but not ()
  2. Both in handle ()
  3. in handle, but not ()

We want operators of higher precedence to be resolved (reduced) first: e.g. addition should be reduced after multiplication


Review

Hand-coded recursive descent

LL(k): able to recognize production rule after seeing only the first symbols

LR(k):


Lecture Slides


Associativity

Left-recursion is left-associativity (natural for left-to-right human languages) Right-recursion is right-associativity

However, we have discovered that left and right recursion have specific applications:

  • Left-associative for bottom-up
  • Right-associative for top-down


Error Recovery

  • For each non-terminal, construct a set of terminals on which the parser can synchronize
  • When an error occurs when looking for , scan until an element of is found, then pop and continue.

What is SYNCH?

  1. If is in , then put in .