SlideShare a Scribd company logo
1 of 43
Download to read offline
Theory of Computation
By Rushabh Wadkar
Topics to be covered
Chomsky Hierarchy in Grammar
Finite Automata to Grammar
Language and Grammar
related problems.
Ambiguous Grammar and
Parsing Tree
Annex: Table Filling Algorithm
Day 6
The Chomsky Hierarchy
● Type 0 known as Unrestricted Grammar.
● Type 1 known as Context Sensitive Grammar.
● Type 2 known as Context Free Grammar.
● Type 3 known as Regular Grammar.
According to Chomsky there exist 4 types of grammars.
The Chomsky Hierarchy
According to Chomsky there exist 4 types of grammars.
The Chomsky Hierarchy
● Type-0 grammars include all formal grammars.
● Type 0 grammar language are recognized by
turing machine.
● These languages are also known as the
Recursively Enumerable languages.
Type 0: Unrestricted Grammar:
The Chomsky Hierarchy
In type 0:
Grammar Production in the form of α ---> β where
α is ( V + T)* V ( V + T)* { V : Variables, T : Terminals}
β is ( V + T )*
In type 0 there must be at least one variable on Left side of
production. For example,
Sab –> ba
A –> S.
Here, Variables are S, A and Terminals a, b.
The Chomsky Hierarchy
● Type-1 grammars generate the context-sensitive
languages.
● The language generated by the grammar are
recognized by the Linear Bound Automata
● Type 1 grammar should be of Type 0.
Type 1: Context Sensitive Grammar
The Chomsky Hierarchy
In type 1:
Grammar Production in the form of
α ---> β
|α| <= |β|
Where α,β ∈ (VUT)*; It is a ε free grammar
For Example,
S –> AB
AB –> abc
B –> b
The Chomsky Hierarchy
● Type-2 grammars generate the context-free
languages.
● The language generated by the grammar is
recognized by a Pushdown automata.
● The grammar should be Type 1.
Type 2: Context Free Grammar:
The Chomsky Hierarchy
In Type 2:
Left hand side of production can have only one variable.
|α| = 1.
There is no restriction on β.
For example,
S –> AB
A –> a
B –> b
The Chomsky Hierarchy
● Type-3 grammars generate regular languages.
● These languages are exactly all languages that
can be accepted by a finite state automaton.
● Type 3 is most restricted form of grammar.
Type 3: Regular Grammar:
The Chomsky Hierarchy
In Type 3:
Type 3 should be in the given form only :
V –> VT* / T*.
(or)
V –> T*V /T*
i.e. Grammar is regular only if it is right linear or left linear.
For example,
S –> aB or S –> Ba
S –> a S –> a
Grammar from FA
We can convert from any Finite automata to a grammar using just
these 2 steps.
Step 1: if 𝛿(qi
,0) = qj
, then introduce transition
qi
---> 0qj
Step 2: if q ∈ F, then introduce transition
q ---> ε
Grammar from FA
Let’s take a look at an example:
Grammar from FA
Let’s take a look at another example:
S ---> aA
A ---> aA | ε
Grammar from FA
Let’s take a look at another example:
S ---> aA
A ---> aB | ε
B ---> aS | ε
Grammar from FA
Let’s take a look at another example:
S ---> aS | bA | ε
A ---> aS | ε
Grammar from Regular Expressions
We can convert it to a FA and then convert it to required grammar.
Grammar from Regular Expressions
We can convert it to a FA and then convert it to required grammar.
Grammar Problems
Q. Obtain grammar for language L = { ax
by
: x ≠ y }
S ---> aSb | A | B
A ---> aA | a
B ---> bB | b
Grammar Problems
Q. Obtain grammar for generating all integers
S ---> AB
A ---> + | -
B ---> CB | C
C ---> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Grammar Problems
Q. Obtain grammar for generating Language L = { an
bn+1
: n>=0}
S ---> aSb
S ---> b
Grammar Problems
Q. Obtain grammar for generating Language L = { an+2
bn
: n>=0}
S ---> aSb
S ---> aa
Grammar Problems
Q. Obtain grammar for generating Language L = { wwR
: w∈{a,b}*}
S ---> aSa
S ---> bSb
S ---> ε
Grammar Problems
Q. Obtain grammar for generating a palindrome string w∈{a,b}*
S ---> aSa
S ---> bSb
S ---> a | b | ε
Grammar Problems
Q. Obtain grammar for (110 + 11)* (10)*
S ---> AB
A ---> 110A | 11A | ε
B ---> 10B | ε
Grammar Problems
Q. Obtain grammar for ab(a+b)*
S ---> abA
A ---> aA | bA | ε
Grammar Problems
Q. Obtain grammar for language L = { 0m
1m
2n
: m>=1, n>=0 }
S ---> AB
A ---> 01 | 0A1
B ---> 2B | ε
Grammar Problems
Q. Obtain grammar for language L = { 0m
1m
2n
: m>=1, n>=0 }
S ---> AB
A ---> 01 | 0A1
B ---> 2B | ε
Grammar Problems
Q. Obtain grammar for string containing not more than 3 a’s
Grammar Problems
Q. Obtain grammar for string containing not more than 3 a’s
S ---> bS | aA | ε
A ---> bA | aB | ε
B ---> bB | aC | ε
C ---> bC | ε
Ambiguous grammar
A CFG is said to ambiguous if there exists more than one derivation tree for the given
input string i.e., more than one LeftMost Derivation Tree (LMDT) or
RightMost Derivation Tree (RMDT).
Definition: G = (V,T,P,S) is a CFG is said to be ambiguous if and only if there exist a
string in T* that has more than on parse tree. Where,
V is a finite set of variables
T is a finite set of terminals.
P is a finite set of productions of the form, A -> α;
where A is a variable and α ∈ (V ∪ T)*
S is a designated variable called the start symbol.
Parse Tree
Let us consider this grammar : E -> E+E|id
We can create 2 parse tree from this grammar
to obtain a string id+id+id :
Ambiguity Problems
Q. Check if the following grammar is ambiguous.
S ---> aS | X
A ---> Xa | a
S => aS
=> aaS
=> aaaS
=> aaaX
=> aaaa
S => X
=> aX
=> aaX
=> aaaX
=> aaaa
Hence the grammar is ambiguous.
Ambiguity Problems
Q. Check if the following grammar is ambiguous.
S ---> aB | bA
A ---> aS | bAA | a
B ---> bS | aBB | b
S ---> aB | bA
A ---> aS | bAA | a
B ---> bS | aBB | b
Hence the grammar
is ambiguous.
Ambiguity Problems
End of Day 6
www.linkedin.com/in/wadkar-rushabh
@RushabhWadkar
Thank you...
EX. 2
EX. 3
Theory of Computation Grammar Concepts and Problems

More Related Content

What's hot

Theory of Automata
Theory of AutomataTheory of Automata
Theory of AutomataFarooq Mian
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite AutomataAdel Al-Ofairi
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesMarina Santini
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsShiraz316
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examplesankitamakin
 
closure properties of regular language.pptx
closure properties of regular language.pptxclosure properties of regular language.pptx
closure properties of regular language.pptxThirumoorthy64
 
4.7. chomskian hierarchy of languages
4.7. chomskian hierarchy of languages4.7. chomskian hierarchy of languages
4.7. chomskian hierarchy of languagesSampath Kumar S
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theorySampath Kumar S
 
3.1,2,3 pushdown automata definition, moves &amp; id
3.1,2,3 pushdown automata   definition, moves &amp; id3.1,2,3 pushdown automata   definition, moves &amp; id
3.1,2,3 pushdown automata definition, moves &amp; idSampath Kumar S
 

What's hot (20)

Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Language
LanguageLanguage
Language
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
 
closure properties of regular language.pptx
closure properties of regular language.pptxclosure properties of regular language.pptx
closure properties of regular language.pptx
 
4.7. chomskian hierarchy of languages
4.7. chomskian hierarchy of languages4.7. chomskian hierarchy of languages
4.7. chomskian hierarchy of languages
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theory
 
3.1,2,3 pushdown automata definition, moves &amp; id
3.1,2,3 pushdown automata   definition, moves &amp; id3.1,2,3 pushdown automata   definition, moves &amp; id
3.1,2,3 pushdown automata definition, moves &amp; id
 

Similar to Theory of Computation Grammar Concepts and Problems

Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
2-Chomsky hierarchy of languages.ppt
2-Chomsky hierarchy of languages.ppt2-Chomsky hierarchy of languages.ppt
2-Chomsky hierarchy of languages.pptshruti533256
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
Theory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and GrammarTheory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and GrammarRushabh2428
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxRaviAr5
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarniveditJain
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptxmainakmail2585
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchySANUC2
 
compiler design Ashutosh Kumar cse-1 roll-046.pdf
compiler design Ashutosh Kumar cse-1 roll-046.pdfcompiler design Ashutosh Kumar cse-1 roll-046.pdf
compiler design Ashutosh Kumar cse-1 roll-046.pdfAshutoshKumar211882
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02hamzamughal39
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghggggggggggggggggggggggggggggggggggadugnanegero
 

Similar to Theory of Computation Grammar Concepts and Problems (20)

Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
2-Chomsky hierarchy of languages.ppt
2-Chomsky hierarchy of languages.ppt2-Chomsky hierarchy of languages.ppt
2-Chomsky hierarchy of languages.ppt
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Theory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and GrammarTheory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and Grammar
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
compiler design Ashutosh Kumar cse-1 roll-046.pdf
compiler design Ashutosh Kumar cse-1 roll-046.pdfcompiler design Ashutosh Kumar cse-1 roll-046.pdf
compiler design Ashutosh Kumar cse-1 roll-046.pdf
 
Ch02
Ch02Ch02
Ch02
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggg
 

Recently uploaded

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 

