Skip to content
Related Articles

Related Articles

Improve Article

Difference between Context Free Grammar and Regular Grammar

  • Last Updated : 31 May, 2021

Noam Chomsky has divided grammar into four types :

Type

Name

0

Unrestricted Grammar

1



Context Sensitive Grammar

2

Context Free Grammar

3

Regular Grammar

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 :

  • 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:

ParameterContext Free GrammarRegular Grammar 
TypeType-2Type-3
RecognizerPush-down automata.Finite State Automata
Rules Productions are of the form:
A->B; 
A∈N(Non-Terminal) 
B∈V*(Any string)
Productions are of the form:
V –> VT / T (left-linear grammar)
(or)
V –> TV /T (right-linear grammar)
RestrictionLess than Regular GrammarMore than any other grammar
Right-hand SideThe right-hand side of production has no restrictions.The right-hand side of production should be either left linear or right linear.
Set PropertySuper Set of Regular Grammar Subset of Context Free Grammar
IntersectionIntersection of two CFL need not be a CFLIntersection of two RG is a RG.
ComplementThey are not closed under complement Closed under complement
RangeThe range of languages that come under CFG is wide.The range of languages that come under RG is less than CFG.
ExamplesS –> AB;A –> a;B –> bS -> aS | bS | ∊ 

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :