Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.

Slides:



Advertisements
Similar presentations
Chapter 5 Pushdown Automata
Advertisements

1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
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…
LR-Grammars LR(0), LR(1), and LR(K).
CS21 Decidability and Tractability
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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Theory of Computation What types of things are computable? How can we demonstrate what things are computable?
Transparency No. P2C3-1 Formal Language and Automata Theory Chapter 3 Pushdown Automata and Context-Free Languages.
Transparency No. P2C3-1 Formal Language and Automata Theory Chapter 3 Pushdown Automata and Context-Free Languages.
Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency.
Transparency No. P2C3-1 Formal Language and Automata Theory Chapter 3 Pushdown Automata and Context-Free Languages.
Transparency No. P2C4-1 Formal Language and Automata Theory Part II Chapter 4 Parse Trees and Parsing.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language.
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 4 Updated by Marek Perkowski.
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.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for 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.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
CS 3240: Languages and Computation Pushdown Automata & CF Grammars NOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10. PLEASE REFER TO THE TEXTBOOK.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
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.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
Pushdown Automata.
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 Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
Context Free Grammars.
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.
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 Accepters & Context-Free Grammars Sipser, Theorem 2.12 Denning, Chapter 8.
Chapter 7 Pushdown Automata
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.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
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.
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.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Context-Free Languages
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency.
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.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
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 6: Context-Free Languages
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,
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Formal Language & Automata Theory
Context free grammar.
Context-Free Languages
فصل دوم Context-Free Languages
Context-Free Languages
CSE 105 theory of computation
Presentation transcript:

Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages

Transparency No. P2C1-1 Formal Language and Automata Theory Chapter 1 Context-Free Grammars and Languages (Lecture 19,20)

CFGs and CFLs Transparency No Bacus-Naur Form Regular Expr is too limited to describe practical programming languages. Bacus-Naur Form (BNF) A popular formalism used to describe practical programming languages: Example BNF: (p116) ::= | | | ::= if then else ::= while do ::= begin end ::= | ; ::= := ::= ::= | = | NEQ ::= | | ( ) ::= + | - | * | / ::= 0 | 1 | 2 |... |8 | 9 ::= a | b | c |... |x | y | z

CFGs and CFLs Transparency No Derivations while do while > do while x > do while x > y do …. while x > y do begin x := (x+1); y := (y-1) end Question: how to determine if the string while x > y do begin x := (x+1); y := (y-1) end belongs to the language represented by the above grammar? Sol: Since the string can be derived from the grammar.

CFGs and CFLs Transparency No CFGs Facts: 1. each nonterminal symbol can derive many different strings. 2. Every string in a derivation is called a sentential form. 3. Every sentential form containing no nonterminal symbols is called a sentence. 4. The language L(G) generated by a CFG G is the set of sentences derivable from a distinguished nonterminal called the start symbol of G. (eg. ) 5. A language is said to be context free (or a context free language (CFL)) if it can be generated by a CFG. A sentence may have many different derivations; a grammar is called unambiguous if this cannot happen (eg: previous grammar is unambiguous)

CFGs and CFLs Transparency No CFGs: related facts CFG are more expressive than FAs (and regular expressions) (i.e., all regular languages are context-free, but not vice versa.) Example CFLs which are not regular: {a n b n | n  0} {Palindrome over {a,b}} = {x  {a,b}* | x = rev(x)} {balanced strings of parentheses} Not all sets are CFLs: Ex: {a n b n c n | n  0 } is not context-free.

CFGs and CFLs Transparency No CFGs and CFLs: a formal defintion a CFG is a quadruple G = (N, ,P,S) where N is a finite set (of nonterminal symbols)  is a finite set (of terminal symbols) disjoint from N. S  N is the start symbol. P is a a finite subset of N x (N   )* (The productions) Conventions: nonterminals: A,B,C,… terminals: a,b,c,… strings in (N   )* : ,… Each (A,  )  P is called a production rule and is usually written as: A  . A set of rules with the same LHS: A    A    A     can be abbreviated as A     |   |  .

