1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.

Slides:



Advertisements
Similar presentations
Chapter 5 Pushdown Automata
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CFGs and PDAs Sipser 2 (pages ). Long long ago…
Pushdown Automata Part II: PDAs and CFG Chapter 12.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
CFGs and PDAs Sipser 2 (pages ). Last time…
Introduction to Computability Theory
CFG => PDA Sipser 2 (pages ).
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
CFG => PDA Sipser 2 (pages ). CS 311 Fall Formally… A pushdown automaton is a sextuple M = (Q, Σ, Γ, δ, q 0, F), where – Q is a finite set.
CS5371 Theory of Computation
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS 490: Automata and Language Theory Daniel Firpo Spring 2003.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Normal forms for Context-Free Grammars
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
1 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
CS490 Presentation: Automata & Language Theory Thong Lam Ran Shi.
Pushdown Automata (PDA) Intro
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Pushdown Automata (PDAs)
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Context-free Languages
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
Theory of Computation 5. Pushdown Automata
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Formal Languages, Automata and Models of Computation
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 3 The Church-Turing Thesis Contents Turing Machines definitions, examples,
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Theory of Languages and Automata By: Mojtaba Khezrian.
Theory of Computation Automata Theory Dr. Ayman Srour.
CS6800 Advance Theory of Computation Spring 2016 Nasser Alsaedi
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
6. Pushdown Automata CIS Automata and Formal Languages – Pei Wang.
Formal Languages, Automata and Models of Computation
Computability Joke. Context-free grammars Parsing. Chomsky
Context-Free Grammars: an overview
G. Pullaiah College of Engineering and Technology
CSE 105 theory of computation
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
CSE 105 theory of computation
COMPILER CONSTRUCTION
CSE 105 theory of computation
Presentation transcript:

1Computer Sciences Department

Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department

4 objectives -Describing Languages -A Formal Definition Of Context-free Grammars -The Properties Of Context-free Languages -A PushDown Automata PDA -Equivalence -The Pumping Lemma

Context-free grammars  Ch.1 introduced two different, though equivalent, methods of describing languages: finite automata and regular expressions.  Languages can be described in this way but that some simple languages, such as (0 n 1 n | n > 0}, cannot. CONTEXT-FREE GRAMMARS (C.F.G.)  powerful method of describing languages.  used in the study of human languages.  understanding the relationship of terms such as noun, verb, and preposition.  An important application of context-free grammars occurs in the specification and compilation of programming languages. 5Computer Sciences Department

Context-free grammars (cont.)  A grammar for a programming language often appears as a reference for people trying to learn the language syntax.  Designers of compilers and interpreters for programming languages often start by obtaining a grammar for the language.  Most compilers and interpreters contain a component called a parser that extracts the meaning of a program prior to generating the compiled code or performing the interpreted execution. 6Computer Sciences Department

Context free languages  The collection of languages associated with context- free grammars are called the context-free languages. 7Computer Sciences Department

Objectives  To give a formal definition of context-free grammars and study the properties of context-free languages.  To introduce pushdown automata, a class of machines recognizing the context-free languages. 8Computer Sciences Department

CONTEXT-FREE GRAMMARS  The following is an example of a context-free grammar, which we call G1:  A grammar consists of a collection of substitution rules, also called productions.  Each rule appears as a line in the grammar, comprising a symbol and a string separated by an arrow. 9Computer Sciences Department

A → β  The symbol is called a variable.  The string consists of variables and other symbols called terminals.  The variable symbols often are represented by capital letters.  The terminals are (input alphabet) often represented by lowercase letters, numbers, or special symbols.  One variable is designated as the start variable (left- hand side of the topmost rule.). 10Computer Sciences Department

Rule  Any production of the form A → β. β can therefore be any string of terminal and non-terminal elements.  Example: A → BC A → a 11 start variable terminal variable symbols Computer Sciences Department

Example 12Computer Sciences Department

Describing a language  a grammar is used to describe a language by generating each string of that language in the following manner: 1.Write down the start variable. (left-hand side of the top rule, unless specified otherwise). 2.Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3.Repeat step 2 until no variables remain. 13Computer Sciences Department

Abbreviation  abbreviate several rules with the same left-hand variable, such as A  0A1 and A  B, into a single line A  0A1 I B, using the symbol " I " as an "or.“ A  0A1 or B. 14Computer Sciences Department

Derivation  The sequence of substitutions to obtain a string is called a derivation.  A derivation of string 000#111 in grammar G1 is: 15 parse tree Computer Sciences Department

 Grammar G2 has ??  rules?? - variables ?? - terminals??. 16Computer Sciences Department

Grammar G2  Grammar G2 has:  10 variables (the capitalized grammatical terms written inside brackets);  27 terminals (the standard English alphabet plus a space character);  18 rules. 17Computer Sciences Department

Derivation  Each of these strings has a derivation in grammar G2. The following is a derivation of the first string on this list. 18Computer Sciences Department

FORMAL DEFINITION OF A CONTEXT- FREE GRAMMAR 19Computer Sciences Department

Example Grammar G1 20 In grammar G2 Computer Sciences Department

Ambiguity  The generation of a sentence by a context-free grammar can be represented by a tree diagram.  Not only one way in which a sentence can be derived. 21Computer Sciences Department

Example Let G be a context-free grammar with the following productions: 1. S → AB 5. Β → Sd 2. S → CD 6. C → aS 3. S → bc 7. D → d 4. A → a The sentence abcd can be derived from this grammar?????????????????? 22Computer Sciences Department

