Compilers Derivations Alex Aiken Derivations A derivation is a - - PowerPoint PPT Presentation

compilers
SMART_READER_LITE
LIVE PREVIEW

Compilers Derivations Alex Aiken Derivations A derivation is a - - PowerPoint PPT Presentation

Compilers Derivations Alex Aiken Derivations A derivation is a sequence of productions S A derivation can be drawn as a tree Start symbol is the trees root For a production X Y 1 Y n


slide-1
SLIDE 1

Alex Aiken

Compilers

Derivations

slide-2
SLIDE 2

Alex Aiken

Derivations A derivation is a sequence of productions S  …  …  …  …  … A derivation can be drawn as a tree – Start symbol is the tree’s root – For a production X  Y1…Yn add children Y1…Yn to node X

slide-3
SLIDE 3

Alex Aiken

Derivations

  • Grammar

E  E + E | E  E | (E) | id

  • String

id  id + id

slide-4
SLIDE 4

Alex Aiken

Derivations

E E+E E E+E id E + E id id + E id id + id         

E E E E E + id * id id

slide-5
SLIDE 5

Alex Aiken

Derivations E

E

slide-6
SLIDE 6

Alex Aiken

Derivations

E E+E 

E E E +

slide-7
SLIDE 7

Alex Aiken

Derivations

E E E E+E E +   

E E E E E + *

slide-8
SLIDE 8

Alex Aiken

Derivations

E E+E E E+E id E + E     

E E E E E + * id

slide-9
SLIDE 9

Alex Aiken

Derivations

E E+E E E+E id E + id id + E E       

E E E E E + * id id

slide-10
SLIDE 10

Alex Aiken

Derivations

E E+E E E+E id E + E id id + E id id + id         

E E E E E + id * id id

slide-11
SLIDE 11

Alex Aiken

Derivations

  • A parse tree has

– Terminals at the leaves – Non-terminals at the interior nodes

  • An in-order traversal of the leaves is the original input
  • The parse tree shows the association of operations, the

input string does not

slide-12
SLIDE 12

Alex Aiken

  • The example is a left-most

derivation

– At each step, replace the left- most non-terminal

  • There is an equivalent

notion of a right-most derivation

E E+E E+id E E + id E id + id id id + id        

Derivations

slide-13
SLIDE 13

Alex Aiken

Derivations E

E

slide-14
SLIDE 14

Alex Aiken

Derivations

E E+E 

E E E +

slide-15
SLIDE 15

Alex Aiken

Derivations

id E E+E E+  

E E E + id

slide-16
SLIDE 16

Alex Aiken

Derivations

E E+E E+id E E + id    

E E E E E + id *

slide-17
SLIDE 17

Alex Aiken

Derivations

E E+E E+id E E E + id id + id      

E E E E E + id * id

slide-18
SLIDE 18

Alex Aiken

Derivations

E E+E E+id E E + id E id + id id id + id        

E E E E E + id * id id

slide-19
SLIDE 19

Alex Aiken

Derivations Note that right-most and left-most derivations have the same parse tree

slide-20
SLIDE 20

Derivations

S aXa abYa acXca acca

Which of the following is a valid derivation of the given grammar? S  aXa X   | bY Y   | cXc | d

S aXa abYa abcXca abcbYca abcbdca S aXa abYa abcXcda abccda S aa

slide-21
SLIDE 21

Derivations Which of the following is a valid parse tree for the given grammar? S  aXa X   | bY Y   | cXc | d

S a b Y a X  c c X S a b Y a X  c c X d S a b Y a X  c c X d  S a b Y a X

slide-22
SLIDE 22

Alex Aiken

Derivations

  • We are not just interested in whether s  L(G)

– We need a parse tree for s

  • A derivation defines a parse tree

– But one parse tree may have many derivations

  • Left-most and right-most derivations are important

in parser implementation