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
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
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…
CFGs and PDAs Sipser 2 (pages ). Last time…
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
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
CS 302: Discrete Math II A Review. An alphabet Σ is a finite set (e.g., Σ = {0,1}) A string over Σ is a finite-length sequence of elements of Σ For x.
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 Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 Computer Language Theory Chapter 2: Context-Free Languages.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Context-free Languages
Lecture Pushdown Automata. stack stack head finite control tape head tape.
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.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages Discourse 04 Context-Free Grammars and Pushdown Automata.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Theory of Languages and Automata By: Mojtaba Khezrian.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Context-Free and Noncontext-Free Languages Chapter 13.
CSCI 2670 Introduction to Theory of Computing September 22, 2004.
Normal Forms (Chomsky and Greibach) Pushdown Automata (PDA) Intro PDA examples MA/CSSE 474 Theory of Computation.
Foundations of Computing Science
Context-Free Grammars: an overview
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
CSE 105 theory of computation
Lecture 17 Oct 25, 2011 Section 2.1 (push-down automata)
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Pushdown Automata Reading: Chapter 6.
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Context-Free Languages
فصل دوم Context-Free Languages
COSC 3340: Introduction to Theory of Computation
CS21 Decidability and Tractability
Context-Free Languages
Chapter 2 Context-Free Language - 01
COSC 3340: Introduction to Theory of Computation
CS21 Decidability and Tractability
Chapter 2 Context-Free Language - 02
Decidability and Tractability
Computer Language Theory
CS21 Decidability and Tractability
CSE 105 theory of computation
Context-Free Grammars
CSE 105 theory of computation
Presentation transcript:

Context-free Languages Foundations of Computing Science Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Context-free Grammar (CFG) A context-free grammar (CFG) is a 4-tuple (V, Σ, R, S), where V is a finite set called the variables Σ is a finite set, disjoint from V, called the terminals R is a finite set of rules, with each rule being a variable and a string of variables and terminals S ∈ V is the start variable Few Terminologies / Notions: If u, v and w are strings of variables and terminals, and A  w is a rule of the grammar, we say that uAv yields uwv, written as uAv => uwv u derives v, written as u => v, if u = v or if a sequence u1, u2, …, uk exists for k ≥ 0 and u => u1 => u2 => … => uk => v The language generated by some context-free grammar (CFG), G, is called the context-free language (CFL), L(G) = {w ∈ Σ* | S => w} * * INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Example of Context-free Grammars G1 = ( {S}, { ( , ) }, R, S ), where set of rules (R), is S  (S) | SS | ε Here, L(G1) = all strings of properly nested parenthesis Example-2: G2 = ( V, Σ, R, <EXPR> ), where V is {<EXPR>, <TERM>, <FACTOR>} and Σ is {a, +, ×, (, )}. The rules (R) are <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM> × < FACTOR > | < FACTOR > <FACTOR>  ( <EXPR> ) | a <EXPR> <TERM> <FACTOR> a + ( ) × Parse Tree for the string ( a + a ) x a INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Designing Context-free Grammars Example: Let L(G3) = equal number of 1s and 0s follow each other = {0n1n | n ≥ 0} U {1n0n | n ≥ 0} The grammar for the language {0n1n | n ≥ 0} is S1  0S11 | ε and the grammar for the language {1n0n | n ≥ 0} is S2  1S20 | ε Therefore, The complete grammar for the grammar L(G3) is S  S1 | S2 ; S1  0S11 | ε ; S2  1S20 | ε Designing CFG for Regular Languages: Regular Languages  DFA If δ(qi , a) = qj is a transition in DFA; add the rule Ri  aRj to CFG Add the rule Ri  ε to CFG if qi is an accept state Make R0 the start variable of CFG, where q0 is the start state of DFA INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Ambiguity A string w is derived ambiguously in context-free grammar G iff it has two or more different leftmost derivations Grammar G is ambiguous iff it generates some string ambiguously Example: Consider the grammar G4: <EXPR>  <EXPR> + <EXPR> | <EXPR> × <EXPR> | (<EXPR>) | a G4 generates the string a + a × a ambiguously <EXPR> a + a × a a + a × a <EXPR> Two Parse Trees for the Same String a + a × a INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Chomsky Normal Form A context-free grammar is in Chomsky Normal Form iff every rule is of the form A BC and A  a, where a is any terminal A, B and C are any variables – except that B and C may not be start variable In addition, we permit the rule S  ε, where S is the start variable Theorem: Any context-free language is generated by a context-free grammar in Chomsky normal form INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Any CFG  Chomsky Normal Form A Transformation Example: S0ASA|aB|a|SA|AS SASA|aB|a|SA|AS AB|S|b Bb S0S|ASA|aB|a|SA|AS SASA|aB|a|SA|AS AB|S Bb SASA|aB AB|S Bb|ε S0ASA|aB|a|SA|AS SASA|aB|a|SA|AS AS|b|ASA|aB|a|SA|AS Bb S0S SASA|aB AB|S Bb|ε S0S SASA|aB|a|SA|AS|S AB|S Bb S0AA1|UB|a|SA|AS SAA1|UB|a|SA|AS Ab|AA1|UB|a|SA|AS A1SA Ua Bb S0S SASA|aB|a AB|S|ε Bb|ε S0S SASA|aB|a|SA|AS|S AB|S|ε Bb INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Pushdown Automaton (PDA) A pushdown automata (PDA) is a 6-tuple (Q, Σ, Γ, δ, q0, F), where Q, Σ, Γ and F are all finite sets Q is the set of states Σ is the input alphabet Γ is the stack alphabet δ : Q × Σε × Γε  P(Q × Γε) is the transition relation q0 ∈ Q is the start state F ⊆ Q is the set of accept states INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Examples of PDA Let the PDA M1 be (Q, Σ, Γ, δ, q1, F), where Q = {q1, q2, q3, q4} Σ = {0, 1} Γ = {0, $} F = {q1, q4} δ is given by PDA M1 recognizes the language, L(M1) = {0n1n | n ≥ 0} Input 1 ε Stack $ q1 {(q2, $)} q2 {(q2, 0)} {(q3, ε)} q3 {(q4, ε)} q4 q2 q3 q1 q4 ε, ε$ 1, 0ε 0, ε0 ε, $ε

Acceptance/Recognition by PDA A pushdown automaton M = (Q, Σ, Γ, δ, q0, F) accepts input w if w can be written as w = w1w2…wm, where each wi ∈ Σε Sequences of states r0, r1, …, rm ∈ Q and strings s0, s1, …, sm ∈ Γ* exists (the strings si represent the sequence of stack contents that M has on the accepting branch of the computation) The following three conditions are satisfied: r0 = q0 and s0 = ε [M starts out properly, in the start state and with an empty stack] For i = 0, 1, …, m-1; we have (ri+1, b) ∈ δ(ri, wi+1, a), where si = at and si+1 = bt for some a, b ∈ Γε and t ∈ Γ* [M moves properly according to the state, stack & next input symbol] rm ∈ F [an accept state occurs at the input end] Theorem: A language is context-free if and only if some pushdown automaton recognizes it Every regular language is context-free INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Pumping Lemma for CFL 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 following conditions: For each i ≥ 0, uvixyiz ∈ A |vy| > 0, and |vxy| ≤ p Examples: The following languages (denoted by B, C, D) are not context-free: B = {anbncn | n ≥ 0} C = {aibjck | 0 ≤ i ≤ j ≤ k} D = {ww | w ∈ {0,1}*} INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR