Sei sulla pagina 1di 21

Context Free Language

CSE, 2nd Year, 4th Semester Copyright to: Abhishek Mukhopadhyay 3/25/2014 7:47:05 PM

Introduction
If we follow the Chomsky classification of language, according the Type-II grammar,

But there is no restriction in the right side. We saw in regular grammar that certain restriction on grammatical forms; eliminating rules of the form and make the arrangement easier.

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

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 child of its parent

More Generally, a derivation tree can be defined with any non-terminal as the root.

Example: S > AB A > aAA A > aA A > a B > bB B > b S A a A A B b a A A aAA

a
yield = aAab 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

yield = aaAA

Observation:Every derivation corresponds to one derivation tree. S => AB => aAAB => aaAB => aaaB => aaab S A a A a A a B b

Observation: Every derivation tree corresponds to one or more derivations.


S => AB => aAAB => aaAB => aaaB => aaab S => AB => Ab => aAAb =>aAab => aaab S => AB => Ab => 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.
6

Observation: Every derivation tree corresponds to exactly one leftmost (and rightmost) derivation.

=> AB => aAAB => aaAB => aaaB => aaab

S A a A a A a B b

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 have more than 1 derivation tree.

Example: Consider the string aaab and the preceding grammar.


S > AB A > aAA A > aA A > a B > bB B > b

S => AB => aAAB => aaAB => aaaB => aaab

S A a A a A a B b

S => AB => aAB => aaAB => aaaB => aaab

S
A a a A A B b

a The string has two left-most derivations, and therefore has two distinct parse trees.
8

Ambiguity in CFG

Context-Free Grammars and Programming Languages


NEXT CLASS is followed by 1. 2. 3. 4. Construction of reduced grammar Elimination of NULL production Elimination of UNIT production Pumping Lemma

Construction of reduced grammar


One 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 a sentence. Removing this production leaves the language unaffected and is a simplification by any definition.

Reduced Grammar

Remove -productions
Algorithm. Remove -productions from grammars for langauges without . 1. Find nonterminals that derive . 2. For each production A w construct all productions A 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.
13

Example
Remove -productions from the grammar S ABc A aA | B bB | . Solution. Step 1: The nonterminals A and B derive . Step 2: From the production S ABc we construct S Bc | Ac | c. From the production A aA we construct A a. From the production B bB we construct B b. Step 3: S ABc | Bc | Ac | c A aA | a B bB | b.

14

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

Dealing with loops


A unit production is a production of the form
A 1 A2

where A1 and A2 are both variables Example


grammar: unit productions: S T

S 0S1 | 1S0S1 | T TS|R| R 0SR

Removal of unit productions


If there is a cycle of unit productions
A1 A2 ... Ak A1

delete it and replace everything with A1


Example
S 0S1 | 1S0S1 | T TS|R| R 0SR

T S 0S1 | 1S0S1 SR| R 0SR R

T is replaced by S in the {S, T} cycle

Removal of unit productions


For other unit productions, replace every chain
A1 A2 ... Ak

by productions A1 ,... , Ak
Example
S 0S1 | 1S0S1 |R| R 0SR S 0S1 | 1S0S1 | 0SR | R 0SR

S R 0SR is replaced by S 0SR, R 0SR

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

Potrebbero piacerti anche