Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Context-Free Grammar (CFG): Introduction

Lesson 15 of 39 in the free Natural Language Processing notes on Siksha Sarovar, written by Rohit Jangra.

Introduction to Context-Free Grammar (CFG)

A grammar is a set of rules that defines which sequences of words form valid sentences in a language. A Context-Free Grammar (CFG) is the most common formalism used in NLP to describe sentence structure.

Formal Definition

A CFG is a 4-tuple G = (N, Σ, R, S):

ComponentMeaning
NA finite set of non-terminal symbols (e.g. S, NP, VP)
Σ (Sigma)A finite set of terminal symbols — the actual words
RA finite set of production rules of the form A → β
SThe start symbol (usually "S" for Sentence)

"Context-free" means each rule's left-hand side is a single non-terminal, applied regardless of surrounding context.

A Simple English Grammar

S  -> NP VP
NP -> Det N | Det Adj N | 'Riya'
VP -> V NP | V
Det -> 'the' | 'a'
Adj -> 'quick' | 'lazy'
N  -> 'dog' | 'cat' | 'book'
V  -> 'chased' | 'reads'

This grammar can generate (and parse) sentences like:

  • "Riya reads a book"
  • "the quick dog chased the lazy cat"

Building and Testing a CFG in NLTK

import nltk
from nltk import CFG

grammar = CFG.fromstring("""
S -> NP VP
NP -> Det N | Det Adj N | 'Riya'
VP -> V NP | V
Det -> 'the' | 'a'
Adj -> 'quick' | 'lazy'
N -> 'dog' | 'cat' | 'book'
V -> 'chased' | 'reads'
""")

print(grammar.start())        # S
print(grammar.productions())  # list of all production rules

Derivation — Generating a Sentence Step by Step

S
-> NP VP                        (S -> NP VP)
-> Det Adj N VP                 (NP -> Det Adj N)
-> the Adj N VP                 (Det -> 'the')
-> the quick N VP                (Adj -> 'quick')
-> the quick dog VP               (N -> 'dog')
-> the quick dog V NP              (VP -> V NP)
-> the quick dog chased NP          (V -> 'chased')
-> the quick dog chased the lazy cat (NP -> Det Adj N -> ...)

Each line applies exactly one production rule — this sequence is called a derivation, and the tree formed by all the rule applications is a parse tree (covered in the next lesson).

Why CFGs Cannot Capture Everything

CFGs handle hierarchical, recursive structure well (e.g. "the book [that Riya read] [that won an award]") but cannot naturally express certain context-sensitive agreement rules, such as subject-verb number agreement ("the dog runs" vs "the dogs run"), without exploding the rule set with separate singular/plural versions of every rule. This is one motivation for more expressive grammar formalisms (e.g. unification grammars) used in advanced NLP systems — beyond the scope of this course, but good to be aware of.

CFG vs Regular Grammar

Regular GrammarContext-Free Grammar
Can representSimple patterns (used by regex, Unit 1)Nested/recursive structures
Example it CAN handleabBalanced parentheses (()())
Used forTokenization, lexical patternsSentence-level syntax, parsing

CFGs are the theoretical foundation for the parsing algorithms — top-down, bottom-up, and CYK — that we study next.