Review: LR(k) parsers - PowerPoint PPT Presentation

About This Presentation
Title:

Review: LR(k) parsers

Description:

A grammar for which we can construct a parsing table is called an LR grammar. Constructing Simple LR (SLR) parsing table: Let G be a grammar with start symbol S, ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 9
Provided by: xyuan
Learn more at: http://www.cs.fsu.edu
Category:
Tags: parsers | review

less

Transcript and Presenter's Notes

Title: Review: LR(k) parsers


1
  • Review LR(k) parsers

input
a1 a2 an
Sm xm s1 x1 s0
LR parsing program
output
Parsing table
Action goto
stack
2
  • The parsing table has two parts action table and
    goto table.
  • The entries in the action table, actions, t can
    have four values
  • shift s1, where s1 is a state.
  • reduce by a production A-gtb.
  • accept
  • error
  • Goto table entries gotos, T s1, means from
    current state s, place a non-terminal symbol T
    results in state s1.

3
  • A grammar for which we can construct a parsing
    table is called an LR grammar.
  • Constructing Simple LR (SLR) parsing table
  • Let G be a grammar with start symbol S, the
    augmented grammar for G, is G with a new start
    symbol S and production S-gtS.
  • LR(0) item An LR(0) item of a grammar G is a
    production of G with a dot at some position of
    the right side.
  • Example A-gtXYZ has four LR(0) items.
  • A-gt.XYZ, A-gtX.YZ, A-gtXY.Z, A-gtXYZ.
  • A-gte has one LR(0) item A-gt..

4
  • The Closure operation
  • Let I be a set of items for a grammar G,
    closure(I) can be calculated as follows
  • JI
  • repeat
  • for each item A-gta.Bb in J and production
    B-gtc,
  • add B-gt.c to J (if not there).
  • Until no more items can be added to J.
  • Example
  • E -gtE I E-gt.E,
    what is closure(I)?
  • E-gtET T
  • T-gtTF F
  • F-gt(E) id

5
  • The Goto Operation
  • Goto(I, X), where I is a set of items and X is a
    grammar symbol, is defined to be the closure of
    the set of all items A-gtaX.b such that A-gta.Xb is
    in I.
  • Goto(I, X) closure(the set of items of the form
    A-gtaX.b)
  • where A-gta.Xb is in I.
  • Example
  • E-gtE I E-gtE., E-gtE. T
  • E-gtET T
  • T-gtTF F Goto(I, ) ?
  • F-gt(E) id

6
  • Construct canonical collection of sets of LR(0)
    items
  • CclosureS-gt.S
  • repeat
  • for each set of items I in C and each grammar
    symbol X such that goto(I, X) is not empty and is
    not in C do
  • add goto(I, X) to C
  • until no more sets of items can be added to C.
  • Example
  • E-gtE
  • E-gtET T
  • T-gtTF F
  • F-gt(E) id
  • Let each set of a state I and goto(I, X) be the
    transition. If I0 is the initial state and all
    other states are final states, this DFA
    recognizes all viable prefixes of grammar.

7
  • Constructing the SLR parsing table
  • Compute the canonical LR collection sets of LR(0)
    items for grammar G, let it be CI0, I1, .,
    In.
  • For terminal a, if A-gtX.aY in Ii and goto(Ii, a)
    Ij, then set actioni, a to shift j.
  • if A-gtX. is in Ii and A ! S, for all terminals
    a in Follow(A), set actioni, a to reduce A-gtX.
  • if S-gtS. is in Ii, then set actioni,
    accept.
  • For non-terminal symbol A, if goto(Ii, A) Ij,
    then set
  • goto(i, A) j
  • set all other table entries to error
  • The initial state is the one holding S-gt.S
  • Example 4.36 in page 224.

8
  • Exercise construct the SLR parsing table for
    grammar
  • S-gtLR,
  • S-gtR
  • L-gtR
  • L-gtid
  • R-gtL
  • The grammar can have shift/reduce conflict or
    reduce/reduce conflict.
  • What about shift/shift conflict
Write a Comment
User Comments (0)
About PowerShow.com