Download presentation
Presentation is loading. Please wait.
1
FORMAL LANGUAGES AND AUTOMATA THEORY
C K Nagpal M.Tech. Ph.D. Associate Professor Computer Engg. YMCA University of Science & Technology
2
Chapter 5 Context Free Grammars and Languages
After studying this chapter, the reader will be able to understand the following: • Basic structure of a context-free grammar (CFG) • Context-free languages as the superset of regular languages • Identification of ambiguity in a CFG Possible Defects in CFG Order of Defect Removal Superset Property Closure & Non Closure Properties Normal Forms for Context Free Grammars Pumping Lemma for Context Free Grammars Ogden’s Lemma Applications of Context Free Grammars
3
Recursive Grammars Language L1 = {akbk | k ≥ 0} over {a, b}.
Recursive definition for L1 Rule 1: ϵ belongs to L1. Rule 2: For every string w belonging to L1, awb belongs to L1. Rule 3: Nothing else belongs to L1. Language L2 = {w | w is palindrome} over {a, b, c}. Recursive definition for L2 Rule 1: ϵ belongs to L2 Rule 2: a, b and c belong to L2 Rule 3: For every string w belonging to L2, awa, bwb and cwc belongs to L2. Rule 4: Nothing else belongs to L2.
4
Context Free Grammars (CFG)
Context Free Grammar is a 4 tuple structure G (V, ∑, P, S) ∑ is the character set of the language. V is the finite set of variables also known as non terminals or syntactic categories. They are used to define the structure of sentences in the language. P is known as set of productions or production set. Each production in the production set of a CFG is of the form A→ (V U ∑ )*. In CFG, the LHS of a production contains a single non terminal and RHS contains a string composed of terminals and non terminals. A → aBBc | aAb S→AA | BB| Bcc S is known as starting sentential form. It is initial production from where the process of string formation begins.
5
Context Free language (CFL)
CFL : The language corresponding to CFG A string w belongs to CFL L, if it can be derived from the CFG G of L. Approaches used to check if a string w belongs to CFL L Derivation Recursive Inference Derivation approach categories Leftmost derivation Rightmost derivation
6
Given Grammar S→ AA | BB | ASA | BSB | a | b A → a
Given Grammar S→ AA | BB | ASA | BSB | a | b A → a B→ b Given String aaabbbaa
7
Some Example CFLs L={0n1n | n ≥ 0} L={0n12n | n ≥ 0}
L over {0, 1} to generate all the strings having alternate sequence of 0 and 1 L over {a, b, c} such that L={wcwR| w is string over {a, b} and wR is reverse of w} L={anbncmdm| n, m > 0}
8
Ambiguity in Context Free Grammar
A grammar is said to be ambiguous if one or more sentences in the corresponding language can be interpreted in more than one ways. The ambiguity in case of context free grammar is reflected by the occurrence of two or more different parse trees for the same sentence.
9
Ambiguous Grammar S→S+S |S x S | a | b Sentence a + a x b
Two different parse trees
10
Possible Defects in CFG
Failure of non terminal(s) to generate terminal(s) S→ABCD|ABD A→ aA|a B→bB|bb C→cC D → aadd Unit Productions S→ABCD|ABD A→ aA|a B→b|C C→D D→d Unreachable Non terminal S→AB A→ aA|a B→bB|cC C→cc D→d E→a Null productions S→aS | AC | AB A→ϵ C→ϵ B→bB | bb D→c
11
Order of Defect Removal
Eliminate null productions Eliminate unit productions Eliminate non reachable non terminals A grammar in which non terminal fails to generate terminals can not be used and has to be reconstructed.
12
Superset Property Set of CFLs is proper superset of set of regular languages that is every regular language is context free language but vice versa is not true
13
Closure & Non Closure Properties
If L1 and L2 are context free languages then the language L1L2 (obtained by concatenation) is also CFL. If L1 and L2 are context free languages then the language L1 U L2 (obtained by union) is also CFL. If L is a CFL then language L* is also CFL The CFLs are closed over the operation of string reversal The CFLs are closed over the operation of string homomorphism Non Closure properties Intersection Complement Difference
14
Normal Forms for Context Free Grammars
The production system of CFG: A→ (V U ∑)* No restriction on the RHS of the production. Normal form : Standard form for the RHS of a production in CFG Reduces the complexity of the automation Resulting automation is easier to implement. Requisites for Normal Form It should be broad enough so that all the possible productions in CFG should be convertible to it
15
Normal Forms Chomsky Normal Form
In the Chomsky Normal Form, a CFG G can have only two types of the productions: A →a A→BC Advantage : Parse tree is a binary tree Greibach Normal Form In Greibach normal form (GNF) each production is of the form A→ aα where a belongs to ∑ is a terminal and α belongs to V* Advantages: Since each production contains exactly one terminal, hence a string of length k will require exactly k steps for derivation thereby leading to the availability of information in priori. Corresponding PDA will never go for the ϵ transitions making the machine implementation (automation) process of CFG smooth and unambiguous.
16
Pumping Lemma for Context Free Grammars
Let there be a context free language L. There exist a positive integer m such that for any string z belonging to L, if | z | ≥ m, then z can be decomposed as z=uvwxy such that |vwx| ≤ m and |vx| ≥ 1 and for every positive integer k we have uvkwxky L. Like the pumping lemma for regular languages, the pumping lemma for CFL can be used to show that a particular CFL is not regular. Mechanism to applying Pumping Lemma Step 1: Chose the language, say L, which we want to show is not CFL. Step 2: Choose a suitable m. Step 3: Choose a string z belonging to L such that |z| ≥ m. Step 4: Break z to uvwxy such that |vx| ≥ 1. Step 5: Choose a suitable k, such that uvkwxky L. If it is possible to find such a k then it is proved that L is not a CFL.
17
Ogden’s Lemma Let there be a context free language L. There exist an integer m such that for any string z, belonging to L and length ≥ m, and containing any m or more positions as distinguished then there exist strings u, v, w, x and y satisfying the following conditions z = uvwxy the string vx contains at least one distinguished position the string vwx contains not more than m distinguished positions the string w contains at least one distinguished position for every k ≥ 0 and uvkwxkyL Ogden’s lemma is a stronger form of pumping lemma for CFL. It focuses on any n distinguished positions of the string z and guarantees that the strings to pumped must have 1 to n number of distinguished positions. This helps us in identifying the substring v and x which are to be pumped. This is in contrast to the pumping lemma which is silent on the fact on how to locate these substrings in the string z.
18
Applications of Context Free Grammars
Design of Programming language Constructs Design of Natural Language Constructs Mark up Languages
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.