0% found this document useful (0 votes)
8 views18 pages

Unit 3 Notes

The document discusses the concept of palindromes and the derivation process in formal grammar, including leftmost and rightmost derivations. It explains how to construct parse trees and addresses ambiguity in grammar with examples. Additionally, it outlines applications of context-free grammars in programming and natural languages, along with problems to determine grammar ambiguity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Unit 3 Notes

The document discusses the concept of palindromes and the derivation process in formal grammar, including leftmost and rightmost derivations. It explains how to construct parse trees and addresses ambiguity in grammar with examples. Additionally, it outlines applications of context-free grammars in programming and natural languages, along with problems to determine grammar ambiguity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Let us consider the language of palindromesA palindrome is a string that reads the same forward and

backward. Ex: madam, Malayalam.


Derivation
The process of deriving a desired string using productions is called as derivation.

The geometrical representation of a derivation is called as a parse tree or derivation tree.

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

Derive the string 11011

P→1P1→11P11→11011

During parsing, we have to take two decisions. These are as follows:

o We have to decide the non-terminal which is to be replaced.


o We have to decide the production rule by which the non-terminal will be replaced.

We have two options to decide which non-terminal to be placed with production rule.

1. Leftmost 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.

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 ∈.

Ambiguity in Grammar: A Grammar G = (V, T, P and S) is said to be ambiguous if there is a


string w ε L (G) for which we can derive two or more distinct derivation tree rooted at S and
yielding w. In other words, a grammar is ambiguous if it can produce more than one leftmost
or more than one rightmost derivation for the same string in the language of the grammar.

Example : S-> AB |aaB A-> a | Aa B ->b

For any string aab; We have two leftmost derivations as;

S->AB ->AaB ->aaB->aab Also, S->aaB->aab


Applications of Context – Free Grammars

• 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

REFER THE PROBLEMS SOLVED IN CLASS


REFER THE PROBLEMS SOLVED IN CLASS
D D gets

REFER THE PROBLEMS SOLVED IN CLASS


REFER THE PROBLEMS SOLVED IN CLASS

You might also like