Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Context Free Grammar

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

143

 Context free grammar is a formal grammar which is used to generate all possible strings in a given
formal language.
 Language generated by context free grammar are known as context free language.

 Context free grammar G can be defined by four tuples as:


o G= (V, T, P, S)  
1. G describes the grammar
2. Tor ∑ describes a finite set of terminal symbols.
3. V describes set of variable or a finite set of non-terminal symbols
4. P describes a set of production rules
5. S is the start symbol.
 In CFG, the start symbol is used to derive the string. You can derive the string by repeatedly
replacing a non-terminal by the right hand side of the production, until all non-terminal have
been replaced by terminal symbols.
 Example: L= {wcwR | w € (a, b)*}

Context free grammar has production rule of the form

A a where a=(V U ∑ )* and A€V

 Production rules:
 S → aSa  
 S → bSb  
 S → c  

Now check that abbcbba string can be derived from the given CFG.

 S ⇒ aSa  
 S ⇒ abSba  
 S ⇒ abbSbba  
 S ⇒ abbcbba  
143

By applying the production S → aSa, S → bSb recursively and finally applying the production S →
c, we get the string abbcbba.

Context-free Languages

 In formal language theory, a language is defined as a set of strings of symbols that may be
constrained by specific rules. Similarly, the written English language is made up of groups of
letters (words) separated by spaces. A valid (accepted) sentence in the language must follow
particular rules, the grammar.
 A context-free language is a language generated by a context-free grammar. They are more
general (and include) regular languages. The same context-free language might be generated by
multiple context-free grammars.
 The set of all context-free languages is identical to the set of languages that are accepted
by pushdown automata (PDA).
 Here is an example of a language that is not regular (proof here) but is context-free:
 \{a^nb^n | n \geq 0\}{anbn∣n≥0}. This is the language of all strings that have an equal number of
a’s and b’s.
 In this notation, a^4b^4a4b4 can be expanded out to aaaabbbb, where there are four a’s and
then four b’s. (So this isn’t exponentiation, though the notation is similar).

A context-free grammar (CFG) is a way of describing a language drawn from a useful set of


languages called the Context-free Languages (CFLs).

1. All regular languages are CFLs.


2. Not all CFLs are regular.

 CFLs are useful for describing “nested” structures (e.g., expressions with properly balanced
parentheses) that occur commonly in programming languages but are known to be not regular.

Noam Chomsky has divided grammar into four types :

Type Name
0 Unrestricted Grammar
143

Context Sensitive Grammar


1
Context Free Grammar
2
Regular Grammar
3

Chomsky Hierarchy

1. Context Free Grammar :

 Language generated by Context Free Grammar is accepted by Pushdown Automata


 It is a subset of Type 0 and Type 1 grammar and a superset of Type 3 grammar.
 Also called phase structured grammar.
 Different context-free grammars can generate the same context-free language.
 Classification of Context Free Grammar is done on the basis of the number of parse trees.
 Only one parse tree->Unambiguous.
 More than one parse tree->Ambiguous.

Productions are in the form –

A->B;
A∈N i.e A is a non-terminal.
B∈V*(Any string).

Example –

S –> AB
A –> a
B –> b

2. Regular Grammar :
143

 It is accepted by Finite State Automata.


 It is a subset of Type 0 ,Type 1 and Type 2 grammar.
 The language it generates is called Regular Language.
 Regular languages are closed under operations like Union, Intersection, Complement etc.
 They are the most restricted form of grammar.

Productions are in the form –

V –> VT / T (left-linear grammar)


(or)
V –> TV /T (right-linear grammar)

Example –

1. S –> ab.
2. S -> aS | bS | ∊

Difference Between Context Free Grammar and Regular Grammar:

Parameter Context Free Grammar Regular Grammar

Type Type-2 Type-3

Recognizer Push-down automata. Finite State Automata

Productions are of the form: Productions are of the form:


A->B;  V –> VT / T (left-linear grammar)
Rules
A∈N(Non-Terminal)  (or)
B∈V*(Any string) V –> TV /T (right-linear grammar)

Restriction Less than Regular Grammar More than any other grammar

Right-hand The right-hand side of production has The right-hand side of production should be either left
Side no restrictions. linear or right linear.

Set Property Super Set of Regular Grammar Subset of Context Free Grammar

Intersection of two CFL need not be a


Intersection Intersection of two RG is a RG.
CFL

Complement They are not closed under complement Closed under complement

The range of languages that come The range of languages that come under RG is less
Range
under CFG is wide. than CFG.

Examples S –> AB;A –> a;B –> b S -> aS | bS | ∊


143

You might also like