A Context-Free Grammar (CFG) is a formal grammar consisting of four components: non-terminal symbols (N), terminal symbols (T), production rules (P), and a starting symbol (S), where production rules follow the format B → α (with B being a non-terminal and α being any combination of terminal and non-terminal symbols). CFGs are accepted by Pushdown Automata and can generate various languages including equal-length strings (like 0^n1^n), repeated patterns (a^n), palindromes, and combinations of different symbols. The three main types of production rules are union rules (S → A | B), closure rules (A → AA), and terminal productions (A → a), which together enable recursive language generation.
Context Free Grammar (CFG) in Automata Theory | Tutorial Part 1
Added:Hello students, welcome back to our class of automata. Now on our last class, we have already discussed about how computer grammar works and we have also discussed what are the different types of grammars available in theory of computer science. Now from the classification of Noam Chomsky we have learned that we have a special type of grammar that is called type two grammar or context free grammar. Now the production rules of any grammar differentiate different types of grammars.
Now in our today's class we are going to discuss about context free grammar. Now in case of context free grammar as we have learned that any grammar is made of NTPS that means non- terminal terminal production rule and starting symbol. Here in context program grammar we also have these four things. So a cfg is also a fouruple thing. Now the production rule for a context grammar is like that B arrow n union T closure. So that means any combination of terminal and non- terminal will be at the right end and B belongs to N whereas in the left side we will have only non- terminals. Okay. So any production of type example is given here a arrow 0 a1 is a context p grammar production rule because here capital a and capital b is our non- terminal where zero and one is our terminal character. Now for this given example we are going to produce a language of type 0 to the power i and 1 to the power i where i greater or equals to zero. That means if there are n number of zero in this language then there will be n number of one in the language. Okay. or maybe there are some null or empty string can also be accepted. Now uh we have also learned in our previous class that a context free grammar is accepted by some PDA or push down automata. Okay.
So for this example if we are going to take one sample language like 0 to the power 2 and 1 to the power 2 then we can make the derivation in such a way that if we here is a is our starting symbol.
So we are starting from this production rule a arrow 0 a1. Okay. So after that we are replacing this a as 0 a1 again and now we are replacing this a by b capital capital b another non- terminal and then we are in the next step we are replacing this capital b by epsylon so our production rule will become 0 to the power 2 and 1 to the^2.
This is a typical example of uh context free grammar. Now how we can make any CFG rules? There are basically three types of rules are available for any CFG. First is union rule where S arrow A or B that means any terminal or non- terminal can be there. production rule where a arrow ab and closer rules that is a arrow a s a here again we are taking another example this is a example of decimal multiplication and division rule now any decimal number uh we can use in here uh maybe we have a decimal number maybe two and we are doing multiplication with three. Uh then we can use this rule. Which rule should I use? We are going to use this rule E arrow E into E. So that means E arrow E into E then E arrow we are replacing this E by I and this E also by I. Now this first E will become sorry for this first I will become 2 because I has a rule 0 1 2 3 any sorry 2 into 3. Okay clear? In the same fashion we can generate any other languages like example given over here a to the power n. Whenever we are making anything like uh a to the power n or b to the power n we are going to uh make a production rule of closure rule.
Okay. So here we are using a s because if we define uh a to the power two maybe uh then we can define like this define it like this S arrow A S. Now S arrow we are replacing this S as A a S.
Now we are keeping these two a intact and replacing this by null s arrow a a or a square. Okay. So in the same fashion we can make languages like this a a bb a a a bb. So uh we are using uh combination of a and b but there will be only uh one non- terminal at a time.
If there are a then there will be no b.
If there is B then there will be no A but any number of A can be generated A a a B b any number of so A to the power N and B to the power N separator. So whenever you are getting two symbols then try to use this property union rule. So uh here I'm using a arrow a or b where a has a rule a a and or uh empty string or b b small b that means uh it's a non- terminal and uh the terminal so in a recursive manner you can generate by this rule in a recursive manner you can generate a to the power n and from the recursive manner uh you can generate b to the power n from this rule in a same fashion if uh we want to generate uh a to the power n comma b to the power n comma c to the power n. So then we will just add another c rule here and uh this will become like this. This will be same only we will add capital C where C will have same rules like C capital C or empty.
Okay. Okay. Now now on this next example we are having this is a very common example you can see multiple of times in your textbook. A B. So here we are using both A and B A to the power N and B to the power N this will work uh in a way that if there are n number of A then there will be n number of B also. So this is same as what we did in case of 0 and one. So this will be our rule. Here we had a rule called a arrow 0 A one and we also added B and B arrow epsylon. Now here I compact mode I used as a arrow a sb or a arrow null.
Okay. And on our next example we are having like this uh ab a number of ab.
So uh this AB will be treated just like a if we can generate a language of a to the power n and our production rule will be uh sorry uh if we use uh a to the power n as our same rule then uh our production rule is s arrow a s then in case of ab it will become ab s. So we can generate the same fashion ab this a will be replaced by ab as many as time you want. Now here is another example where we are using uh a to the power m and b to the power m. Now m and m are both different. So uh you can have uh n m number of n and uh n number of b. So that's why uh on our previous example when we had uh a to the power n and b to the power n then n and n both are same. So if you have 2 a then you will have 2 b also but in but in this case you can have 3 a or you can have 5 b any any number of b or any number of a can be there. So that's why uh we are using this rule uh a arrow a b but this a can be replaced by any number of a a. So our new rule is a arrow null terminal a and a and this b capital b will be replaced by b arrow a. So that means this particular example is generating nothing but a to the power m right whereas this particular example generating b to the power of n right now we are just keeping them together using our production rule.
Okay. So students hopefully I think the concept of context free grammar is very clear to you and uh for your homework I am giving you some further examples also. The rules are given there and you can try them in your home. Okay. So like some rules are given there. Uh context p grammar for odd palindrome context p grammar for event palindrome some uh language generation like a to the power m b to the power n and c to the power k k where we have three different rules. So we are just using it. It is just same as this uh this rule a to the power m and b to the power m where we need to generate uh three symbols here. We had two symbols a and b there. So we had capital a and capital b. So here we have capital A, B and C three symbols. Okay. So on our next class we are going to discuss about uh ambiguity and uh some other aspect of any context grammar. Okay. So you can try these rules in your home by your own. Okay. Thank you.
Up Next

What Is a Derivative? An Introduction to Calculus 1 Concepts
@MathAndScience
213.5K views•2016-02-04

Gain Recalibration in Hippocampal Path Integration: Math Theory
@1024kyz
144 views•2020-07-02

Fourier Series Introduction: The Big Idea Explained
@DrTrefor
387K views•2021-05-03

The Mathematical Impossibility of Accurate World Maps
@Vox
23.3M views•2016-12-02
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Mathematics












































