Unit 3 Notes
Unit 3 Notes
Derivation is a sequence of production rules. It is used to get the input string through these pro
duction rules.
Ex : P→Ꜫ/0/1/0P0/1P1
P→1P1→11P11→11011
We have two options to decide which non-terminal to be placed with production rule.
1. Leftmost Derivation:
Example:
2. Rightmost Derivation:
The process of deriving a string by expanding the leftmost non-terminal at each step is called
as leftmost derivation.
The geometrical representation of leftmost derivation is called as a leftmost derivation tree
Ex:
Consider the expression grammar :
E -> E + E | E * E | (E) | I
Constructing Parse tree:
Yield of Parse Tree:
The derivation or the yield of a parse tree is the final string obtained by concatenating the labels
of the leaves of the tree from left to right, ignoring the ∈.
• CFGs can be used to define the syntax of programming languages and natural
languages.
• Context Free Grammars are used to define the High-Level Structure of a Programming
Languages.
• Every Context Free Grammars can be converted to a Parser which is a component of a
Compiler that identifies the structure of a Program and converts the Program into a
Tree.
• Document Type Definition in XML is a Context Free Grammars which describes the
HTML tags and the rules to use the tags in a nested fashion.
Problems:
1. Consider the grammar S→aS|aSbS|Ꜫ Show that deviation for the string aab is
ambiguous
2. Show that the following grammar is ambiguous.
S ->aB | ab A -> AB | a B -> Abb | b string: ab
3. Check whether the given grammar is ambiguous or not-
S → SS S→a S→b string: abba
4. Check whether the given grammar is ambiguous or not-
S → A / B A → aAb / ab B → ab B/∈ string: ab
5. Check whether the given grammar is ambiguous or not-
S → a / abSb / aAb A → bS / aAAb w = abababb
6. Check whether the given grammar is ambiguous or not
S → aSbS / bSaS / ∈ w = abab
7. Check whether the given grammar is ambiguous or not.
R → R + R / R . R / R* / a / b w = ab + a