Docsity

Shift-Reduce Parsing - Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Shift-Reduce Parsing - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Lecture #8, Feb. 7, 2007

  • Shift-reduce parsing,
  • Shift-reduce and reduce-reduce conflicts,
  • Precedence parsing,
  • LR parsers,
  • LR parsers and FSAs,
  • LR parsing Tables,
  • Sets of Items construction.

Bottom Up Parsing

Shift-Reduce parsing

  • data structures: input-string and stack.
  • look at symbols on top of stack, and the input-string and decide:
    • shift (move first input to stack)
    • reduce (replace top n symbols on stack by a non-terminal)
    • accept (declare victory)
    • error (be gracious in defeat)

Problems (ambiguous grammars)

  1. shift reduce conflicts: stack Input Action

x + y + z?

stack Input Action if x t if y t s2 e s3?

  1. reduce reduce conflicts:

suppose both procedure call and array reference have similar syntax:

  • x(2) := 6
  • f(x)

stack Input Action

id ( id ) id?

Should id reduce to a parameter or an expression. Depends on whether the bottom most id is an array or a procedure.

Using ambiguity to your advantage

  • Shift-Reduce and Reduce-Reduce errors are caused by ambiguous

grammars.

  • We can use resolution mechanisms to our advantage. Use an

ambiguous grammar (smaller more concise, more natural parse

trees) but resolve ambiguity using rules.

  • Operator Precedence
    • Every operator is given a precedence
    • Precedence of the operator closest to the top of the stack and the precedence of operator next on the input decide shift or reduce.
    • Sometimes the precedence is the same. Need more information: Associativity information.

Precedence parsers

• Precedence parsers have limitations

• No production can have two consecutive non-terminals

• Parse only a small subset of the Context Free Grammars

• Need a more robust version of shift- reduce parsing.

• LR - parsers

  • State based - finite state automatons (w / stack)
  • Accept the widest range of grammars
  • Easily constructed (by a machine)
  • Can be modified to accept ambiguous grammars by using precedence and associativity information.

LR Parsers

  • Table Driven Parsers
  • Table is indexed by state and symbols (both term and non-term)
  • Table has two components.
    • ACTION part
    • GOTO part

state

terminals non-terminals

0 1

2

id + * ( ) $ E T F

shift (state = 5)

reduce(prod = 12)

goto(state = 2)

ACTION GOTO

Table vs FSA

• The Table encodes the FSA

• The action part encodes

  • Transitions on terminal symbols (shift)
  • Finding the end of a production (reduce)

• The goto part encodes

  • Tracing backwards the symbols on the RHS
  • Transition on non-terminal, the LHS

• Tables can be quite compact

LR Table

state

terminals non-terminals

0 1 2 3 4 5 6 7 8 9

10

11

id +^ * (^ )^ $^ E^ T^ F

s5 s4 (^1 2 )

s6 acc

r2 (^) s7 r2 r r4 r4 (^) r r s5 (^) s4 8 2 3 r6 (^) r6 r6 (^) r

s5 (^) s4 (^9 )

s5 s4 10

s6 s

r1 (^) s7 r1 (^) r

r3 r3^ r3^ r

r5 r5 r5 (^) r

Example Parse

Stack Input

(?,0) id * id + id $

(?,0)(id,5) * id + id $

(?,0)(F,3) * id + id $

(?,0)(T,2) * id + id $

(?,0)(T,2)(*,7) id + id $

(?,0)(T,2)(*,7)(id,5) + id $

(?,0)(T,2)(*,7)(F,10) + id $

(?,0)(T,2) + id $

(?,0)(E,1) + id $

(?,0)(E,1)(+,6) id $

(?,0)(E,1)(+,6)(id,5) $

(?,0)(E,1)(+,6)(F,3) $

(?,0)(E,1)(+,6)(T,9) $

(?,0)(E,1) $

1) E -> E + T
2) E -> T
3) T -> T * F
4) T -> F
5) F -> ( E )
  1. F -> id

Constructing LR Tables

  • A state represents some local state of the parse.
  • It encodes “how far” we have progressed in recognizing

some RHS of a production.

  • We will represent this by an “item”
    • An item is a production with a dot
    • A ->. X Y Z
    • A -> X. Y Z
    • A -> X Y. Z
    • A -> X Y Z.
  • An item with a dot at the end is called a completed item
  • An item with a dot at the beginning is called an initial item
  • A state in the FSA is represented by a set of items.

Example closure construction

Start of with the single item derived from the start symbol.

I = { E’ ->. E }

Then the closure of I contains:

{ E’ ->. E,

E ->. E + T,

E -> .T

T ->. T * F

T ->. F

F ->. ( E )

F ->. id }

0) E’ -> E
1) E -> E + T
• E -> T
3) T -> T * F
4) T -> F
5) F -> ( E )

6) F -> id

The GOTO Construction

  • Goto(I,X)
    • I is a set of items
    • X is a grammar symbol
    • This discovers new sets of items in the FSA given an old set.
  • GOTO(I,X) = closure(J) such that
    • [A -> a X. b ] in J when [A -> a. X b ] is in I
  • GOTO(I,X) will be {} if there is no item in I with a dot before an X

• Example: I = { E’->. E, E-> E. + T}

GOTO(I,+) = { E-> E +. T, (initial item)

T->. T * F, (plus the closure of it)

T->. F,

F->. ( E ),

F->. id }