Theory Of Automata By Dr. MM Alam

Slides:



Advertisements
Similar presentations
Theory Of Automata By Dr. MM Alam
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Context Free Grammars.
Theory Of Automata By Dr. MM Alam
Theory Of Automata By Dr. MM Alam
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Theory Of Automata By Dr. MM Alam
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Theory Of Automata By Dr. MM Alam
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
So far... A language is a set of strings over an alphabet. We have defined languages by: (i) regular expressions (ii) finite state automata Both (i) and.
Lecture # 5 Pumping Lemma & Grammar
Lecture # 28 Theory Of Automata By Dr. MM Alam 1.
Context Free Grammars.
Lecture 11 Theory of AUTOMATA
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Lecture # 26 Theory Of Automata By Dr. MM Alam 1.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Lecture 04: Theory of Automata:08 Transition Graphs.
Lecture # 31 Theory Of Automata By Dr. MM Alam 1.
Chomsky Normal Form.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Theory Of Automata By Dr. MM Alam
Recap lecture 31 Context Free Grammar, Terminals, non- terminals, productions, CFG, context Free language, examples.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Context Free Grammars Chapter 12.
CONTEXT-FREE LANGUAGES
Theory Of Automata By Dr. MM Alam
Theory of Computation Lecture #
Transition Graphs.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Context-Free Grammars: an overview
Formal Language & Automata Theory
Context-free grammars, derivation trees, and ambiguity
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
CSC312 Automata Theory Grammatical Format Chapter # 13 by Cohen
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Recap lecture 38 Example of PDA with table for running a string, Equivalent PDA, PDA for EVEN EVEN Language. Non-Derterministic PDA, Example of Non-Derterministic.
Chapter 7 Regular Grammars
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Derivations and Languages
Theory of Computation Lecture #
Task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a}
Recap lecture 30 Deciding whether two languages are equivalent or not, example, deciding whether an FA accept any string or not, method 3, examples, finiteness.
COSC 3340: Introduction to Theory of Computation
Context Free Grammars-II
Answer Questions about Exam2 problems
CSCI 432 Computer Science Theory
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Theory Of Automata By Dr. MM Alam Lecture # 23 Theory Of Automata By Dr. MM Alam

Lecture#22 Recap…. Introduction to Context Free Grammars How a High Level language is converted to low level instructions, that computer understand. What are Production Rules and Derivations What is a CFG CFG Examples JFLAP for CFG

Context Free Language Example 3 Let the terminals be a and b, the only nonterminal be S, and Productions PROD 1 S → aS PROD 2 S → bS PROD 3 S → a PROD 4 S →b

Context Free Language Example 3 The word baab can be produced as follows: S => bS (by PROD 2) => baS (by PROD 1) => baaS (by PROD 1) => baab (by PROD 4)

Context Free Language Example 3 Productions 3 and 4 can be used only once and only one of them can be used. E.g, to generate babb we apply in order Prods 2, 1, 2, 4, as below: S => bS => baS => babS => babb

Context Free Language Example 4 Let the terminals be a and b, nonterminals be S, X, and Y. The productions are: S → X S → y X → ʎ Y→aY Y → bY Y → a Y → b

Context Free Language Therefore ʎ is the only word of type X. S → X S → y X → ʎ Y→aY Y → bY Y → a Y → b All the words in this language are either of type X, if the first production in their derivation is S → X or of type Y, if the first production in their derivation is S → Y The only possible continuation for words of type X is the production X → ʎ Therefore ʎ is the only word of type X.

Context Free Language S → X S → y X → ʎ Y→aY Y → bY Y → a Y → b The productions whose left side is Y form a collection identical to the productions in the previous example except that the start symbol S has been replaced by the symbol Y. We can carry on from Y the same way we carried on from S before. This does not change the language generated, which contains only strings of terminals.

Context Free Language Example 5 Let the terminals be a and b. Let the only nonterminal be S. Let the productions be S → aS S → bS S → a S → b S → ʎ

Context Free Language S → aS S → bS S → a S → b S → ʎ The word ab can be generated by the derivation S => aS => abS => ab ʎ = ab

Context Free Language Example 5 or by the derivation S => aS => ab The language of this CFG is also (a + b)*, but the sequence of productions that is used to generate a specific word is not unique.

Context Free Language Example 6 Let the terminals be a and b, nonterminals be S and X, and the productions be S → XaaX X → aX X → bX X → a X → b

Context Free Language anything aa anything or (a + b)*aa(a + b)* S → XaaX X → aX X → bX X → a X → b If the nonterminal X appears in any working string we can apply productions to turn it into any word we want. Therefore, the words generated from S have the form anything aa anything or (a + b)*aa(a + b)*

Context Free Language Example 7 Let the terminals be a and b, nonterminals be S, X, and Y the productions be S → XY X → aX X → bX X → a Y → Ya Y → Yb Y → a

Context Free Language Example 7 What can be derived from X? Let us look at the X productions alone. X → aX X → bX X → a X => bX => baX => babX => babbX => babba

Context Free Language Example 7 Similarly, the words that can be derived from Y are exactly those that begin with an a. To derive abbab, Y => Yb => Yab => Ybab => Ybbab => abbab

Tree format for CFG In old-fashioned English grammar courses students were often asked to diagram a sentence. They were to draw a parse tree, which is a picture with the base line divided into subject and predicate.

Trees Start with the symbol S. Every time a production is used to replace a nonterminal by a string, draw downward lines from the nonterminal to each character in the string. The CFG S → AA A → AAA I bA I Ab I a Tree of this CFG is on next slide

Trees S → AA A → AAA I bA I Ab I a

Trees We begin with S and apply the production S → AA. To the left-hand A the production A → bA is applied. To the right-hand A apply A → AAA is applied. The b that we have on the bottom line is a terminal, so it does not descend further. In the terminology of trees it is called a terminal node.

Ambiguity DEFINITION A CFG is called ambiguous if for at least one word in the language that it generates there are two possible derivations of the word that correspond to different syntax trees.

CFG Example 1 Consider the language generated by the CFG: PROD I S→ AB PROD 2 A → a PROD 3 B → b There are two different sequences of applications of the productions that generate the word ab. One is PROD 1, PROD 2, PROD 3. The other is PROD 1, PROD 3, PROD 2. S => AB => aB => ab or S => AB => Ab => ab

CFG Example 1 When a corresponding syntax trees is drawn we see that the two derivations are essentially the same:

CFG Example 2 Consider the language PALINDROME, define by the CFG below: S → aSa I bSb a I b| ʎ At every stage in the generation of a word by this grammar the working string contains only the one nonterminal S smack dab in the middle. The word grows like a tree from the center out.

CFG Example 2…. ... baSab => babSbab== babbSbbab => babbaSabbab ... When we finally replace S by a center letter (or A if the word has no center letter) we have completed the production of a palindrome. The word aabaa has only one possible generation: S => aSa => aaSaa => aabaa

CFG Example 2….

CFG EXAMPLE 3 The language of all not-null strings of a's can be defined by a CFG as follows: S → aS I Sa I a In this case the word a3 can be generated by four different trees Shown on next slide

CFG Ambiguity This CFG is therefore ambiguous.

CFG However the same language can also be defined by the CFG: S → aS I a

Introduction to CFG in JFLAP JFLAP GRAMMAR Menu

Lecture#23 Summary CFG Examples Introduction to Trees Ambiguity in CFGs CFG in JFLAP Thanks to Daniel I cohen. The material for these slides has been taken from his book Automata Theory.