Module-3 notes
Module-3 notes
G=(V, T, P, S)
where,
• V is a finite set of variables, also called sometimes non-terminals. Each
variable represents a language i.e., a set of strings.
• T is a finite set of symbols that form the strings of the language being
defined. We call this alphabet the terminals or terminal symbols.
• S is the start symbol, is one of the variables represents the language being
defined.
• There is a finite set of productions or rules that represent the recursive
definition of a language. Each production consists of:
a) A variable that is being defined by the production. This variable is
often called the head of the production.
b) The production symbol →
c) A string of zero or more terminals and variables. This string, called
the body of the production, represents one way to form strings in the
language of the variable of the head.
Let us derive the sentence id+id*id from the grammar for arithmetic expression:
E => E+E
=>id+E
=>id+E*E
=>id+id*E
=>id+id*id
Leftmost and Rightmost Derivations
LMD:
In order to restrict the number of choices we have in deriving a string, it is often
useful to require that at each step we replace the leftmost variable by one of its
production bodies. Such a derivation is called a leftmost derivation, and we
indicate that a derivation is leftmost by using the relations and , for one or
many steps, respectively.
RMD:
Similarly, it is possible to require that at each step the rightmost variable is
replaced by one of its bodies. If so, we call the derivation rightmost and use the
Ex: Consider the string w=id+id*id. Let us derive w using LMD and RMD as
follows:
LMD:
E E+E
id+E
id+E*E
id+id*E
id+id*id
RMD:
E E+E
E+E*E
E+E*id
E+id*id
id+id*id
Sentential Forms:
Examples:
Design Context free grammar for the given language:
1. Construct the CFG for the language having any number of a's over the set
∑= {a}.
Ans :
S→aS | ε
Therefore, G=(V,T,S,P)
where V={S}, T={a}, P={S→aS | ε}, S is the start symbol.
2. Construct a CFG for the regular expression (0+1)*
Ans: S→0S | 1S | ε
Therefore, G=(V,T,S,P)
where V={S}, T={0,1}, P={S→0S | 1S | ε}, S is the start symbol.
3. Construct a CFG for a language L = {wcwR | where w € (a, b)*}. Also
derive the string “abbcbba”
Ans:
S→aSa |bSa |c
Derivations:
S => aSa
S => abSba //S→bSb
S =>abbSbba //S→bSb
S =>abbcbba //S→c
Parse Trees
There is a tree representation for derivations that has proved extremely useful.
This tree shows us clearly how the symbols of a terminal string are grouped into
substrings, each of which belongs to the language of one of the variables of the
grammar.
L→L, S | S
a. (a,a)
c. (a, ((a,a),(a,a)))
Ambiguous Grammars:
Q. What do you mean by ambiguous grammar? Explain with
example.
A CFG is ambiguous if it produces more than one parse tree for a string in the
language. – i.e. there exists any string in the language that is the yield of two or
more parse trees. In other way, if we are able to derive any string of a grammar
either by applying LMD two or more time or applying RMD two or more time
we call such grammar as Ambiguous grammar.
Example:
1. A good example for ambiguous grammar is the grammar for arithmetic
expressions:
Since we got 2 different parse trees and LMD’s we say that given grammar is
ambiguous.
Example 1:
There are two causes of ambiguity in the grammar of arithmetic expression,
Inherent Ambiguity
A context free language L is said to be inherently ambiguous if all its grammars
are ambiguous. If even one grammar for L is unambiguous, then L is an
unambiguous language.
Consider the Language,
PUSHDOWN AUTOMATA
The context free languages have a type of automaton that defined them. This
automaton, called a “pushdown automaton”, is an extension of the nondeterministic finite
automaton with ε – transitions, which is one of the ways to define the regular languages.
The pushdown automaton is essentially an ε – NFA with the addition of a stack. The
stack canbe read, pushed and popped only at the top, just like the “stack” data structure.
We define two different versions of the pushdown automaton: one that accepts by
entering an accepting state, like finite automata do and another version that accepts by
emptying its stack, regardless of the state it is in. we show that these two variations accept
exactly the context free languages i.e. grammars can be converted to equivalent pushdown
automata and vice-versa.
Finite
Input Accept/reject
state
control
Stack
Figure 6.1: A Pushdown automata with stack
❖ As shown in the Figure 6.1, PDA has finite state control that reads inputs, one symbol
at a time. The pushdown automaton is allowed to observe the symbol at the top of
the stack and to base its transition on its current state, input symbol, and the symbol
at the top of stack.
Definition:
Method 2:
Converting CFG to PDA by final State (Suitable for CFG in GNF form)
Algorithm:
[For GNF, all productions must be of the form, A→aα, where aϵT and
αϵV*]
Step2: Push Start symbol of the grammar to the stack and change state from q0
to q1. The transition is:
Step3: For each production of the form, A→aα, where aϵT and αϵV*, add
transitions,
δ(q1, a, A)=(q1, α)
S→aSSS | a
Ans:
Step3:
S→aSSS δ(q1, a, S)=(q1, SSS)
S→a δ(q0, a, S)=(q1, ϵ)
******************************************************