Formal definition of Context-Free Grammer
Here’s the formal definition of a Context-Free Grammar (CFG).
Definition
A Context-Free Grammar is a 4-tuple:
where:
-
— A finite set of nonterminal symbols (or variables).
-
— A finite set of terminal symbols, such that
-
— A finite set of production rules of the form
where and (that is,
is any finite string of terminals and/or nonterminals, possibly empty). -
— A distinguished element of , called the start symbol.
✅ Key condition that makes it “context-free”:
In every production , the left-hand side consists of exactly one nonterminal symbol .
Characteristics of CFGs
-
Each production’s LHS must be a single nonterminal (unlike context-sensitive grammars).
-
They can describe nested and recursive structures, which are impossible for regular grammars.
-
Languages generated by CFGs are called Context-Free Languages (CFLs).
Example
Let:
-
-
-
-
is the start symbol.
This grammar generates strings with equal numbers of a
’s followed by b
’s:
Derivation example:
Parse Trees
A parse tree shows how a string is generated from the start symbol.
For the earlier grammar
A Worked Example
Problem: Create a CFG for
Solution:
Derivation for "abba"
:
Comments
Post a Comment