15 Derivations
15 Derivations
15 Derivations
Arles Rodríguez
[email protected]
Facultad de Ciencias
Departamento de Matemáticas
Universidad Nacional de Colombia
Definition: Derivation
• A derivation is a sequence of productions
Grammar: 𝐸
E
-> E + E 𝐸 +¿ 𝐸
-> E * E + E 𝐸 ∗ 𝐸 𝑖𝑑
-> id * E + E
-> id * id + E
𝑖𝑑 𝑖𝑑
-> id * id + id This is called the Parse Tree
We can produce a derivation for the string and build the tree at the same time
Derivations: Example
Grammar:
Derivations: Example
• Production E goes to E + E
E 𝐸
-> E + E
𝐸 +¿ 𝐸
Grammar:
Derivations: Example
• Replace E by E * E
E 𝐸
-> E + E
-> E * E + E
𝐸 +¿ 𝐸
𝐸 ∗ 𝐸
Grammar:
Derivations: Example
• Replace E by id
E 𝐸
-> E + E
-> E * E + E
𝐸 +¿ 𝐸
-> id * E + E 𝐸 ∗ 𝐸
𝑖𝑑
Grammar:
Derivations: Example
• Replace E by id
E 𝐸
-> E + E
-> E * E + E
𝐸 +¿ 𝐸
-> id * E + E 𝐸 ∗ 𝐸
-> id * id + E
𝑖𝑑 𝑖𝑑
Grammar:
Derivations: Example
• Replace E by id
E 𝐸
-> E + E
-> E * E + E
𝐸 +¿ 𝐸
-> id * E + E 𝐸 ∗ 𝐸 𝑖𝑑
-> id * id + E
-> id * id + id
𝑖𝑑 𝑖𝑑
Grammar:
Derivations
• A parse tree has:
– Terminals at the leaves
– Non-terminal at the interior nodes
• An in-order transversal of the leaves is the
original input.
• The parse tree shows the association of
operation, the input string does not.
Derivations
• The previous
example corresponds
E
to a left-most -> E + E
derivation:
-> E * E + E
– At each step replace
-> id * E + E
the left most non-
-> id * id + E
terminal.
-> id * id + id
Derivations
• There is an
equivalent notion of
E
right-most derivation -> E + E
-> E + id
-> E * E + id
-> E * id + id
-> id * id + id
Derivations
• There is an
equivalent notion of
right-most derivation
𝐸
E
Derivations
• There is an
equivalent notion of
right-most derivation
𝐸
E
-> E + E
𝐸 +¿ 𝐸
Derivations
• There is an
equivalent notion of
right-most derivation 𝐸
E
-> E + E
𝐸 +¿ 𝐸
-> E + id 𝑖𝑑
Derivations
• There is an
equivalent notion of
right-most derivation 𝐸
E
-> E + E
𝐸 +¿ 𝐸
-> E + id 𝐸 ∗ 𝐸 𝑖𝑑
-> E * E + id
Derivations
• There is an
equivalent notion of
right-most derivation 𝐸
E
-> E + E
𝐸 +¿ 𝐸
-> E + id 𝐸 ∗ 𝐸 𝑖𝑑
-> E * E + id
𝑖𝑑
-> E * id + id
Derivations
• There is an
equivalent notion of
right-most derivation 𝐸
E
-> E + E
𝐸 +¿ 𝐸
-> E + id 𝐸 ∗ 𝐸 𝑖𝑑
-> E * E + id
𝑖𝑑 𝑖𝑑
-> E * id + id
-> id * id + id
Derivations
• The right-most and left-most derivations
have the same parse tree.
• Every parse-tree has a right-most and left-
most derivation.
• There are many other derivations like random
non-terminal selection but we most concern
about right-most and left-most derivations.
Exercise
Exercise 2
Summary
• We are not just interested in whether
– We need a parse tree for s
• A derivation defines a parse tree
– But one parse tree may have many derivations
– We study left-most and right-most derivations
since they are important in parser implementation.
¡Thank you!
References
• Aho et al. Compilers: principles, techniques, and
tools. Torczon et al. (2014) (Section 4.2)
• Slides are based on the design of Aiken Alex. CS 143.
https://web.stanford.edu/class/cs143/