Types of Grammars:: Grammar
Types of Grammars:: 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
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, # }
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
Example
Write the Derivation process of the string: 0010100
S → 0S0 | 1S1 | 0 | 1
7
Derivation Order:
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