Context Free Language
Context Free Language
1
An informal example
Language of palindromes: L p a l
A palindrome is a string that reads the same forward and
backward Ex: otto, madamimadam , 0110, 11011, ǫ
• What is a CFG?
• Informally, a CFG is a set of rules for deriving (or generating) strings (or
sentences) in a language.
3
Context-Free Languages
• The class of context-free languages generalizes over the class of regular languages, i.e.,
every regular language is a context-free language.
• The reverse of this is not true, i.e., every context-free language is not necessarily
regular. For example, as we will see {0k1k | k≥0} is context-free but not regular.
• Many issues and questions we asked for regular languages will be the same for context-
free languages:
4
Different Kinds of Automata
Automata are distinguished by the temporary memory
results to
The solution of more computational
problems
Context Free Languages
7
Pushdown Automata
8
Pushdown Automata
9
10
Context Free Grammars
We have just studies Context Free Grammar (CFG)
Context free as sentence can be generated in any sequence
On the left hand side of production rules we have a non-terminal and on the
right hand side we have a sequence of terminal and non-terminals.
The language generated by G is the set of all possible sentences that may be
generated from the start symbol S.
Context-free grammars are important because they are powerful enough to
describe the syntax of programming languages.
almost all programming languages are defined via context-free grammars
11
Context Free Grammars
CFG is a collection of the followings
1. An alphabet ∑ of letters called terminals from
which the strings are formed, that will be the
words of the language.
2. A set of symbols called non-terminals, one of
which is S, stands for “start here”.
3. A finite set of productions of the form
non-terminal finite string of terminals and
/or non-terminals.
• The terminals are designated by small letters,
while the non-terminals are designated by capital
letters.
• There is at least one production that has the non-
terminal S as its left side.
12
• Example CFG:
• Example Derivation:
– A set of replacement rules, each having a Left-Hand Side (LHS) and a Right-Hand
Side (RHS).
– Two types of symbols; variables and terminals.
– LHS of each rule is a single variable (no terminals).
– RHS of each rule is a string of zero or more variables and terminals.
– A string consists of only terminals.
14
Formal Definition of CFG
• Formally, a Context-Free Grammar (CFG) is a 4-tuple:
G = (V, T, P, S)
T - A finite set of terminals (V and T do not intersect: do not use same symbols)
This is our ∑
S - A starting non-terminal (S is in V) 15
• Example CFG for {0k1k | k≥0}:
G = ({S}, {0, 1}, P, S) // Remember: G = (V, T, P, S)
P:
(1) S –> 0S1 or just simply S –> 0S1 | ε
(2) S –> ε
• Example Derivations:
P:
(1) S –> ABC
(2) A –> aA A –> aA | ε
(3) A –> ε
(4) B –> bB B –> bB | ε
(5) B –> ε
(6) C –> cC C –> cC | ε
(7) C –> ε
• Example Derivations:
1 E → I
2 E → E +E
3 E → E ∗E
4 E → (E)
5 I → a
6 I → b
7 I → Ia
8 I → Ib
9 I → I0
10 I → I1
• E ⇒ E ∗E ⇒ I ∗E ⇒ a ∗E ⇒ a ∗(E ) ⇒
• a ∗(E + E ) ⇒ a ∗(I + E ) ⇒ a ∗(a + E ) ⇒ a ∗(a + I ) ⇒
• a ∗(a + I 0) ⇒ a ∗(a + I 00) ⇒ a ∗(a + b00)
• Note 1: At each step we might have several rules to choose from, e.g.
• I ∗E ⇒ a ∗E ⇒ a ∗(E ), versus
• I ∗E ⇒ I ∗(E ) ⇒ a ∗(E ).
• Note 2: Not all choices lead to successful derivations of a particular string, for instance
• E ⇒ E + E (at the first step)
EXAMPLES
1− Leftmost derivation: previous example
2− Rightmost derivation:
E ⇒rm E∗E ⇒rm E ∗ (E) ⇒rm
E ∗ (E + E) ⇒rm E ∗ (E + I ) ⇒ r m E ∗ (E + I 0) ⇒rm
E ∗(E + I 00) ⇒ r m E ∗(E + b00) ⇒ r m E ∗(I + b00) ⇒ r m
E ∗(a + b00) ⇒ r m I ∗(a + b00) ⇒ r m a ∗(a + b00)
∗
We can conclude that E ⇒ rm a ∗(a + b00)
21
• Example:
S –> AB S A
A –> aAA
A –> aA A B aA
A –> a
B –> bB a A A b aAA
B –> b
a
• Notes:
– Root can be any non-terminal
– Leaf nodes can be terminals or non-terminals
– A derivation tree with root S shows the productions used to obtain a sentential form
22
• Observation: Every derivation corresponds to one derivation tree.
S => AB S Rules:
=> aAAB S –> AB
=> aaAB A B A –> aAA
=> aaaB A –> aA
=> aaab a A A b A –> a
B –> bB
a a B –> b
S => AB S
=> aAAB
=> aaAB A B
=> aaaB
=> aaab a A A b
a a
• Observation: Let G be a CFG. Then there may exist a string x in L(G) that has
more than 1 leftmost (or rightmost) derivation. Such a string will also have
more than 1 derivation tree.
24
• Example: Consider the string aaab and the preceding grammar.
S –> AB S => AB S
A –> aAA => aAAB
A –> aA => aaAB A B
A –> a => aaaB
B –> bB => aaab a A A b
B –> b
a a
S => AB S
=> aAB
=> aaAB A B
=> aaaB
=> aaab a A b
a A
a
• The string has two left-most derivations, and therefore has two distinct parse trees.
25
• Definition: Let G be a CFG. Then G is said to be ambiguous if there exists an
x in L(G) with >1 leftmost derivations. Equivalently, G is said to be
ambiguous if there exists an x in L(G) with >1 parse trees, or >1 rightmost
derivations.
• Note: Given a CFL L, there may be more than one CFG G with L = L(G).
Some ambiguous and some not.
26
• An ambiguous Grammar: A leftmost derivation
E -> I ∑ ={0,…,9, +, *, (, )} E=>E*E
=>I*E
E -> E + E =>3*E+E
E -> E * E =>3*I+E
=>3*2+E
E -> (E) =>3*2+I
I -> ε | 0 | 1 | … | 9 =>3*2+5
I E + E
E
3
I I
3 2
28
29
• A language may be Inherently ambiguous:
L ={anbncmdm | n≥1, m ≥ 1} {anbmcmdn | n ≥ 1, m ≥ 1}
• An ambiguous grammar:
S -> AB | C
A -> aAb | ab
B -> cBd | cd
Derivation 1 of Derivation 2 of
aabbccdd: aabbccdd:
S => AB S => C
=> aAbB => aCd
=> aabbB => aaDdd
=> aabb cBd => aa bDc dd
=> aabbccdd => aabbccdd
31