فصل دوم Context-Free Languages

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

Chapter 5 Pushdown Automata
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata CPSC 388 Ellen Walker Hiram College.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 Computer Language Theory Chapter 2: Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CS 461 – Sept. 26 CFG correctness Section 2.2 – Pushdown Automata.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Pushdown Automata (PDAs)
Context-free Languages
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Theory of Languages and Automata By: Mojtaba Khezrian.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
6. Pushdown Automata CIS Automata and Formal Languages – Pei Wang.
Formal Languages, Automata and Models of Computation
Pushdown Automata.
Closed book, closed notes
Context-Free Grammars: an overview
G. Pullaiah College of Engineering and Technology
CSE 105 theory of computation
CSE 105 theory of computation
CIS Automata and Formal Languages – Pei Wang
Chapter 7 PUSHDOWN AUTOMATA.
Lecture 17 Oct 25, 2011 Section 2.1 (push-down automata)
Context Free Languages
Intro to Theory of Computation
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Pushdown automata a_introduction.htm.
CS21 Decidability and Tractability
Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
CS21 Decidability and Tractability
Chapter 2 Context-Free Language - 02
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Computer Language Theory
CS21 Decidability and Tractability
… NPDAs continued.
CSE 105 theory of computation
CSE 105 theory of computation
Normal Forms for Context-free Grammars
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

فصل دوم Context-Free Languages نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف پاییز 86

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Table Of Content Context-Free Grammars Pushdown Automata Non-Context-Free Languages پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Context-Free Grammars A collection of substitution rules (production) Each rule → A line in the grammar Each line → A variable, an arrow, a string The string → variables and terminals پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Context-Free Grammars (Example) An Example: (G1) Three rules Variables: A,B Start Variable: A Terminals: 0,1,# پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Derivation Generating the strings of the language: G1: پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Parse Tree پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Context-Free Language All Strings generated in this way L(G1) : G2 (describes a fragment of the English language): پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف A Derivation Example Derivation of the sentence “a boy sees” in G2 پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Context-Free Language (Formal Definition) پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Context-Free Language (Formal Definition) u derives v The language of the grammar: پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples G3: R (Set of Rules): Strings such as: abab, aaabbb, aababb If: a→( and b →), then: L(G3) → language of all strings of properly nested parentheses پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples G4: V: Σ: R (Set of Rules): پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Examples (G4) پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Designing Context-Free Grammars Break the CFL into simpler pieces, if possible Construct grammar for each piece Combine the rules and the new rule S→S1|S2|…|Sk, where the Si are the start variables for the individual grammars پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example Language: : Add the rule: to give the grammar: پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Designing Context-Free Grammars Any DFA can be converted to an equivalent CFG Make a variable Ri for each state qi Add the rule , if Add the rule , if qi is an accept state پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Ambiguity When a grammar can generate the same string in different ways Undesirable for applications such as programming languages پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example This grammar generates the string ambiguously پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Ambiguity Leftmost derivation: at every step, the leftmost remaining variable is replaced. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف CHOMSKEY NORMAL FORM Any context-free language is generated by a context-free grammar in chomskey normal form. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example Step 1: (make a new start variable) Step 2: Remove ε rules B→ε , shown on the left and A→ε , shown on the right پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) Step 3a: Remove unit rules S→S, shown on the left and S0→S, shown on the right Step 3b: Remove unit rules A→B and A→S پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) Step 4: convert the remaining rules into the proper form by adding variables and rules پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA Like nondeterministic finite automata, with an extra component: stack The stack provides additional memory Pushdown automata are equivalent in power to context-free پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف PUSHDOWN AUTOMATA Push: Writing a symbol on the stack Pop: Removing a symbol Recognizes {0n1n|n≥0} As each 0 is read from the input, push it onto the stack. Pop a 0 off the stack for each 1 read. Reading the input is finished exactly when the stack becomes empty→Accept پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

PUSHDOWN AUTOMATA (formal definition) پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example L={0n1n|n≥0} Q={q1,q2,q3,q4} Σ={0,1} Γ={0,$} F={q1,q4} δ: پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Equivalence with context-free grammars A language is context free if and only if some pushdown automaton recognizes it. Proof Idea: Let A be a CFL. Show how to convert the A’s CFG, G into an equivalent PDA, P. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof Any terminal symbols appearing before the first variable are matched with input. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.) (r,u)∈δ(q,a,s)→When q is the state of the automaton, a is the next input symbol, and s is on the top of the stack, push the string u onto the stack and go on to the state r. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.) Q={qstart,qloop,qaccept} E Step 1: δ(qstart,ε,ε)={(qloop,S$)} Step 2: (a) δ(qloop,ε,A)={(qloop,w)|where A→w is a rule} (b) δ(qloop,a,a)={(qloop,ε)} (c) δ(qloop,ε,$)={(qaccept,ε)} پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Proof (cont.) پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

Relationship of the regular and context-free languages Every regular language is context-free. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

NON-CONTEXT-FREE LANGUAGES Pumping Lemma for context-free languages: If A is a context-free language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s may be divided into five pieces s=uvxyz satisfying the conditions: For each i≥0, uvixyiz∈A |vy|>0 |vxy|≤p پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example B={anbncn|n≥0} Assume that B is a CFL and p is the pumping length. s=apbpcp: a member of B and of length at least p We show that it can not be pumped پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف Example (cont.) s=uvxyz Condition1 v and y contain one type pf symbol→ uv2xy2z cannot contain equal number of a’s, b’s and c’s. Either v or y contain more than one type of symbol→ uv2xy2z cannot contain a’s, b’s and c’s in the correct order. پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف

نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف End of chapter 2  پاییز 86 نظریه زبان ها و ماشین ها دانشگاه صنعتی شریف