unit24
unit24
Sharma
Unit II
Grammar G (V , T , S , P)
E E + E E E + E
E ( E ) | ( E )
E -E | -E
E id | id
E E + E
E E + E| ( E ) | -E | id
( E )
-E
id
Learn Compiler Design: From B K Sharma
What is derivation?
Derivation: is a process that transform a starting non-
terminal into a collection of terminals / tokens by
applying a sequence of grammar rule applications and
substitutions of Non-terminals with terminals or
tokens.
A derivation uses the productions of a grammar to
replace non-terminals until there are only tokens left.
In CFG, the start symbol is used to derive the string.
We can derive the string by repeatedly replacing a
non-terminal by the right hand side of the production,
until all non-terminal have been replaced by terminal
symbols.
Learn Compiler Design: From B K Sharma
Leftmost Derivations:
Rightmost Derivations:
Rightmost derivation:
Learn Compiler Design: From B. K. Sharma
Types of Derivations:
Example
G: Leftmost Derivations:
Learn Compiler Design: From B. K. Sharma
Types of Derivations:
Example
Rightmost Derivations:
Learn Compiler Design: From B K Sharma
Sentential form
G:
Learn Compiler Design: From B K Sharma
Derivation Tree / Parse Tree
Grammar Problems
- ambiguity
- ε-moves
Grammar
- cycles
Problems
- left recursion
- left factoring
Extra Slide
Learn Compiler Design: From B K Sharma