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

Types of Grammars:: Grammar

The document discusses context free grammars and different types of grammars. It provides examples of context free grammars that generate languages like (a+b)*, (a+b)+, anbn and others. It defines key concepts like productions, terminals, nonterminals, derivation and leftmost and rightmost derivations. In particular: 1. A context free grammar consists of an alphabet of terminals, nonterminals including a start symbol S, and productions of the form A->α where A is a nonterminal and α is a string of terminals and/or nonterminals. 2. The language generated is the set of strings of terminals that can be derived from the start symbol S using the productions.
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)
57 views10 pages

Types of Grammars:: Grammar

The document discusses context free grammars and different types of grammars. It provides examples of context free grammars that generate languages like (a+b)*, (a+b)+, anbn and others. It defines key concepts like productions, terminals, nonterminals, derivation and leftmost and rightmost derivations. In particular: 1. A context free grammar consists of an alphabet of terminals, nonterminals including a start symbol S, and productions of the form A->α where A is a nonterminal and α is a string of terminals and/or nonterminals. 2. The language generated is the set of strings of terminals that can be derived from the start symbol S using the productions.
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

GRAMMAR

 Formal language: is a set of words, that is, finite strings of symbols taken
from the alphabet, recognized by Automata and generated by Grammars.
 A grammar: is a list of rules which can be used to produce or generate all
the strings of a language, and which does not generate any strings which
are not in the language.
 Languages that are generated by context-free grammars are context-free
languages

Types of Grammars :
According to Noam Chomosky, there are four types of grammars − Type
0, Type 1, Type 2, and Type 3 are summarized as follow:

Type 3
 Regular grammars: A→ a , A→aB where (A,B) is nonterminal ,(a)
is terminal
 A single nonterminal on the left- hind side.
 A single terminal, followed or preceded by a single nonterminal.

Example
X → aY | X → a
Y→b
Or
X → Ya | X → a
Y→b

Type-2
 Generate the Context Free Language
 The productions must be in the form A → ∝
 where A ∈ N (Nonterminal) and ∝ ∈ (T∪N)* (String of terminals and
nonterminals).
 These languages generated by these grammars are be recognized by a
non-deterministic pushdown automaton.

Example:
S → X a X → a| aX | abc |ε
1
Type-1
 generate context-sensitive languages.
 The productions must be in the form αAβ → αγβ
 where A∈ N (nonterminal) and α,β,γ∈ (T ∪ N)* (Strings of terminals and
nonterminals)
 The strings α and β may be empty, but γ must be non-empty.
 The languages generated by these grammars are recognized by a linear
bounded automaton.

Example
AB → AbBc A → bcA B → b

Type-0
 The productions have no restrictions.
 They generate the languages that are recognized by a Turing machine.
 The productions can be in the form of α → β where α is a string of
terminals and non-terminals with at least one non-terminal and α cannot
be null.
 β is a string of terminals and non-terminals.

Example:
S → ACaB Bc → acB CB → DB aD → Db

2
CONTEXT FREE GRAMMAR

A context free grammar, called CFG, is a collection of three things:


1. An alphabet Σ of letters called terminals from which we are going to
make strings that will be the words of a language.
2. A set of symbols called nonterminals, one of which is the symbol S,
standing for "start here".
3. A finite set of production of the form:
One nonterminal → finite string of terminals and/ or nonterminals. Where
the strings of terminals and nonterminals can consist of only terminals or of
any nonterminals, or any mixture of terminals and nonterminals or even the
empty string. We require that at least one production has the nonterminal S as
its left side.

Definition
The language generated by the CFG is the set of all strings of terminals
that can be produced from the start symbol S using the production as
substitutions. A language generated by the CFG is called a context free
language (CFL).

Example
Let the only terminal be a.
Let the only nonterminal be S.
Let the production be:
S → aS
S→ε
The language generated by this CFG is exactly a*.
In this language we can have the following derivation:
S → aS → aaS → aaaS → aaaaS → aaaaaS → aaaaa ε = aaaaa

Example
Let the only terminal be a.
Let the only nonterminal be S.
Let the production be:
3
S → SS
S→a
S→ε
The language generated by this CFG is also just the language a*.
In this language we can have the following derivation:
S → SS → SSS → SaS → SaSS → ε aSS → ε aaS → ε aa ε = aa

Example
Let the terminals be a, b. And the only nonterminal be S.
Let the production be:
S → aS
S → bS
S→a
S→b
The language generated by this CFG is (a+b)+.
In this language we can have the following derivation:
S → bS → baS → baaS → baab

Example
Let the terminals be a, b. And the only nonterminal be S.
Let the production be:
S → aS
S → bS
S→ε
The language generated by this CFG is (a+b)*.
In this language we can have the following derivation:
S → bS → baS → baaS → baa ε = baa

Example
Let the terminals be a, b,Λ. And the nonterminals be S,X,Y.
Let the production be:
S→X
S→Y
X→ε
Y → aY
Y → bY
Y→a
Y→b
The language generated by this CFG is (a+b)*.

4
Example
Let the terminals be a, b, ε. And the nonterminals be S,X,Y.
Let the production be:
S → XY
X→ε
Y → aY
Y → bY
Y→a
Y→b
The language generated by this CFG is (a+b)+.

Example
Let the terminals be a, b.
Let the nonterminals be S,X.
Let the production be:
S → XaaX
X → aX
X → bX
X→ε
The language generated by this CFG is (a+b)* aa(a+b)*.
To generate baabaab we can proceed as follows:
S→XaaX→bXaaX→baXaaX→baaXaaX→baabXaaX→baabΛaaX=baabaaX
→baabaabX→baabaab ε =baabaab

Example
Let the terminals be a, b.
Let the nonterminals be S,X,Y.
Let the production be:
S → XY
X → aX
X → bX
X→a
Y → Ya
Y → Yb
Y→a
The language generated by this CFG is (a+b)* aa(a+b)*.
To drive babaabb:
S→XY→bXY→baXY→babXY→babaY→babaYb→babaYbb→babaabb

5
Example
Let the terminals be a, b.
Let the nonterminals be S,BALANCED,UNBALANCED.
Let the production be:
S → SS
S → BALANCED S
S → S BALANCED
S→ε
S → UNBALANCED S UNBALANCED
BALANCED→ aa
BALANCED→ bb
UNBALANCED → ab
UNBALANCED → ba
The language generated by this CFG is even-even.

Example
Let the terminals be a, b.
Let the nonterminals be S,A,B.
Let the production be:
S → aB
S → bA
A→a
A → aS
A → bAA
B→b
B → bS
B → aBB
The language generated by this CFG is the language EQUAL of all strings that
have an equal number of a's and b's.

Example
The following Grammar G1consists of
A → 0A1
A→B
B→#
Or
A → 0A1 | B
B→#
6
Productions: or collection of substitution rules
nonterminals : (including the start variable),typically upper-case
Terminals: typically lower-case, other symbols

start variable : A
Nonterminals: {A,B}
Terminals: {0, 1, # }

use grammar to generate each string of that language in the following


manner :
1.Write down the start symbol. It is the symbol on the left-hand side of
the top rule.
2.Find a symbol that is a rule starts with it and Replace it with the right-
hand side of that rule.
3. Repeat step 2 until no nonterminal remain.

Derivation
Derivation: is a sequence of rewriting rules, applied to the starting
nonterminal, ending with a string of terminals.

Example
Derivation of string 000#111 in previous example

Solution : A → 0A1 → 00A11 → 000A111 →000B111 → 000#111

Example
Write the Derivation process of the string: 0010100
S → 0S0 | 1S1 | 0 | 1

Solution : S → 0S0 → 00S00 → 001S100 → 0010100

7
Derivation Order:

 Derivation is leftmost if the nonterminal replaced in every step is the


leftmost nonterminal.
 Derivation is rightmost if the nonterminal replaced in every step is the
rightmost nonterminal.

Example
S → aAB
A → bBb
B →A|ε
Solution :

1. Leftmost derivation :
S → aAB → abBbB → abAbB → abbBbbB → abbbbB → abbbb

2. Rightmost derivation :
S → aAB → aA → abBb → abAb → abbBbb → abbbb

Example:
Consider the grammar G = ({ S, A, B, C } , {a, b, c } , S , P )
Where P :
S → ABC
A → aA | ε
B → bB | ε
C → cC | ε
Derivation abbc

Solution :

1. Leftmost derivation :
S → ABC → aABC → aBC → abBC → abbBC → abbC → abbcC → abbc

2. Rightmost derivation :
S →ABC → ABcC → ABc → AbBc → AbbBc → Abbc → aAbbc → abbc

8
Example:
Write Grammar that generate this language
L = { anbn where n ≥ 0 }

Solution :
 A context-free grammar :
S → aSb | ε
 A derivation :
S → aSb → aaSbb → aabb
S → aSb → aaSbb → aaaSbbb → aaabbb

Example:
Which language generates the grammar G given by the productions:
S → aSa | aBa
B → bB | b

Solution :
L = { anbman | n > 0 , m > 0 }
The rule S → aSa recursively builds an equal number of a's on each end of the
string. The recursion is terminated by the application of the rule S → aBa,
ensuring at least on e leading and one trailing a . the recursive B rule then
generates any number of b's . to remove the variable B from the string and
obtain a sentence of the language, the rule B → b must be applied, forcing the
presence of at least one b .

Example:
Construct context free grammars to accept the following languages:
L(G)={w | w starts and ends with the same symbol}, Σ = {0, 1}

Solution :
S → 0A0 | 1A1
A → 0A | 1A | ε

Example:
Find a Context-free Grammar for the following language:
L= {an bm ck : k = n + m and n, m ≥1}
Solution :
S → aSc | B
B → bBc | ε
9
Example:
Find a Context-free Grammar for the following language:
L= {w | w is odd and its middle symbol is 0 }
∑={0,1}
Solution :
S → 0 | 0S0 | 0S1 | 1S0 | 1S1

10

You might also like