Alex Aiken
Compilers Derivations Alex Aiken Derivations A derivation is a - - PowerPoint PPT Presentation
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 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
Alex Aiken
Derivations
- Grammar
E E + E | E E | (E) | id
- String
id id + id
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
Alex Aiken
Derivations E
E
SLIDE 6
Alex Aiken
Derivations
E E+E
E E E +
SLIDE 7
Alex Aiken
Derivations
E E E E+E E +
E E E E E + *
SLIDE 8
Alex Aiken
Derivations
E E+E E E+E id E + E
E E E E E + * id
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
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
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
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
Alex Aiken
Derivations E
E
SLIDE 14
Alex Aiken
Derivations
E E+E
E E E +
SLIDE 15
Alex Aiken
Derivations
id E E+E E+
E E E + id
SLIDE 16
Alex Aiken
Derivations
E E+E E+id E E + id
E E E E E + id *
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
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
Alex Aiken
Derivations Note that right-most and left-most derivations have the same parse tree
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
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
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