Recently uploaded (20)

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Theory of Computation Grammar Concepts and Problems

  • 1. Theory of Computation By Rushabh Wadkar
  • 2. Topics to be covered Chomsky Hierarchy in Grammar Finite Automata to Grammar Language and Grammar related problems. Ambiguous Grammar and Parsing Tree Annex: Table Filling Algorithm Day 6
  • 3. The Chomsky Hierarchy ● Type 0 known as Unrestricted Grammar. ● Type 1 known as Context Sensitive Grammar. ● Type 2 known as Context Free Grammar. ● Type 3 known as Regular Grammar. According to Chomsky there exist 4 types of grammars.
  • 4. The Chomsky Hierarchy According to Chomsky there exist 4 types of grammars.
  • 5. The Chomsky Hierarchy ● Type-0 grammars include all formal grammars. ● Type 0 grammar language are recognized by turing machine. ● These languages are also known as the Recursively Enumerable languages. Type 0: Unrestricted Grammar:
  • 6. The Chomsky Hierarchy In type 0: Grammar Production in the form of α ---> β where α is ( V + T)* V ( V + T)* { V : Variables, T : Terminals} β is ( V + T )* In type 0 there must be at least one variable on Left side of production. For example, Sab –> ba A –> S. Here, Variables are S, A and Terminals a, b.
  • 7. The Chomsky Hierarchy ● Type-1 grammars generate the context-sensitive languages. ● The language generated by the grammar are recognized by the Linear Bound Automata ● Type 1 grammar should be of Type 0. Type 1: Context Sensitive Grammar
  • 8. The Chomsky Hierarchy In type 1: Grammar Production in the form of α ---> β |α| <= |β| Where α,β ∈ (VUT)*; It is a ε free grammar For Example, S –> AB AB –> abc B –> b
  • 9. The Chomsky Hierarchy ● Type-2 grammars generate the context-free languages. ● The language generated by the grammar is recognized by a Pushdown automata. ● The grammar should be Type 1. Type 2: Context Free Grammar:
  • 10. The Chomsky Hierarchy In Type 2: Left hand side of production can have only one variable. |α| = 1. There is no restriction on β. For example, S –> AB A –> a B –> b
  • 11. The Chomsky Hierarchy ● Type-3 grammars generate regular languages. ● These languages are exactly all languages that can be accepted by a finite state automaton. ● Type 3 is most restricted form of grammar. Type 3: Regular Grammar:
  • 12. The Chomsky Hierarchy In Type 3: Type 3 should be in the given form only : V –> VT* / T*. (or) V –> T*V /T* i.e. Grammar is regular only if it is right linear or left linear. For example, S –> aB or S –> Ba S –> a S –> a
  • 13. Grammar from FA We can convert from any Finite automata to a grammar using just these 2 steps. Step 1: if 𝛿(qi ,0) = qj , then introduce transition qi ---> 0qj Step 2: if q ∈ F, then introduce transition q ---> ε
  • 14. Grammar from FA Let’s take a look at an example:
  • 15. Grammar from FA Let’s take a look at another example: S ---> aA A ---> aA | ε
  • 16. Grammar from FA Let’s take a look at another example: S ---> aA A ---> aB | ε B ---> aS | ε
  • 17. Grammar from FA Let’s take a look at another example: S ---> aS | bA | ε A ---> aS | ε
  • 18. Grammar from Regular Expressions We can convert it to a FA and then convert it to required grammar.
  • 19. Grammar from Regular Expressions We can convert it to a FA and then convert it to required grammar.
  • 20. Grammar Problems Q. Obtain grammar for language L = { ax by : x ≠ y } S ---> aSb | A | B A ---> aA | a B ---> bB | b
  • 21. Grammar Problems Q. Obtain grammar for generating all integers S ---> AB A ---> + | - B ---> CB | C C ---> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  • 22. Grammar Problems Q. Obtain grammar for generating Language L = { an bn+1 : n>=0} S ---> aSb S ---> b
  • 23. Grammar Problems Q. Obtain grammar for generating Language L = { an+2 bn : n>=0} S ---> aSb S ---> aa
  • 24. Grammar Problems Q. Obtain grammar for generating Language L = { wwR : w∈{a,b}*} S ---> aSa S ---> bSb S ---> ε
  • 25. Grammar Problems Q. Obtain grammar for generating a palindrome string w∈{a,b}* S ---> aSa S ---> bSb S ---> a | b | ε
  • 26. Grammar Problems Q. Obtain grammar for (110 + 11)* (10)* S ---> AB A ---> 110A | 11A | ε B ---> 10B | ε
  • 27. Grammar Problems Q. Obtain grammar for ab(a+b)* S ---> abA A ---> aA | bA | ε
  • 28. Grammar Problems Q. Obtain grammar for language L = { 0m 1m 2n : m>=1, n>=0 } S ---> AB A ---> 01 | 0A1 B ---> 2B | ε
  • 29. Grammar Problems Q. Obtain grammar for language L = { 0m 1m 2n : m>=1, n>=0 } S ---> AB A ---> 01 | 0A1 B ---> 2B | ε
  • 30. Grammar Problems Q. Obtain grammar for string containing not more than 3 a’s
  • 31. Grammar Problems Q. Obtain grammar for string containing not more than 3 a’s S ---> bS | aA | ε A ---> bA | aB | ε B ---> bB | aC | ε C ---> bC | ε
  • 32. Ambiguous grammar A CFG is said to ambiguous if there exists more than one derivation tree for the given input string i.e., more than one LeftMost Derivation Tree (LMDT) or RightMost Derivation Tree (RMDT). Definition: G = (V,T,P,S) is a CFG is said to be ambiguous if and only if there exist a string in T* that has more than on parse tree. Where, V is a finite set of variables T is a finite set of terminals. P is a finite set of productions of the form, A -> α; where A is a variable and α ∈ (V ∪ T)* S is a designated variable called the start symbol.
  • 33. Parse Tree Let us consider this grammar : E -> E+E|id We can create 2 parse tree from this grammar to obtain a string id+id+id :
  • 34. Ambiguity Problems Q. Check if the following grammar is ambiguous. S ---> aS | X A ---> Xa | a S => aS => aaS => aaaS => aaaX => aaaa S => X => aX => aaX => aaaX => aaaa Hence the grammar is ambiguous.
  • 35. Ambiguity Problems Q. Check if the following grammar is ambiguous. S ---> aB | bA A ---> aS | bAA | a B ---> bS | aBB | b
  • 36. S ---> aB | bA A ---> aS | bAA | a B ---> bS | aBB | b Hence the grammar is ambiguous. Ambiguity Problems
  • 37. End of Day 6 www.linkedin.com/in/wadkar-rushabh @RushabhWadkar Thank you...
  • 38.
  • 39.
  • 40. EX. 2
  • 41.
  • 42. EX. 3