Lecture 6: 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
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,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CS21 Decidability and Tractability
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
CFGs and PDAs Sipser 2 (pages ). Last time…
Introduction to Computability Theory
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.
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.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 4 Updated by Marek Perkowski.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS 490: Automata and Language Theory Daniel Firpo Spring 2003.
Normal forms for Context-Free Grammars
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Chapter 7 PDA and CFLs.
::ICS 804:: Theory of Computation - Ibrahim Otieno SCI/ICT Building Rm. G15.
CS490 Presentation: Automata & Language Theory Thong Lam Ran Shi.
1 Theory of Digital Computation Course material for undergraduate students on IT Department of Computer Science University of Veszprem Veszprem, Hungary.
CS/IT 138 THEORY OF COMPUTATION Chapter 1 Introduction to the Theory of Computation.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Pushdown Automata (PDAs)
Context-free Languages
Context-free Languages Chapter 2. Ambiguity.
Introduction to Language Theory
Part VII. Models for Context-Free Languages 1/50.
Context-Free and Noncontext-Free Languages Chapter 13 1.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Context-Free and Noncontext-Free Languages Chapter 13 1.
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.
Context-Free and Noncontext-Free Languages Chapter 13.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Lecture 5: Finite Automata 虞台文 大同大學資工所 智慧型多媒體研究室.
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.
Lecture 2: Limiting Models of Instruction Obeying Machine 虞台文 大同大學資工所 智慧型多媒體研究室.
1 Alphabets: An Alphabet is a finite set of symbols. We will usually use  to denote the alphabet of input symbols or “terminal characters.” String: A.
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.
1.Draw a parse tree for the following derivation: S  C A C  C A b b  b b A b b  b b B b b  b b a A a a b b  b b a b a a b b 2. Show on your parse.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
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.
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.
Week 14 - Friday.  What did we talk about last time?  Simplifying FSAs  Quotient automata.
CS6800 Advance Theory of Computation Spring 2016 Nasser Alsaedi
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
CSE 105 theory of computation
PDAs Accept Context-Free Languages
Chapter Thirteen: Stack Machines
Context-Free Grammars
Context-Free Languages
CSE 105 theory of computation
Lecture 5: Turning Machine
CSE 105 theory of computation
Presentation transcript:

Lecture 6: Context-Free Languages 虞台文 大同大學資工所 智慧型多媒體研究室

Content Language Recognizer vs. Language Generator Context-Free Grammars Regular Languages vs. Context-Free Languages Pushdown Automata Pushdown Automata vs. Context-Free Grammars Properties of Context-Free Languages Context-Sensitive Grammars

Lecture 6: Context-Free Languages Language Recognizer vs. Language Generator 大同大學資工所 智慧型多媒體研究室

Recognizer vs. Generator Parsing Machines Grammas Generation

Example: Regular Expressions Is the expression power of r. e. strong enough? Example: Regular Expressions A rule (grammar) to generate strings  Next, generate any number of a’s or any number b’s Finally, generate an b First, generate an a

Regular Expressions As Grammars S, M, A, B  nonterminals Production Rules S  start symbol a, b  terminals

Regular Expressions As Grammars Example:

Lecture 6: Context-Free Languages Context-Free Grammars 大同大學資工所 智慧型多媒體研究室

Definition (V, , P, S) A context-free grammar G is a quadruple where V : finite set of nonterminals V   =   : finite set of terminals P : finite set of rules, i.e., subset of V (V )* S : start symbol, SV In the form of A with AV and (V )*

Notation Conventions The capital letters A, B, C, D, E, and S denote nonterminals; S is the start symbol. The lower-case letters a, b, c, d, e, digits and boldface strings are terminals. The capital letters X, Y, and Z that may be either terminals or variables. The lower-case letters u, v, w, x, y, and z denote strings of terminals. The lower-case Greek letters , , and  denote strings of variables and terminals.

Notation Conventions reflexive and transitive closure of a derivation of G of m from 1.

