0% found this document useful (0 votes)
12 views10 pages

Context Free Grammer

The document provides an overview of Context-Free Grammar (CFG), defining its components and importance in language syntax. It includes examples of CFGs for arithmetic expressions, balanced parentheses, and palindromes, as well as explanations of derivation and parse trees. The conclusion emphasizes the significance of CFGs in programming languages and formal languages.

Uploaded by

johnthedon1729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Context Free Grammer

The document provides an overview of Context-Free Grammar (CFG), defining its components and importance in language syntax. It includes examples of CFGs for arithmetic expressions, balanced parentheses, and palindromes, as well as explanations of derivation and parse trees. The conclusion emphasizes the significance of CFGs in programming languages and formal languages.

Uploaded by

johnthedon1729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Context-Free Grammar (CFG)

Definition, Components, and Examples

Dr. Mayank Kumar Jain

Amity University Rajasthan, Jaipur

February 18, 2025

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 1 / 10
Outline

1 Introduction

2 Components of CFG

3 Examples of CFG

4 Derivation in CFG

5 Parse Trees

6 Conclusion

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 2 / 10
What is a Context-Free Grammar?

A Context-Free Grammar (CFG) is a formal grammar used to


describe the syntax of languages.
It consists of a set of production rules that define how symbols can
be replaced.
CFGs are widely used in programming languages, compilers, and
natural language processing.

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 3 / 10
Components of a CFG

A CFG is defined by the 4-tuple G = (V , T , P, S), where:


V : A finite set of non-terminal symbols.
T : A finite set of terminal symbols (disjoint from V ).
P: A finite set of production rules of the form A → α, where A ∈ V
and α ∈ (V ∪ T )∗ .
S: The start symbol (S ∈ V ).

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 4 / 10
Example 1: Arithmetic Expressions

CFG for simple arithmetic expressions:

E →E +T |T
T →T ∗F |F
F → (E ) | id

Explanation:
E : Expression
T : Term
F : Factor
id: Represents identifiers (e.g., variables or numbers).

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 5 / 10
Example 2: Balanced Parentheses

CFG for balanced parentheses:

S → (S) | SS | ε

Explanation:
S: Start symbol.
ε: Represents the empty string.
This grammar generates strings like (), (()), ()(), etc.

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 6 / 10
Example 3: Palindrome Language

CFG for palindromes over {a, b}:

S → aSa | bSb | a | b | ε

Explanation:
S: Start symbol.
This grammar generates palindromes like a, aba, abba, etc.

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 7 / 10
Derivation in CFG

Derivation is the process of generating a string from a CFG by applying


production rules.
Leftmost Derivation: Replace the leftmost non-terminal first.
Rightmost Derivation: Replace the rightmost non-terminal first.
Example:

S → aSb | ab

Leftmost Derivation for aabb:

S ⇒ aSb ⇒ aabb

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 8 / 10
Parse Trees
Parse Tree:
A tree representation of the derivation of a string.
The root is the start symbol, and the leaves are terminal symbols.
Example:
S → aSb | ab | ε
Parse Tree for aabb:
S

a S b

a S b

ε
Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free
Jaipur) Grammar (CFG) February 18, 2025 9 / 10
Conclusion

Context-Free Grammars are essential for defining the syntax of


programming languages and other formal languages.
They consist of non-terminals, terminals, production rules, and a start
symbol.
Examples include arithmetic expressions, balanced parentheses, and
palindromes.

Dr. Mayank Kumar Jain (Amity University Rajasthan,Context-Free


Jaipur) Grammar (CFG) February 18, 2025 10 / 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy