- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# What is context free grammar? Explain with examples

A context free grammar (CFG) is a forma grammar which is used to generate all the possible patterns of strings in a given formal language.

It is defined as four tuples −

**G=(V,T,P,S)**

- G is a grammar, which consists of a set of production rules. It is used to generate the strings of a language.
- T is the final set of terminal symbols. It is denoted by lower case letters.
- V is the final set of non-terminal symbols. It is denoted by capital letters
- P is a set of production rules, which is used for replacing non-terminal symbols (on the left side of production) in a string with other terminals (on the right side of production).
- S is the start symbol used to derive the string

## Example

Construct CFG for the language having any number of a's over the set ∑={a}

## Solution

Regular Expression= a*

Production rule for the Regular Expression is as follows −

S->aS rule 1

S-> ε rule 2

Now if we want to derive a string "aaaaaa" we can start with start symbol

Start with start symbol:

s | rule |

aS | 1 |

aaS | 1 |

aaaS | 1 |

aaaaS | 1 |

aaaaaS | 1 |

aaaaaaS | 1 |

aaaaaa | 2 |

The regular expression=a* can generate a set of strings { ε,a,aa,aaa,...}

We can have a null string because S is a start symbol and rule 2 gives S-> ε

- Related Questions & Answers
- What is Context-Free Grammar?
- Explain about CYK Algorithm for Context Free Grammar
- Explain removing unit productions in context free grammar
- Explain the simplification of context free grammar in TOC
- What is Context-sensitive Grammar?
- Convert the given Context free grammar to CNF
- How to generate the language for context free grammar?
- Generate a CNF for a given context free grammar
- Explain Pumping lemma for context free language
- How to convert context free grammar to push down automata?
- Explain the context free language closure under concatenation?
- Explain about pumping lemma for context free language?
- Explain the context free language closure under union operation?
- What is JavaScript closure? Explain with examples.
- Generate a Context-free grammar for the language L = {anbm| m≠n}?

Advertisements