Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Context Free Grammar

If a formal grammar’s production rules can be used regardless of the context of a nonterminal, it is said to be “context free.” The single nonterminal on the left side can always be replaced with the right side, regardless of the symbols that are around it. Because of this, it differs from context-sensitive grammar.

In this article, we will look more into Context Free Grammar according to the GATE Syllabus for (Computer Science Engineering) CSE. We will read ahead to find out more about it.

Table of Contents

What is Context Free Grammar?

A formal grammar called context free grammar (CFG) is used to produce every conceivable string in a given formal language.

Four tuples are used to define the context free grammar G:

G = (V, T, P, S)


  • G refers to a grammar that consists of sets of various production rules. We use it to generate a language’s strings.
  • T refers to the terminal symbol’s final set. Lower case letters are used to denote it.
  • V refers to the nonterminal symbol’s final set. Capital letters are used to denote it.
  • P refers to a set of production rules that can be used to replace the nonterminal symbols (on the production’s left side) in a string along with other terminals (present on the production’s right side).
  • S refers to the start symbol that is used to derive the string.

The start symbol is used in CFG to derive the string. This string can be derived by replacing a nonterminal repeatedly by the production’s right-hand side, until and unless the terminal symbols replace all the nonterminals.

Capabilities of CFG

The CFG comes with various capabilities, such as:

  • The majority of programming languages may be described using context-free grammar.
  • An effective parser can be created automatically if the grammar is well designed.
  • It is possible to create grammars that are appropriate for expressions by utilising the associative properties and precedence data.
  • Context-free grammar can describe nested structures, such as balancing parentheses, matching begin-end, related if-then-else statements, and more.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

Also Explore,