Week 2 lecture 1 l.jpg
Advertisement
This presentation is the property of its rightful owner.
1 / 21

Week 2 – Lecture 1 PowerPoint PPT Presentation

Week 2 – Lecture 1 Compiler Construction Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR pa rsing table Bottom-up Parsing Shift reduce parsing Parse from the input string to the start symbol Begins at the bottom of the parse tree working up towards the root

Related searches for Week 2 – Lecture 1

Download Presentation

Week 2 – Lecture 1

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Week 2 lecture 1 l.jpg

Week 2 – Lecture 1

Compiler Construction

Bottom Up (Shift reduce, LR parsing)

SLR, LR(0) parsing

SLR parsing table


Bottom up parsing l.jpg

Bottom-up Parsing

  • Shift reduce parsing

    • Parse from the input string to the start symbol

    • Begins at the bottom of the parse tree working up towards the root

  • At each step a substring (of terminals and nonterminals comprising the right hand side of a production reduced to a single nonterminal (the left side of a production)

  • A rightmost derivation in reverse


Example l.jpg

example

E ->E + T | T

T->T * F | F

F -> (E) | id

What is the rightmost derivation for id + id * id

A handle is a substring which matches the right hand side of a production

Handles are pruned


Reductions made by shift reduce parser l.jpg

Reductions made by shift-reduce parser


Stack implementation of a shift reduce parser l.jpg

Stack implementation of a shift reduce Parser

Actions - shift, reduce, accept

Configurations of a shift-reduce parser on input id1 + id2 * id3


Shift reduce conflicts l.jpg

Shift-reduce conflicts

Stmt -> if Expr then Stmt

| if Expr then Stmt else Stmt

other

STACK

…if Expr then Stmt

INPUT

Else …$


Lr k parsers l.jpg

LR(k) Parsers

  • LR – Left to right scan of the input, Rightmost derivation, k number of symbols lookahead

  • Most general nonbacktracking shift-reduce parsing method

  • Parses all grammars that predictive parsers can and more

  • Too much work to construct by hand


The lr parser l.jpg

an

a1

ai

$

The LR Parser

INPUT

sm

LR

Parsing Program

STACK

OUTPUT

Xm

sm-1

Xm-1

goto

action

s0

Aho, Sethi and Ullman (p.217)


Lr parsing table l.jpg

State

goto

LR Parsing Table

action

Page 219 Aho, Sethi and Ullman


Slide10 l.jpg

T

E

+

9

*

go to 7

6

0

1

F

go to 3

(

go to 4

id

go to 5

T

*

2

F

7

10

(

go to 4

id

go to 5

F

3

(

)

(

E

4

8

11

+

go to 6

T

go to 2

F

go to 3

id

id

5

Page 226 Aho, Sethi and Ullman


Slide11 l.jpg

Moves of an LR parser on id * id + id

  • (1) E -> E + T

  • (2) E -> T

  • (3) T -> T * F

  • (4) T -> F

  • (5) F -> (E)

  • (6) F -> id


Constructing slr parsing tables l.jpg

Constructing SLR parsing tables

  • Closure operation

  • GOTO operation

  • The set of items construction

  • SLR parsing table


The closure operation l.jpg

The Closure Operation

If I is the set of items for a grammar G, then closure(I) is the set of items constructed from I by:

  • Initially, every item in I is added to closure(I)

  • If A -> a .Bb is in closure(I) and B -> g is a production, then add the item B -> .g to I if it is not already there. Apply this rule until no more new items can be added to closure(I)

    E’ -> E

    E -> E + T

    T -> T * F | F

    F -> (E) | id

Initially I is the set of one item {[E’->.E]}


Closure of e e l.jpg

Closure of {[E’->.E]}

E’->.E


The goto operation l.jpg

The GOTO operation

  • Move the dot over one symbol

  • This becomes the kernel item for a new state

  • Compute its closureI1 = {[E’ ->E .], [E -> E . + T]}

    goto(I1 +) = {[E -> E + . T]}

    Closure(I1) = ??


First and follow sets l.jpg

First and Follow Sets

  • First and Follow sets tell when it is appropriate to put the right hand side of some production on the stack in predictive parsing

    (i.e. for which input symbols)

  • In LR parsing FOLLOW sets are used to tell us if we should reduce a handle or shift an input symbol to produce a bigger handle


First sets l.jpg

First Sets

  • If X is a terminal, then FIRST(X) is {X}

  • IF X -> e is a production, then add e to FIRST(X)

  • IF X is a nonterminal and X -> Y1Y2…Yk is a production, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and e is in all of First(Y1), …First(Yi-1). If e is in FIRST(Yj) for all j = 1, 2, …k, then add e to FIRST(X).


First sets18 l.jpg

FIRST sets

(1) E’ -> E

(2) E -> E + T

(3) E -> T

(4) T -> T * F

(5) F -> (E)

(6) F -> id


The slr parsing table l.jpg

The SLR parsing table

  • Construct C = {I0, I1, …., In}, the collection of sets of LR(0) items

  • State i is constructed from Ii. The parsng actions for state i are determined as follows

    • If [A->a.ab] is in Ii and goto(Ii, a) = Ij, then set action[i,a] to shift j. A must be a terminal.

    • If [A->a.] is in Ii, then set action[I, a] to “reduce A -> a” for all a in FOLLOW(A); here A may not be S’

    • If [S’ -> S .] is in Ii then set action [i, $] to accept.


Follow sets l.jpg

Follow Sets

  • Apply the following rules until no more terminals can be added to any follow set

    • Place $ in FOLLOW (S’), where S is the start symbol and $ is the input right end marker

    • If there is a production A -> aBb, then everything in FIRST(b) except for e is placed in FOLLOW(B)

    • If there is a production A -> aB, or a production A -> aBb where FIRST(b) contains e (i.e. B => e), then everything in FOLLOW(A) is in FOLLOW(B).

*


Follow sets21 l.jpg

Follow Sets

(1) E’ -> E

(2) E -> E + T

(3) E -> T

(4) T -> T * F

(5) F -> (E)

(6) F -> id


  • Login