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

Unit 3-1

A Context-Free Grammar (CFG) is a formal system that describes context-free languages using a set of production rules, variables, and terminals. It allows for the generation of strings through recursive replacement and is more expressive than regular languages. However, CFGs have limitations, such as ambiguity and an inability to check the meaning of strings.

Uploaded by

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

Unit 3-1

A Context-Free Grammar (CFG) is a formal system that describes context-free languages using a set of production rules, variables, and terminals. It allows for the generation of strings through recursive replacement and is more expressive than regular languages. However, CFGs have limitations, such as ambiguity and an inability to check the meaning of strings.

Uploaded by

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

What is Context-Free Grammar?

A grammar consists of one or more variables that represent classes of strings (i.e., languages) . There
are rules that say how the strings in each class are constructed. The construction can use :

 Symbols of the alphabet

 Strings that are already known to be in one of the classes

 Or both

Context-Free Grammar

A context-free grammar (CFG) is a formal system used to describe a class of languages known
as context-free languages (CFLs). Purpose of context-free grammar is:

 To list all strings in a language using a set of rules (production rules).

 It extends the capabilities of regular expressions and finite automata.

A GFG (or just a grammar) G is a tuple G = (V, T, P, S) where

 V is the (finite) set of variables (or non terminals or syntactic categories). Each variable
represents a language, i.e., a set of strings

 T is a finite set of terminals, i.e., the symbols that form the strings of the language being
defined

 P is a set of production rules that represent the recursive definition of the language.

 S is the start symbol that represents the language being defined. Other variables represent
auxiliary classes of strings that are used to define the language of the start symbol.

A grammar is said to be the Context-free grammar if every production is in the form of:

G -> (V∪T)* , where G ∊ V

 V (Variables/Non-terminals): These are symbols that can be replaced using production rules.
They help in defining the structure of the grammar. Typically, non-terminals are represented
by uppercase letters (e.g., S, A, B).

 T (Terminals): These are symbols that appear in the final strings of the language and cannot
be replaced further. They are usually represented by lowercase letters (e.g., a, b, c) or specific
symbols.

 The left-hand side can only be a Variable, it cannot be a terminal.

 But on the right-hand side here it can be a Variable or Terminal or both combination of
Variable and Terminal.

The above equation states that every production which contains any combination of the 'V' variable
or 'T' terminal is said to be a context-free grammar.

Core Concepts of CFGs

A CFG is defined by:


 Nonterminal symbols (variables): Represent abstract categories or placeholders
(e.g., E,SE,S).

 Terminal symbols (alphabet): The actual characters or tokens in the language


(e.g., a, b,+,∗,(,)a, b, +, *, (, )a, b,+,∗,(,)).

 Production rules: Specify how non terminals can be replaced with other non terminals or
terminals
(e.g., E→E+EE → E + EE→E+E).

 Start symbol: A special nonterminal from which derivations begin.

CFG vs. Other Models

Model Description

Finite Automata Accept strings via computation (accept/reject).

Regular
Match strings by describing their structure.
Expressions

CFG Generate strings via recursive replacement.

Example: Arithmetic Expressions

Suppose we want to describe all legal arithmetic expressions using addition, subtraction,
multiplication, and division.

Here is one possible :

Production Rules:

CFG:
E → int
E → E Op E
E → (E)
Op → +
Op → -
Op → *
Op → /

Example Derivation:

⇒ E Op E
E

⇒ E Op int
⇒ int Op int
⇒ int / int
Designing a CFG

When creating CFGs:

1. Base case: Define the simplest valid strings.

2. Recursive rules: Combine smaller components into larger ones.

Examples:

1. Palindromes over {a, b}:

S → ε | a | b | aSa | bSb

2. Balanced Parentheses:

S → ε | (S) | SS

Languages Defined by CFGs

The language L(G) generated by a CFG G is: L(G)={ ω∈Σ*∣S⇒∗ω}

 ω: Strings made of terminals.

 S⇒∗ω: S derives ω via zero or more production applications.

Regular Languages vs. Context-Free Languages

Property Regular Languages Context-Free Languages

Power Limited More expressive

Memory
Finite Unbounded recursion
Requirements

Definable Simple patterns (e.g., Nested structures (e.g., palindromes,


Structures repetition) balanced parentheses)

Non-CFG Example

Productions such as:

a->bSa, or
a->ba is not a CFG as on the left-hand side there is a terminal which does not follow the CFGs rule.

But we can construct it by :

Lets consider the string "aba" and and try to derive the given grammar from the productions given.
We start with symbol S, apply production rule S->bSa and then (S->a) to get the string "aba".
Parse tree of string "aba"

In the computer science field, context-free grammars are frequently used, especially in the areas of
formal language theory, compiler development, and natural language processing. It is also used for
explaining the syntax of programming languages and other formal languages.

Limitations of Context-Free Grammar

 Cannot Handle Everything :

o CFGs are good for defining basic rules of a language, but they can’t handle
everything.

o Some rules in English or programming languages are too complex for CFG.

 Can Be Confusing (Ambiguity)

o Sometimes, CFG can allow more than one meaning for the same sentence or code.

o This is called ambiguity, and it makes it hard for the computer to understand the
correct meaning.

 Can’t Check Meaning

o CFGs only look at the structure, not the meaning.

o They can’t check if the types match, if variables are used properly, or if functions are
called correctly.

You might also like