Compilers Shift-Reduce Parsing Alex Aiken Shift-Reduce Parsing - - PowerPoint PPT Presentation

compilers
SMART_READER_LITE
LIVE PREVIEW

Compilers Shift-Reduce Parsing Alex Aiken Shift-Reduce Parsing - - PowerPoint PPT Presentation

Compilers Shift-Reduce Parsing Alex Aiken Shift-Reduce Parsing Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse Alex Aiken Shift-Reduce Parsing Important Fact #1 has an interesting


slide-1
SLIDE 1

Alex Aiken

Compilers

Shift-Reduce Parsing

slide-2
SLIDE 2

Alex Aiken

Shift-Reduce Parsing Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse

slide-3
SLIDE 3

Alex Aiken

Shift-Reduce Parsing Important Fact #1 has an interesting consequence: – Let  be a step of a bottom-up parse – Assume the next reduction is by X  – Then  is a string of terminals Why? Because X   is a step in a right-most derivation

slide-4
SLIDE 4

Alex Aiken

Shift-Reduce Parsing

  • Idea: Split string into two substrings

– Right substring is as yet unexamined by parsing – Left substring has terminals and non-terminals – The dividing point is marked by a |

slide-5
SLIDE 5

Alex Aiken

Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce

slide-6
SLIDE 6

Alex Aiken

Shift-Reduce Parsing

  • Shift: Move | one place to the right

– Shifts a terminal to the left string ABC|xyz  ABCx|yz

slide-7
SLIDE 7

Alex Aiken

Shift-Reduce Parsing

  • Apply an inverse production at the right end of the

left string – If A  xy is a production, then Cbxy|ijk  CbA|ijk

slide-8
SLIDE 8

Alex Aiken

Shift-Reduce Parsing

int * int | + int reduce T  int int * T | + int reduce T  int * T T + int | reduce T  int T + T | reduce E  T T + E | reduce E  T + E E |

slide-9
SLIDE 9

Alex Aiken

Shift-Reduce Parsing

|int * int + int shift int | * int + int shift int * | int + int shift int * int | + int reduce T  int int * T | + int reduce T  int * T T | + int shift T + | int shift T + int | reduce T  int T + T | reduce E  T T + E | reduce E  T + E E |

slide-10
SLIDE 10

Alex Aiken

Shift-Reduce Parsing + int * int int

 |int * int + int

slide-11
SLIDE 11

Alex Aiken

Shift-Reduce Parsing + int * int int

 |int * int + int int | * int + int

slide-12
SLIDE 12

Alex Aiken

Shift-Reduce Parsing + int * int int

 |int * int + int int | * int + int int * | int + int

slide-13
SLIDE 13

Alex Aiken

Shift-Reduce Parsing + int * int int

 |int * int + int int | * int + int int * | int + int int * int | + int

slide-14
SLIDE 14

Alex Aiken

Shift-Reduce Parsing + int * int int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int 

slide-15
SLIDE 15

Alex Aiken

Shift-Reduce Parsing T + int * int int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int 

slide-16
SLIDE 16

Alex Aiken

Shift-Reduce Parsing T + int * int int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int 

slide-17
SLIDE 17

Alex Aiken

Shift-Reduce Parsing T + int * int int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | 

slide-18
SLIDE 18

Alex Aiken

Shift-Reduce Parsing T + int * int T int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | 

slide-19
SLIDE 19

Alex Aiken

Shift-Reduce Parsing T E + int * int T int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | 

slide-20
SLIDE 20

Alex Aiken

Shift-Reduce Parsing E T E + int * int T int T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | E | 

slide-21
SLIDE 21

Shift-Reduce Parsing

|id + -id id|+ -id E’|+ -id E’ +|-id E’ + -|id E’ + -id| E’ + -E’| E’ + E’| E’ + E| E|

For the given grammar, what is the correct shift- reduce parse for the string: id + -id

E  E’ | E’ + E E’ -E’ | id | (E)

|id + -id id|+ -id id +|-id id + -|id id + -id| id + -E’| id + E’| id + E| E’ + E| E| |id + -id |E’ + -id E’|+ -id E’ +|-id E’ + -|id E’ + -|E’ E’ +|-E’ E’ +|E’ E’ +|E E’|+ E |E’ + E |E |id + -id id|+ -id E’ +|-id E’ + -|id E’ + -id| E’ + -E’| E’ + E’| E’ + E| E|

slide-22
SLIDE 22

Alex Aiken

Shift-Reduce Parsing

  • Left string can be implemented by a stack

– Top of the stack is the |

  • Shift pushes a terminal on the stack
  • Reduce

– pops symbols off of the stack (production rhs) – pushes a non-terminal on the stack (production lhs)

slide-23
SLIDE 23

Alex Aiken

Shift-Reduce Parsing

  • In a given state, more than one action (shift or reduce) may

lead to a valid parse

  • If it is legal to shift or reduce, there is a shift-reduce conflict
  • If it is legal to reduce by two different productions, there is a

reduce-reduce conflict