CFGs and Its Derivation Tree
CFGs and Its Derivation Tree
1. Start with the Start Symbol: Begin with the start symbol, which
represents the starting point for generating strings.
2. Apply Production Rules: Apply the production rules to the current
symbol, replacing it with the corresponding string of terminals and/or
non-terminals.
3. Repeat Step 2: Continue applying production rules until a string of
terminals is generated.
4. Resulting String: The resulting string is a valid sentence in the
language defined by the CFG.
# Example CFG
Consider a simple CFG that generates strings of binary numbers:
S → AB
A → 0A | 1A | ε
B → 0B | 1B | ε
In this example, S is the start symbol, A and B are non-terminals, and 0
and 1 are terminals. The production rules define how to generate
strings of binary numbers.
Applications of CFGs
CFGs have numerous applications in:
Types of Derivations
There are two types of derivations:
E=E+E
E=E*E
E=a|b|c
Solution :
1. Leftmost Derivation:
In the leftmost derivation, the input is scanned and replaced with the
production rule from left to right. So in leftmost derivation, we read the input
string from left to right.
Example:
Production rules:
1. E = E + E
2. E = E - E
3. E = a | b
Input
1. a - b + a
The leftmost derivation is:
1. E = E + E
2. E = E-E+E
3. E = a-E+E
4. E = a-b+E
5. E = a-b+a
2. Rightmost Derivation:
In rightmost derivation, the input is scanned and replaced with the
production rule from right to left. So in rightmost derivation, we read the
input string from right to left.
Example
Production rules:
1. E = E + E
2. E = E - E
3. E = a | b
Input
1. a - b + a
The rightmost derivation is:
1. E = E - E
2. E=E-E+E
3. E=E-E+a
4. E=E-b+a
5. E=a-b+a
When we use the leftmost derivation or rightmost derivation, we may get the
same string. This type of derivation does not affect on getting of a string.
Example 2:
Derive the string "abb" for leftmost derivation and rightmost derivation using
a CFG given by,
1. S → AB | ε
2. A → aB
3. B → Sb
Solution:
Leftmost derivation:
Rightmost derivation:
Example 3:
Derive the string "00101" for leftmost derivation and rightmost derivation
using a CFG given by,
1. S → A1B
2. A → 0A | ε
3. B → 0B | 1B | ε
Leftmost derivation:
1. S
2. A1B
3. 0A1B
4. 00A1B
5. 001B
6. 0010B
7. 00101B
8. 00101
Rightmost derivation:
1. S
2. A1B
3. A10B
4. A101B
5. A101
6. 0A101
7. 00A101
8. 00101
Derivation Tree
Derivation tree is a graphical representation for the derivation of the given
production rules for a given CFG. It is the simple way to show how the
derivation can be done to obtain some string from a given set of production
rules. The derivation tree is also called a parse tree.
Example 1:
Production rules:
1. E = E + E
2. E = E * E
3. E = a | b | c
Input
1. a * b + c
Step 1:
Step 2:
Step 2:
Step 4:
Step 5:
Note: We can draw a derivation tree step by step or
directly in one step.
Example 2:
Draw a derivation tree for the string "bab" from the CFG given by
1. S → bSb | a | b
Solution:
The above tree is a derivation tree drawn for deriving a string bbabb. By
simply reading the leaf nodes, we can obtain the desired string. The same
tree can also be denoted by,
Example 4:
Show the derivation tree for string "aabbbb" with the following grammar.
1. S → AB | ε
2. A → aB
3. B → Sb
Solution:
To draw a tree we will first try to obtain derivation for the string aabbbb
What is Ambiguity?
A CFG is ambiguous if:
A string can have two or more derivation trees.
The same string can have different meanings based on the grammar.
This makes the grammar unclear and problematic, especially for programming languages,
where each expression must have only one valid interpretation.
example :
G:->Sa|aS|a
string to drive aaa