SlideShare a Scribd company logo
1 of 23
Dr. Hussien Sharaf
Computer Science Department
dr.sharaf@from-masr.com
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
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
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
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
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
Balanced-parentheses
Prod1 S → (S)
Prod2 S → ()
 Derive the string ((())).
S → (S) …..[by prod1]
→ ((S)) …..[by prod1]
→ ((())) …..[by prod2]
Dr. Hussien M. Sharaf 7
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
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
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
Dr. Hussien M. Sharaf 11
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
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
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
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
CFG – 6
 i.e. {Λ, ab, aaabbb,… }
 S → aSb|Λ
 Derive “aaaabbbb”
 S →aSb→aSb→aaSbb →aaaSbbb
→aaaaSbbbb →aaaabbbb
Dr. Hussien M. Sharaf 16
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
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
 i.e. {Λ, ab, abbaabba,… }
 S → aSa| bSb| Λ Derive abaaba
19
S
S
Λ
aa
S bb
S aa
Dr. Hussien M. Sharaf
CFG – 9
 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
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
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
Thank you
Dr. Hussien M. Sharaf 23

More Related Content

What's hot

5.5 permutations and combinations
5.5 permutations and combinations5.5 permutations and combinations
5.5 permutations and combinationsmath123c
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Regular expression
Regular expressionRegular expression
Regular expressionRajon
 
Intro To Formula Project
Intro To Formula ProjectIntro To Formula Project
Intro To Formula ProjectKathy Favazza
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & CombinationPuru Agrawal
 
Probability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and CombinationsProbability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and CombinationsKate Nowak
 
Discrete mathematics presentation
Discrete mathematics presentationDiscrete mathematics presentation
Discrete mathematics presentationMdZiad
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 
Hw2 2017-spring
Hw2 2017-springHw2 2017-spring
Hw2 2017-spring奕安 陳
 
Permutation and Combination Maths
Permutation and Combination MathsPermutation and Combination Maths
Permutation and Combination MathsVardhan Jain
 
Alphabets , strings, languages and grammars
Alphabets , strings, languages  and grammarsAlphabets , strings, languages  and grammars
Alphabets , strings, languages and grammarshele987
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular ExpressionsOCSI
 
Automata theory
Automata theoryAutomata theory
Automata theorycolleges
 

What's hot (20)

Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Ch02
Ch02Ch02
Ch02
 
5.5 permutations and combinations
5.5 permutations and combinations5.5 permutations and combinations
5.5 permutations and combinations
 
Permutation and combination
Permutation and combinationPermutation and combination
Permutation and combination
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Intro To Formula Project
Intro To Formula ProjectIntro To Formula Project
Intro To Formula Project
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & Combination
 
Probability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and CombinationsProbability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and Combinations
 
Lecture2 B
Lecture2 BLecture2 B
Lecture2 B
 
Discrete mathematics presentation
Discrete mathematics presentationDiscrete mathematics presentation
Discrete mathematics presentation
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Hw2 2017-spring
Hw2 2017-springHw2 2017-spring
Hw2 2017-spring
 
Permutation and Combination Maths
Permutation and Combination MathsPermutation and Combination Maths
Permutation and Combination Maths
 
Alphabets , strings, languages and grammars
Alphabets , strings, languages  and grammarsAlphabets , strings, languages  and grammars
Alphabets , strings, languages and grammars
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
Automata theory
Automata theoryAutomata theory
Automata theory
 

More from Arab Open University and Cairo University

More from Arab Open University and Cairo University (20)

Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 

Recently uploaded

How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 

Recently uploaded (20)

How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 

Lec4

  • 1. Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 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
  • 11. Dr. Hussien M. Sharaf 11
  • 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
  • 23. Thank you Dr. Hussien M. Sharaf 23