CONTEXT FREE
LANGUAGE
.
AUTOMATA THEORY 1
Context free grammar and context free language
• In formal language theory, a context free language is a language generated by
some context free grammar
• The set of all CFL is identical to the set of language accepted by pushdown
automata
• Context free grammar is defined by 4 tuples as G = {V, ∑, S, P} where
• V = set of variable or non terminal symbol
• ∑ = set of terminal symbol
• S = start symbol
• P = production rule
AUTOMATA THEORY 2
Con…
• Context free grammar has production rule of the form
A → ā where ā = {V ꓴ ∑}* and AєV
Example: for generating a language that generates equal number of a’s and b’s
In the form aⁿbⁿ , the context free grammar can be defined as
G = {S,A), (a,b), (A→aAb|є)}
S→aAb
→aaAbb (by S→ aAb production)
→aaaAbbb (by same production rule with above )
→aaabbb (by A → є)
AUTOMATA THEORY 3
Ambiguous grammar
• A grammar is said to be ambiguous if there exist two or more
derivation tree for string w (that means two or more left derivation
tree )
• Example: G= {{S},{a+b,+,*},p,S} where p consist of S→S+S|S*S|a|b
• The string a+a*b can be generated as
• S→ S+S S → S*S
→ a+S → S+S*S
→ a+S*S → a+S*S
→ a+a*S → a+a*S
a+a*b a+a+b
AUTOMATA THEORY 4
Derivation of context free grammar
• Context free grammar
• E→ E+E|E*E|id supposed we want to derivate id+id*id
• E → E+E
• E → E*E
• E →id
• Left most derivation right most derivation parse tree derivation
E → E+E E →E*E E
→ id+E E+E*E E + E
→ id+E*E E+E*id
→ id+id*E E+id*id id E * E
id+id*id id+id*id id id
AUTOMATA THEORY 5
Con..
• Example1: Determine whether the following is ambiguous grammar or not
• G: S→(S)|SS|є
• Use parse tree derivation ()()()
S S
S S S S
( S ) S S S S ( S )
є ( S ) ( S ) ( S ) ( S ) є
є є є є
AUTOMATA THEORY 6
Con…
• Example2
Determine whether the following grammar is ambiguous or not
• G1: S→SbS|a
• G2: S→aB|ab|, A→AB|a, B→Abb|b
Use the string ababa
AUTOMATA THEORY 7