Context Free Language1
-
Upload
amukhopadhyay -
Category
Documents
-
view
227 -
download
0
Transcript of Context Free Language1
-
8/12/2019 Context Free Language1
1/21
Context Free Language
CSE, 2ndYear, 4thSemester
Copyright to: Abhishek Mukhopadhyay3/25/2014 7:47:05 PM
-
8/12/2019 Context Free Language1
2/21
Introduction
If we follow the Chomsky classification oflanguage, according the Type-II grammar,
But there is no restriction in the right side. Wesaw in regular grammar that certain
restriction on grammatical forms; eliminatingrules of the form and makethe arrangement easier.
-
8/12/2019 Context Free Language1
3/21
Context Free Language
A language L is said to be context-free if and only if
there is a context free grammar G such that L = L (G).Examples of Context-Free Languages
The grammar G = ({S} , {a, b}, S, P), with productions
S > aSa, S -> bSb, S >A,
is context-free. A typical derivation in this grammar is
S =t> aSa => aaSaa => aabSbaa => aabbaa.
The language is context-free, but this is not regular
-
8/12/2019 Context Free Language1
4/21
Parse Tree
Definition: Let G = (V, T, P, S) be a CFG. A tree is a
derivation (or parse) tree if:
Every vertex has a label from V U T U {}
The label of the root is S
If a vertex with label A has children with labels X1, X2,, Xn, from
left to right, then
A> X1, X2,, Xn
must be a production in P
If a vertex has label , then that vertex is a leaf and the only childof its parent
More Generally, a derivation tree can be defined with any
non-terminal as the root.
-
8/12/2019 Context Free Language1
5/21
5
Example:
S> AB S A
A> aAA
A> aA A B a A
A> a
B> bB a A A b aAA
B> ba
yield = aAab yield = aaAA
Notes:
Root can be any non-terminal
Leaf nodes can be terminals or non-terminals
A derivation tree with root S shows the productions used to obtain a sentential form
-
8/12/2019 Context Free Language1
6/21
6
Observation:Every derivation corresponds to one derivation tree.
S => AB S
=> aAAB
=> aaAB A B
=> aaaB
=> aaab a A A b
a a
Observation:Every derivation tree corresponds to one or more derivations.
S => AB S => AB S => AB
=> aAAB => Ab => Ab
=> aaAB => aAAb => aAAb
=> aaaB =>aAab => aaAb
=> aaab => aaab => aaab
Definition:A derivation is leftmost (rightmost)if at each step in the derivation a
production is applied to the leftmost (rightmost) non-terminal in the sentential form.
The first derivation above is leftmost, second is rightmost, the third is neither.
-
8/12/2019 Context Free Language1
7/21
7
Observation: Every derivation tree corresponds to exactly one leftmost (and
rightmost) derivation.
S => AB S
=> aAAB
=> aaAB A B
=> aaaB
=> aaab a A A b
a a
Observation: Let G be a CFG. Then there may exist a string x in L(G) that has
more than 1 leftmost (or rightmost) derivation. Such a string will also havemore than 1 derivation tree.
-
8/12/2019 Context Free Language1
8/21
8
Example:Consider the string aaab and the preceding grammar.
S> AB S => AB S
A> aAA => aAAB
A> aA => aaAB A BA> a => aaaB
B> bB => aaab a A A b
B> b
a a
S => AB S
=> aAB
=> aaAB A B
=> aaaB
=> aaab a A b
a A
a
The string has two left-most derivations, and therefore has two distinct parse trees.
-
8/12/2019 Context Free Language1
9/21
-
8/12/2019 Context Free Language1
10/21
Context-Free Grammars and
Programming Languages
NEXT CLASS is followed by
1. Construction of reduced grammar
2. Elimination of NULL production3. Elimination of UNIT production
4. Pumping Lemma
-
8/12/2019 Context Free Language1
11/21
Construction of reduced grammarOne invariably wants to remove productions from a grammar that can never take part
in any derivation. For example, in the grammar whose entire production set is
the production S > A clearly plays no role, as A cannot be transformed into a
terminal string. While A can occur in a string derived from S, this can never lead to asentence. Removing this production leaves the language unaffected and is a
simplification by any definition.
-
8/12/2019 Context Free Language1
12/21
Reduced Grammar
http://localhost/var/www/apps/conversion/tmp/scratch_9/Reduced_grammar.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_9/Reduced_grammar.docx -
8/12/2019 Context Free Language1
13/21
13
Remove -productions
Algorithm. Remove -productions from grammars for
langauges without .
1. Find nonterminals that derive .
2. For each productionA w construct all productionsA w where w is obtained from
w by removing one or more occurrences of the
nonterminals from Step 1.3. Combine the original productions with those of step
2 and eliminate any -productions.
-
8/12/2019 Context Free Language1
14/21
14
Example
Remove -productions from the grammar
S ABc
A aA |
B bB | .
Solution.
Step 1: The nonterminalsA and B derive . Step 2: From the production S ABc we construct S Bc | Ac | c.
From the productionA aA we constructA a.
From the production B bB we construct B b.
Step 3: S ABc | Bc | Ac | c
A aA | aB bB | b.
-
8/12/2019 Context Free Language1
15/21
15
Problem(Remove -productions)
Remove -productions from
S ABc | Ab | c
A ABa |
B Bbc | .
Solution.
S ABc | Ab | c| Bc | Ac | b
A ABa | Ba | Aa | a
B Bbc | bc.
-
8/12/2019 Context Free Language1
16/21
Dealing with loops
A unit productionis a production of the form
whereA1andA2are both variables Example
A1 A2
S 0S1 | 1S0S1 | TT S | R |
R 0SR
grammar: unit productions:
S T
R
-
8/12/2019 Context Free Language1
17/21
Removal of unit productions
If there is a cycle of unit productions
delete it and replace everything withA1
Example
A1 A2 ... Ak A1
S 0S1 | 1S0S1 | T
T S | R |
R 0SR
S T
R
S 0S1 | 1S0S1
S R |
R 0SR
Tis replaced by Sin the {S, T}cycle
-
8/12/2019 Context Free Language1
18/21
Removal of unit productions
For other unit productions, replace every chain
by productionsA1
,... , Ak
Example
A1 A2 ... Ak
S R 0SRis replaced by S 0SR, R 0SR
S 0S1 | 1S0S1
| R |
R 0SR
S 0S1 | 1S0S1
| 0SR|
R 0SR
-
8/12/2019 Context Free Language1
19/21
-
8/12/2019 Context Free Language1
20/21
Chomsky Normal Form
A Context free grammar is in CNF if every
production is in form, Aa or ABC and S
, L(G).
Construction of CNF
First remove and unit production
Then construct according to the above
mentioned production rules
-
8/12/2019 Context Free Language1
21/21