CS118 Programming Languages

Lecture 16


Contents

  1. Review of shift/reduce
  2. Set of all parse stacks
  3. The LR(0) machine

Review of shift/reduce

The problem is parsing text according to a cfg; an answer is LR(k). The first step is a closer look at parsing.

A 10-rule cfg for propositions:

P
  O eol         r0
O
  O | A         r1
  A             r2
A
  A & N         r3
  N             r4
N
  ~ B           r5
  B             r6
B
  t             r7
  f             r8
  ( O )         r9

An input text (to be parsed):

t&(t|f)

A shift/reduce sequence for the input:

action stack input
start t&(t|f)
shift t &(t|f)
r7 B &(t|f)
r6 N &(t|f)
r4 A &(t|f)
shift A& (t|f)
shift A&( t|f)
shift A&(t |f)
r7 A&(B |f)
r6 A&(N |f)
r4 A&(A |f)
r2 A&(O |f)
shift A&(O| f)
shift A&(O|f )
r8 A&(O|B )
r6 A&(O|N )
r4 A&(O|A )
r1 A&(O )
shift A&(O)
r9 A&B
r6 A&N
r3 A
r2 O
r0 P

The set of all reducible parse stacks is a language

(See Worcester talk)

Some elementary automata theory:

A FA (finite automaton) has rules of the form:

If you see X in state S1, eat the X and goto state S2.

There is an initial state. There are one or more final states. If, obeying the rules of a FA, you eat all the input and end up in a final state, the input is accepted.

A DFA (deterministic FA) is a FA where each step either succeeds or fails (no ambiguity).

An NFA is a FA that is not deterministic (more than one correct choice at some point).

There is an algorithm that turns an NFA into an equivalent DFA.

Example of a regular expression and corresponding DFA

(see book)

The LR(0) machine

Given the grammar for x+ eof

    G -> E eof
    E -> T
    T -> T x
    T -> x
  1. compute the LR(0) machine
  2. extend to SLR(1) machine
    • using follow sets from the grammar
    • by back-n-forward-one in the LR(0) machine
  3. use the SLR(1) machine on several examples

Do the LR(0) computation for P (grammar above) (appoint a FA scribe -- he/she does what the others say to do)

Now you know what yacc does.

LR.ppt


go to:

CS118
Home Page
CS118
Summary
Previous
Lecture
Next
Lecture