Context-Free Languages The language generated by G = (V, , P, S), denoted as L(G), is

Example L(G)=? G = (V, , P, E) E: Expression T: Term F: Factor

Example L(G)=? R1 R2 R3 R4 R5 R6 R7

Example L(G)=? L(G)=? x1 x1*(x2*x1+x1) x1*(x2*(x1+x1)) x1x2+x1+x2

Example L(G)=? G = (V, , P, S) L(G)={anbn|n0} V={S} ={a, b} S  aSb  aaSbb  a3Sb3      anSbn  anbn

Example L(G)=? G = (V, , P, S) L(G)={anbn|n0} Is the expression power of context-free grammar stronger than regular expression? V={S} ={a, b} L(G)={anbn|n0} S  aSb S   P= S  aSb  aaSbb  a3Sb3      anSbn  anbn

Lecture 6: Context-Free Languages Regular Languages vs. Context-Free Languages 大同大學資工所 智慧型多媒體研究室

Definition  Regular Grammar A CFG G = (V, , P, S) is regular iff P  V  *(V{}). tailing nonterminal nonterminal leading terminal(s) RHS LHS

Example G is regular. Consider G = (V, , P, S) with V={S, A, B} S  bA S  aB A  abaS B  babS S   P=

Regular Grammars vs. Regular Languages Consider G = (V, , P, S) with G is regular. V={S, A, B} ={a, b} S  bA  babaS  · · · S  bA S  aB A  abaS B  babS S   P= S  aB  ababS  · · · Regular Expression L(G)=(abab + baba)*

Theorem Let L be a language. L is regular iff L is generated by a regular grammar. accepted by a DFA

L is regular iff L is generated by a regular grammar. Theorem L is regular iff L is generated by a regular grammar. Pf) “” Given a DFA M = (K, , , s, F), we will construct a regular grammar, say, G st. L(M) = L(G). Consider G = (K, , P, S) st. From the correspondence btw. M and G, it is easily seen that . . . Hence,

L is regular iff L is generated by a regular grammar. Theorem L is regular iff L is generated by a regular grammar. Pf) “” Given a regular grammar G = (V, , P, S), we will construct an NFA, say, M st. L(G) = L(M). Consider M = (K, , , s, F) st. From the correspondence btw. M and G, it is easily seen that . . . Hence,

Example G is regular. Consider G = (V, , P, S) with V={S, A, B} f B S  bA S  aB A  abaS B  babS S   P= a bab  b S > A aba

The Chomsky Hierarchy Chomsky Hierarchy Languages Grammars Automaton Type 0 Recursively enumerable unrestricted Turing Machine Recursive Decider Type 1 Context-Sensitive Linear-Bounded Automaton Type 2 Context-Free Push-Down Automaton Type 3 Regular NFA or DFA

The Chomsky Hierarchy Non-recursively enumerable Context-sensitive Context-free Regular

Lecture 6: Context-Free Languages Pushdown Automata 大同大學資工所 智慧型多媒體研究室

Stack or Pushdown store Basic Concept How to accept language L={wwR: w*}? a b Finite Control Reading head Input tape Stack or Pushdown store

Definition (K, , , , s, F) A pushdown automata is a sextuple where K : a finite set of states  : a finite set of input symbols  : a finite set of stack symbols  : the transition function K **  K * s K : the initial state F K : the set of final states

Definition  More Precise Version A PDA is possibly nondeterministic. Definition  More Precise Version A pushdown automata is a sextuple (K, , , , s, F) where K : a finite set of states  : a finite set of input symbols  : a finite set of stack symbols  : the transition function K **  2K * s K : the initial state F K : the set of final states

The Transition Function of PDA K **  K * current state symbol(s) under reading head symbol(s) on the top of stack next state stack symbol(s) replaced

The Transition Function of PDA :K **  K * The Transition Function of PDA (p, v, ) (q, ) v w u p q finite control   v w u p q finite control  

Stack Operations (p, v, ) (q, ) Case 1:   ,    replacement push Case 2:   ,  =  Case 4:  = ,  =  Stack Operations pop none (p, v, ) (q, ) v w u p q finite control   v w u p q finite control  