solution & Derivation tree The sentence abcd can be derived from this grammar as follows: 1. S ⇒ AB ⇒ aB ⇒ aSd ⇒ abcd. 2. S ⇒ AB ⇒ ASd ⇒ Abcd ⇒ abcd, 3. S ⇒ CD ⇒ aSD ⇒ abcD ⇒ abcd. (Alternative) 23 Derivation tree for the sentence abcd -1&2 Derivation tree for the sentence abcd -3 true or false????? c Computer Sciences Department

Ambiguity (cont.)  If a grammar generates the same string in several different ways, we say that the string is derived ambiguously in that grammar.  If a grammar generates some string ambiguously we say that the grammar is ambiguous. 24Computer Sciences Department

Example For example, consider grammar G5: This grammar generates the string a+axa ambiguously?????????????? Yes (two different parse trees) 25Computer Sciences Department

DEFINITION 2.7  A string ω is derived ambiguously in context-free grammar G if it has two or more different leftmost derivations.  Grammar G is ambiguous if it generates some string ambiguously. 26Computer Sciences Department

Chomsky normal form 27Computer Sciences Department

28Computer Sciences Department

EXAMPLE 2.10  Let G6 be the following CFG and convert it to Chomsky normal form by using the conversion procedure just given. The series of grammars presented illustrates the steps in the conversion. Rules shown in bold have just been added. Rules shown in gray have just been removed. 29Computer Sciences Department

30Computer Sciences Department

31Computer Sciences Department

32Computer Sciences Department

Context-Free Languages {0 n 1 n I n > 0} is not regular??? 33 Example: G1 A → 0A1 A → B B → # A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111 L(G1) = {0 n #1 n | n ≥ 0 } parse tree Can the following grammar generate the subsequent language? 000#111 Computer Sciences Department

A Grammar for Arithmetic Expressions Let : X = {E, T, F, id, +, -,*,/,(,), a, b, c} T = {a, b, c, +, -,*,/,(,)}. The start symbol S is E and the productions are as follows 34 Write a derivation of string (a + b)*c Computer Sciences Department

35 Write a derivation of string (a + b)*c The derivation of string (a + b)*c: Computer Sciences Department

PUSHDOWN AUTOMATA  It’s a new type of computational model called pushdown automata.  These automata are like nondeterministic finite automata but have an extra component called a stack.  The stack provides additional memory beyond the finite amount available in the control. 36Computer Sciences Department

37 The following figure is a schematic representation of a finite automaton. The control represents the states and transition function, the tape contains the input string, and the arrow represents the input head, pointing at the next input symbol to be read. Schematic of a finite automaton Computer Sciences Department

38 Schematic of a pushdown automaton With the addition of a stack component we obtain a schematic representation of a pushdown automaton, as shown in the following figure Computer Sciences Department

39Computer Sciences Department

PUSHDOWN AUTOMATA  A pushdown automaton (PDA) can write symbols on the stack and read them back later.  Writing a symbol "pushes down" all the other symbols on the stack.  At any time the symbol on the top of the stack can be read and removed.  Writing a symbol on the stack is often referred to as pushing the symbol, and removing a symbol is referred to as popping it. 40Computer Sciences Department

PUSHDOWN AUTOMATA  a stack is a " last in, first out " storage device:  If certain information is written on the stack and additional information is written afterward, the earlier information becomes inaccessible until the later information is removed.  A stack is valuable because it can hold an unlimited amount of information. 41Computer Sciences Department

PUSHDOWN AUTOMATA  Recall that a finite automaton is unable to recognize the language {0 n 1 n I n > 0} because it cannot store very large numbers in its finite memory.  A PDA is able to recognize this language because it can use its stack to store the number of Os it has seen.  Thus the unlimited nature of a stack allows the PDA to store numbers of unbounded size. 42Computer Sciences Department

Informal description  The following informal description shows how the automaton for this language works:  Read symbols from the input. As each 0 is read, push it onto the stack.  Pop a 0 off the stack for each 1 read.  If reading the input is finished exactly when the stack becomes empty of 0 s, accept the input.  If the stack becomes empty while is remain or if the is are finished while the stack still contains 0 s or if any 0 s appear in the input following is, reject the input. 43Computer Sciences Department

FORMAL DEFINITION OF A PUSHDOWN AUTOMATON 44Computer Sciences Department

45Computer Sciences Department

46Computer Sciences Department

47Computer Sciences Department

48 Let P be defined through X = { a, b, c }, Z = { z A = z 1, z 2, z 3 }, S = { S A, S, }, Z F = { z 3 } and, finally the state transitions Computer Sciences Department

Example a 3 bc 3 = aaabccc?? 49 ((a, z 1, S A ),(z 1, SS A )) ((a, z 1, S),(z 1, SS)) ((b, z 1, S),(z 2,λ)) ((c, z 2, S),(z 2, λ)) ((c, z 2, S A ),(z 3, λ)) Computer Sciences Department

50 Input string = 0011 Input string = Computer Sciences Department

51Computer Sciences Department

52 PDA in Figure - test empty stack by initially placing a special symbol, $, on the stack If ever it sees $ again on the stack, it knows that the stack is effectively empty. Computer Sciences Department

53Computer Sciences Department

54

Computer Sciences Department55

Computer Sciences Department56 EQUIVALENCE WITH CONTEXT-FREE GRAMMARS

Computer Sciences Department57

Proof Computer Sciences Department58

Computer Sciences Department59

Computer Sciences Department60

Computer Sciences Department61

Computer Sciences Department62 THE PUMPING LEMMA FOR CONTEXT-FREE LANGUAGES

Computer Sciences Department63 PROOF

Computer Sciences Department64

Computer Sciences Department65 Explanation

Computer Sciences Department66

Computer Sciences Department67

Computer Sciences Department68

Computer Sciences Department69  The one difference is that the PDA can decide at any point to stop reading ω and begin reading ω R.

Computer Sciences Department70