2. p.434: Regular Languages and non-
regular languages
Dr. Hussien M. Sharaf 2
Language
Defined by
Corresponding
Accepting Machine
Nondeter-minism=
Determinism
Regular expression Finite automaton, transition graph Yes
Context-free
grammar
Pushdown automaton No
Type 0 grammar Turing machine, Post machine,
Pushdown automaton
Yes
3. CFG
A context-free grammar is a notation for
defining context free languages.
It is more powerful than finite automata or
RE’s, but still cannot define all possible
languages.
Useful for nested structures, e.g., parentheses
in programming languages.
Basic idea is to use “variables” to stand for
sets of strings.
These variables are defined recursively, in
terms of one another.
Dr. Hussien M. Sharaf 3
4. CFG formal definition
C =(V, Σ, R, S)
V: is a finite set of variables.
Σ: symbols called terminals of the alphabet of the
language being defined.
S V: a special start symbol.
R: is a finite set of production rules of the form A→
where A V, (V Σ)
Dr. Hussien M. Sharaf 4
5. CFG -1
Define the language { anbn | n > 1}.
Terminals = {a, b}.
Variables = {S}.
Start symbol = S.
Productions ={
S → ab,
S → aSb }
Summary
S → ab
S → aSb
Dr. Hussien M. Sharaf 5
6. Derivation
We derive strings in the language of a CFG by starting
with the start symbol, and repeatedly replacing some
variable A by the right side of one of its productions.
Derivation example for “aabb”
Using S→ aSb
generates uncompleted string that still has a non-
terminal S.
Then using S→ ab to replace the inner S
Generates “aabb”
S aSb aabb ……[Successful derivation of aabb]
Dr. Hussien M. Sharaf 6
7. Balanced-parentheses
Prod1 S → (S)
Prod2 S → ()
Derive the string ((())).
S → (S) …..[by prod1]
→ ((S)) …..[by prod1]
→ ((())) …..[by prod2]
Dr. Hussien M. Sharaf 7
8. Palindrome
Describe palindrome of a’s and b’s using CGF
1] S → aS 2] S → bS
3] S → b 4] S → a
Derive “baab” from the above grammar.
S → bS [by 2]
→ baS [by 1]
→ baaS [by 1]
→ baab [by 3]
Dr. Hussien M. Sharaf 8
9. CFG – 2.1
Describe anything (a+b)* using CGF
1] S → Λ 2] S → Y 3] Y→ aY
4] Y → bY 5] Y →a 6] Y→ b
Derive “aab” from the above grammar.
S → aY [by 3]
Y → aaY [by 3]
Y → aab [by 6]
Dr. Hussien M. Sharaf 9
10. CFG – 2.2
1] S → Λ 2] S → Y 3] Y→ aY
4] Y → bY 5] Y →a 6] Y→ b
Derive “aa” from the above grammar.
S → aY [by 3]
Y → aa [by 5]
Dr. Hussien M. Sharaf 10
12. CFG – 3
Describe anything (a+b)* using CGF
1] S →aS 2] S → bS 3] S →Λ
Derive “aab” from the above grammar.
S → aS [by 1]
S → aaS [by 1]
S → aabS [by 2]
S → aabΛ [by 3] → aab
Dr. Hussien M. Sharaf 12
13. CFG – 4
Describe anything (a+b)*aa(a+b)* using CGF
1] S →XaaX 2] X → aX 3] X → bX
4] X →Λ
Note: rules 2, 3, 4 represents anything (a+b)*
Derive “baabaab” from the above grammar.
S → XaaX [by 1] X→ bXaaX [by 3]
X→ bΛaaX [by 4] X → baabX [by 3]
X → baabaX [by 2] X→ baabaaX [by 2]
X→ baabaabX [by 3] X→ baabaabΛ[by 4]
Dr. Hussien M. Sharaf 13
14. CFG – 5
Describe a language that has even number of a’s and even
number of b’s using CGF.
i.e. aababbab
1] S →SS 2] S →BALANCED S
3] S →S BALANCED 4] S →Λ
5] S →UNBALANCED S UNBALANCED
6] BALANCED → aa 7] BALANCED → bb
8] UNBALANCED → ab
9] UNBALANCED → ba
Dr. Hussien M. Sharaf 14
15. CFG – 5
Derive “aababbab” from the above grammar.
S → BALANCED S [by 1]
BALANCED→ aa UNBALANCED S UNBALANCED
[by 5]
UNBALANCED → aa ba S UNBALANCED [by 9]
UNBALANCED → aa ba S ab [by 8]
S → aa ba BALANCED S ab [by 3]
UNBALANCED → aa ba bb S ab [by 7]
S → aa ba bb ab [by 4]
→ aababbab
Dr. Hussien M. Sharaf 15
16. CFG – 6
i.e. {Λ, ab, aaabbb,… }
S → aSb|Λ
Derive “aaaabbbb”
S →aSb→aSb→aaSbb →aaaSbbb
→aaaaSbbbb →aaaabbbb
Dr. Hussien M. Sharaf 16
17. CFG – 7
i.e. {Λ, ab, abbaabba,… }
S → aSa| bSb| Λ
Derive “abbaabba” “abba abba”
S →aSa→abSba→abbSbba →abbaSabba→ abba abba
→ abbaabba
Dr. Hussien M. Sharaf 17
18. CFG – 8
i.e. {Λ, abb, ababa,… }
S → aSa| bSb| a| b| Λ
Derive “ababa” “ab a ba”
S →aSa→abSba→abSba →abSba→ ababa
Note: an
ban
can be represented by S → aSa| b
But an
ban
bn+1
can not be represented by CFG !
Dr. Hussien M. Sharaf 18
19. i.e. {Λ, ab, abbaabba,… }
S → aSa| bSb| Λ Derive abaaba
19
S
S
Λ
aa
S bb
S aa
Dr. Hussien M. Sharaf
CFG – 9
20. Deduce CFG of addition and parse the
following expression 2+3+5
1] S→S+S|N
2] N→1|2|3|4|5|6|7|8|9|0
N1|N2|N3|N4|N5|N6|N7|N8|N9|N0
20
S
S+N
S
+
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
Dr. Hussien M. Sharaf
CFG – 10
21. CFG – 11.1
Deduce CFG of a addition/multiplication and
parse the following expression 2+3*5
1] S→S+S|S*S|N
2] N→1|2|3|4|5|6|7|8|9|0
N1|N2|N3|N4|N5|N6|N7|N8|N9|N0
21
S
S*S
S
*
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
Dr. Hussien M. Sharaf
22. CFG -11.2 without ambiguity
Deduce CFG of a addition/multiplication
and parse the following expression 2*3+5
1] S→ Term|Term + S
2] Term → N|N * Term
3] N→1|2|3|4|5|6|7|8|9|0
N1|N2|N3|N4|N5|N6|N7|N8|N9|N0
22
S
S+N
S
+
N
5
S
*
3
N
2
N
Can u make
another parsing
tree ?
Dr. Hussien M. Sharaf