Yields in One Step if (p, v, ) (q, )

in some (including zero) steps Yields * in some (including zero) steps if

Languages Accepted by PDA’s M = (K, , , , s, F)

Example A deterministic PDA

Example t1 t2 t3 t4 t5 state input stack transition abbcbba s  - f 3 ba f 5 a f 5  f 4

How to design a PDA to accept L = {wwR}? Example state input stack transition How to design a PDA to accept L = {wwR}? t1 t2 t3 t4 t5 abbcbba s  - bbcbba s a 1 bcbba s ba 2 cbba s bba 2 bba f 3 ba f 5 a f 5  f 4

Example t1 t2 t3 t4 t5 state input stack transition s abbbba  - s f bba bba 3 f ba ba 5 f a a 5 f   4

A nondeterministic PDA Example t1 t2 t3 t4 t5 A nondeterministic PDA

Lecture 6: Context-Free Languages Pushdown Automata vs. Context-Free Grammars 大同大學資工所 智慧型多媒體研究室

Leftmost and Rightmost Derivations Given a context-free grammar there may be several ways of rewriting its nonterminals to arrive at precisely the same string. Two systematic ways of derivations are Leftmost derivation Rightmost derivation

Example V={S, A, B}, ={a, b, c} Consider G = (V, , P, S) with Note that there are other derivations that are neither leftmost nor rightmost. Example V={S, A, B}, ={a, b, c} Consider G = (V, , P, S) with S  aABb A  bAb A  c B  aB B  b P= Rightmost Derivation Leftmost Derivation S  aABb  abAbBb  abcbBb  abcbaBb  abcbabb L S  aABb  aAaBb  aAabb  abAbabb  abcbabb R

See textbook for the proof Lemma For any context-free grammar G = (V, , P, S) and any string w *, See textbook for the proof

 Context-Free Grammar Theorem The class of languages accepted by pushdown automata is exactly the class of context-free languages. Pf) Context-Free Grammar  Pushdown Automaton G = (V, , P, S) M = ? Pushdown Automaton  Context-Free Grammar M = (K, , , , s, F) G = ?

Theorem Pf) G = (V, , P, S)  M = ({p, q}, , V , , p, {q}) M = ? The class of languages accepted by pushdown automata is exactly the class of context-free languages. Theorem Pf) Context-Free Grammar  Pushdown Automaton G = (V, , P, S)  M = ({p, q}, , V , , p, {q}) M = ? Transition Function Stack Symbols Input Symbols Start State Final State States

One can show that L(M)=L(G). The class of languages accepted by pushdown automata is exactly the class of context-free languages. Theorem Pf) Context-Free Grammar  Pushdown Automaton G = (V, , P, S)  M = ({p, q}, , V, , p, {q}) M = ? One can show that L(M)=L(G). (see text)

 Context-Free Grammar The class of languages accepted by pushdown automata is exactly the class of context-free languages. Theorem Pf) Pushdown Automaton  Context-Free Grammar M = (K, , , , s, F) G = ? see text

Lecture 6: Context-Free Languages Properties of Context-Free Languages 大同大學資工所 智慧型多媒體研究室

Review  Properties of Regular Languages The regular languages are closed under: union; concatenation; Keene star; complementation; intersection.

Properties of Context-Free Languages The context-free languages are closed under: union; concatenation; Keene star; complementation; intersection. ? ? ? ? ?

Properties of Context-Free Languages The context-free languages are closed under: union; concatenation; Keene star.

Properties of Context-Free Languages The context-free languages are closed under: union; concatenation; Keene star.

Properties of Context-Free Languages The context-free languages are closed under: union; concatenation; Keene star.

Review: The Pumping Theorem for Regular Languages Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uvnwL for all n  0.

What CFG’s Can? What CFG’s cannot? Which of the following languages are context-free?

The Pumping Theorem for Context-Free Languages See textbook for the proof. The Pumping Theorem for Context-Free Languages Let G be context-free gramma. Then there is a number K, depending on G, such that any w  L(G) with |w|>K can be rewritten as w = uvxyz in such a way that either v or y is nonempty and uvnxynz  L(G) for all n  0.

Theorem is not context-free. Pf) u v x y Let Let G be context-free gramma. Then there is a number K, depending on G, such that any w  L(G) with |w|>K can be rewritten as w = uvxyz in such a way that either v or y is nonempty and uvnxynz  L(G) for all n  0. Theorem is not context-free. Pf) Let u v x y It is impossible for us to rewrite w as w = uvxyz such that |vy|>0 and uvnxynz  L(G) for all n  0.

Theorem The context-free languages is not closed under intersection or complementation.

 CFL’s are not closed under intersection. Theorem The context-free languages is not closed under intersection or complementation. Pf) Facts: context-free  not context-free  CFL’s are not closed under intersection. Fact: If CFL’s are closed under complementation, they would be also closed under intersection.

Theorem: Algorithmic Properties See textbook for the proof. Theorem: Algorithmic Properties There are algorithms for answering the following questions about context-free grammars: Given a grammar G, and a string w, is wL(G)? Is L(G)=? How? What is compiler-compiler?

The Chomsky Hierarchy Chomsky Hierarchy Languages Grammars Automaton Type 0 Recursively enumerable unrestricted Turing Machine Recursive Decider Type 1 Context-Sensitive Linear-Bounded Automaton Type 2 Context-Free Push-Down Automaton Type 3 Regular NFA or DFA

The Chomsky Hierarchy Non-recursively enumerable Context-sensitive Context-free Regular

Lecture 6: Context-Free Languages Context-Sensitive Grammars 大同大學資工所 智慧型多媒體研究室

Definition  Context-Free Grammar (Review) A context-free grammar G is a quadruple (V, , P, S) where V : finite set of nonterminals V   =   : finite set of terminals P : finite set of rules in the form of A with AV and (V )* S : start symbol, SV

Definition  Context-Sensitive Grammar (I) A context-sensitive grammar G is a quadruple A context-free grammar G is a quadruple (V, , P, S) where V : finite set of nonterminals V   =   : finite set of terminals P : finite set of rules in the form of A with AV and (V )*    with , (V )+ and ||  ||. S : start symbol, SV

Definition  Context-Sensitive Grammar (II) A context-sensitive grammar G is a quadruple A context-free grammar G is a quadruple (V, , P, S) where V : finite set of nonterminals V   =   : finite set of terminals P : finite set of rules in the form of A with AV and (V )*    A   with , (V )*, AV and (V )+. with , (V )+ and ||  ||. S : start symbol, SV

Definition  Context-Sensitive Grammar (II) A language L is said to be context-sensitive if there exists a context-sensitive grammar G, such that L = L(G) or L = L(G){}

Is it context-sensitive? Review is not context-free. Is it context-sensitive?

Example Consider G = (V, , P, S) with V={S, A, B} ={a, b, c} L(G)=?

Example Consider G = (V, , P, S) with V={S, A, B} ={a, b, c}

Example Consider G = (V, , P, S) with V={S, A, B} ={a, b, c}

The Chomsky Hierarchy Chomsky Hierarchy Languages Grammars Automaton Type 0 Recursively enumerable unrestricted Turing Machine Recursive Decider Type 1 Context-Sensitive Linear-Bounded Automaton Type 2 Context-Free Push-Down Automaton Type 3 Regular NFA or DFA

The Chomsky Hierarchy Non-recursively enumerable Context-sensitive Context-free Regular

Exercises Construct pushdown automata for the following languages over the alphabet {a, b}: {w | w has an equal number of a’s and b’s } {w | the length of w is odd and its middle symbol is a b} Prove that the following languages are not context free: {aibjck | 0 < i < j < k} {wwRw | w  {a,b}*} Construct context-free grammars that define the following languages over {a, b}: {ambn | m  n} {aibjck | i + j = k}