CFGs and CFLs Transparency No Derivations Let   we say  is derivable from  in one step, in symbols,   G  ( G may be omitted if there is no ambiguity) if  can be obtained from  by replacing some occurrence of a nonterminal symbol A in  with , where A    P; i.e., if there exist        and production    s.t.      and    . Let  * G be the reflexive and transitive closure of  G, i.e., define   0 G  for any    k+1 G  iff there is  s.t.   k G  and   G . Then   * G  iff  k  0 s.t.   k G . Any string in  U    derivable from S (i.e  S    G  is called a sentential form, in particular, if  is a terminal string (i.e.,  *),  is called a sentence.

CFGs and CFLs Transparency No Language generated by a CFG The language generated by G, denoted L(G), is the set L(G) = def { x   * | S  * G x }. A language B   * is a context-free language (CFL) if B = L(G) for some CFG G. Ex 19.1: The nonregular set A= {a n b n | n  0 } is a CFL. Since it can be generated by the grammar G: S   | aSb or more precisely G = (N, ,P,S) where N = {S}  = {a,b} P = { S  , S  aSb } a 3 b 3  L(G) since S  aSb  aaSbb  aaaSbbb  aaabbb.  S  4 aaabbb and S  * aaabbb

CFGs and CFLs Transparency No Techniques for show L = L(G) But how to show that L(G) = A ( = {a n b n | n  0 }) ? a consequence of the following lemmas: Lem 1: S  n+1 a n b n for all n  0. Lemm2: If S  * x ==> x is of the form a k Sb k or a k b k. (in particular, if x is a sentence => x  A ). Pf: of lem 1: by ind. on n. n = 0 ==> S  e. (ok) n = k+1 > 0 : By ind. hyp. S  k+1 a k b k  S  aSb  k+1 a k+1 b k+1.  S  n+1 a n b n. Pf of lem2: by ind. on k s.t. S  k x. k = 0 => S  0 S = a 0 Sb 0. k = t+1 > 0. S  t a m Sb m  a m aSbb m (ok) or  a m b m. (ok).

CFGs and CFLs Transparency No Balanced Parentheses Ex 19.2: The set of palindromes P = { x  {a,b}* | x = rev(x) }. can be generated by the grammar G: S   | a | b | aSa | bSb. cf: The inductive definition of P 1. Initial condition: , a and b are palindromes. 2. recursive condition: If S is a palindrome, then so are aSa and bSb. Balanced Parentheses: Ex1: 2+3x5-4x6 ==> ((2+3)x(5-(4x6))) ==> (( ) ( ( ))) --- balanced parentheses. Ex2: unbalanced parentheses: ( ( )) ( (( )) ))) --- no of “(“  no of “)”. ( ( ) ( ( ) ) )) ((( )) --- unmatched “)” encountered.

CFGs and CFLs Transparency No Balanced Parentheses Formal definition: let   { [, ] }. Define L,R:  *  N as follows: L(x) = number of “[“ in x. R(x) = number of “]” in x. a string x   * is said to be balanced iff (i) L(x) = R(x) -- equal # of left and right parentheses. (ii) for all prefix y of x, L(y)  R(y). --- no dangling right parenthesis. Now define PAREN = { x  { [, ] }* | x is balanced }. Thm 20.1 : PAREN can be generated by the CFG G: S   | [S] | S S pf: 1. L(G)  PAREN. Lem1: If S  * x then x is balanced. in particular, if x contains no S => x  PAREN.  L(G)  PAREN.

CFGs and CFLs Transparency No proof of theorem 20.1 pf of lem1 : by ind. on k s.t. S  k x. k = 0 => S  0 S = x is balanced. k = t + 1 > 0: ==> S  t ySz  yz (1) or  y[S]z (2) or  ySSz (3). By ind. hyp., ySz is balanced. => L(yz) = L(ySz) = R(ySz) = R(yz) and if y = wu => L(w)  R(w) since w is also a prefix of ySz. if z = wu => L(yw) = L(ySw)  R(ySw) = R(yw).  yz is balanced. Case (2) and (3) can be proved similarly.

CFGs and CFLs Transparency No Proof of theoreom 20.1 (cont’d) Pf: PAREN  L(G) (i.e., if x is balanced ==> S  * x. ) By ind. on |x|. 1. |x| = 0 ==> x =  ==> S   (ok). 2. |x| > 0. Then either (a)  a proper prefix y of x that is balanced or (b) All proper prefixes y of x are not balanced. In case (a), we have x = y z with |y|,|z| < |x| for some z. => L(z) = L(x) - L(y) = R(x) - R(y) = R(z) For all prefix w with z = w w’: L(w) = L(yw) - L(y)  R(yw) - R(y) = R(w) ==> both y and z are balanced ==> by ind. hyp., S  * y and S  * z ==> S  SS  * yS  *yz. In case (b): x = [z] for some z (why ?) Moreover it can be shown that z is balanced too. Hence S  * z. ==> S  * [S]  * [z] = x. QED

CFGs and CFLs Transparency No Pushdown Automata: a preview FAs recognize regular languages. What kinds of machines recognize CFLs ? ===> Pushdown automata (PDAs) PDA: Like FAs but with an additional stack as working memory. Actions of a PDA 1. Move right one tape cell (as usual FAs) 2. push a symbol onto stack 3. pop a symbol from the stack. Actions of a PDA depends on 1. current state 2. currently scanned I/P symbol 3. current top stack symbol. A string x is accepted by a PDA if it can enter a final state (or clear all stack symbols) after scanning the entire input. More details defer to later chapters.