0% found this document useful (0 votes)
184 views5 pages

Context Free Grammar

A context-free grammar is a formal grammar used to generate strings in a formal language. It consists of variables, terminals, production rules, and a start symbol. Context-free languages include regular languages and describe structures like balanced parentheses. Chomsky hierarchy classifies grammars from unrestricted to regular based on generative power.

Uploaded by

Aashu Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views5 pages

Context Free Grammar

A context-free grammar is a formal grammar used to generate strings in a formal language. It consists of variables, terminals, production rules, and a start symbol. Context-free languages include regular languages and describe structures like balanced parentheses. Chomsky hierarchy classifies grammars from unrestricted to regular based on generative power.

Uploaded by

Aashu Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy