Presentation is loading. Please wait.

Presentation is loading. Please wait.

Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.

Similar presentations


Presentation on theme: "Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy."— Presentation transcript:

1 Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy of Prof. Xinhua Zhuang at Dept. of CECS at University of Missouri-Columbia Spring 2016 CISG 5115 Theory of Computation

2 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.1 Context-free Grammars 2

3 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University More Powerful Method of Describing Languages? Chapter 1 introduced two different, but equivalent, methods of describing languages: finite automata and regular expressions Many languages can be described in this way but some simple languages cannot This chapter introduces “context-free grammars”, which describe certain features that have a recursive structure Context-free languages (including regular languages) Pushdown automata (recognizes context-free languages) 3

4 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example One of the variables is designated as the start variable (i.e. A in the example) The grammar generates the string 000#111 Derivation method Parse tree method All language generated in this way constitute the language of the grammar Any language that can be generated by context-free grammar is called a context-free language 4 terminals variables Context-free grammar substitution rules (also called as productions)

5 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Context Free Grammar (CFG) Formal Definition V : finite set of variables (nonterminals) : finite set of terminals. : a special start variable, R : finite set of productions of form, where (rules) Conventions 5

6 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Language of a Grammar Formal Definition Define yield, Define such that “A finite # of application of productions" The language of a grammar G, L ( G ) All strings of terminals derivable from S L is a CFL iff CFG G such that L = L ( G ) Parse tree: a tree structured representation of a derivation. 6

7 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Designing CFG Union of simpler CFGs For strings of form X or Y, use where A derives X and B derives Y. Repeated symbols Equal number of symbols 7

8 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Designing CFG Example Find a grammar for the language How to Find a grammar for Merge the two grammars 8

9 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity A grammar can generate the same string in many ways Depending on its derivation procedure, each string may have different meaning Undesirable for certain applications such as programming language ( + may calculated earlier than ) 9

10 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity – cont’ A string is derived ambiguously in G if it has two or more leftmost derivations. G is ambiguous if it generates some strings ambiguously. A language L is inherently ambiguous if G such that L ( G ) = L, G is ambiguous. Ex (p. 2.29) : 10

11 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity – cont’ Leftmost derivation: a way to remove ambiguity in CFG Replace the leftmost variable at every step Each parse tree has a unique leftmost derivation. CFG: How to derive ? 11

12 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Normal Forms Write grammars in simpler forms. Eliminate -productions unit productions useless symbols Examples Chomsky normal form (CNF) Greibach Normal Form (GNF) 12

13 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Chomsky Normal Form (CNF) Every production is of the form where Also Theorem: Any CFL is generated by a CFG in CNF. Proof: Convert any G into CNF. 13

14 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Convert a CFG G into CNF 1.Add a new start symbol 2.Eliminate -productions 3.Eliminate unit productions 4.Eliminate useless symbols 5.Convert rules into proper form 14

15 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 15 1.Add a new start symbol

16 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 16 2.Eliminate -productions

17 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 17 3.Eliminate unit productions

18 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 18 3.Eliminate unit productions

19 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 19 4.Eliminate useless symbols 5.Convert rules into proper form

20 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.2 Pushdown Automata 20

21 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) PDA: NFA with an extra component called a stack Stack: unlimited storage, LIFO (last-in first-out) Pushing a symbol Popping a symbol The stack provides additional memory beyond the finite amount available in the control PDA can write symbols on the stack and read them back later Schematic of FA and PDA 21 aabb state control Input tape aabb state control Input tape xyz stack

22 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) – cont’ PDA may be nondeterministic Deterministic and nondeterministic PDA are not equivalent in power How PDA recognize ? (informally) Read symbols from the input. As each 0 is read, push it onto the stack. As soon as 1s are seen, pop a 0 off the stack for each 1 read. If reading the input is finished exactly when the stack becomes empty of 0s, accept the input. Otherwise, reject it. 22

23 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) Formal Definition 23

24 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Procedure of Converting CFG to PDA Push special mark $ and the start symbol S to the stack Repeat forever if the top of the stack is variable A : (apply a rule for A ) substitute A by the right hand side (RHS) of a rule. terminal a : (check applicability) compare the next input symbol with a If match, repeat. If not match, stop the branch. symbol $: enter the accept state. 24 state control A1A stack 11000 1 0$ input Intermediate string: 01A1A0

25 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 Following is the formal description of the PDA that recognizes the language. Let be, where,,,, and is given by the following table, wherein blank entries signify. 25 Input01 stack0$0$0$

26 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 – cont’ Following is the state diagram of the PDA that recognizes the language. 26

27 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 – cont’ Following is the state diagram of the PDA that recognizes the language 27

28 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 2 28

29 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University PDA: Equivalence with CFG 29

30 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof 30

31 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ For simplicity, following modifications are enforced on P. 1.It has a single accept state,. 2.It empties its stack before accepting. 3.Each transition either pushes a symbol onto the stack or pops on off the stack, but it does not do both at the same time. Features 1 and 2 are easy to enforce. To give it Feature 3, we replace each transition that simultaneously pops and pushes with a two transition sequence that goes through a new state, each transition that neither pops nor pushes with a two transition sequence that pushes then pops an arbitrary stack symbol. 31

32 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ For any input string x, P ’s first move on x must be a push. The last move on x must be a pop, because the stack ends up empty. Two Possible Cases Either the symbol popped at the end is the symbol that was pushed at the beginning => stack became empty only at the end of P ’s computation on x : or not => stack became empty in the middle: 32

33 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ Two Possible Cases Either the symbol popped at the end is the symbol that was pushed at the beginning => stack became empty only at the end of P ’s computation on x : 33

34 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ Two Possible Cases or not => stack became empty in the middle: 34

35 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof of Lemma 2.27 Let Variables of G are, and the start variable is Transition Rules G : For each,, and, if contains and contains, put the rule in G For each, put the rule in G. Finally, for each, put the rule in G. 35

36 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.3 Non-context-free Languages 36

37 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Big Picture 37

38 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Non-Context-Free Languages 38

39 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof 39

40 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof – cont’ 40

41 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof – cont’ 41 For Condition 3, we need to be sure that vxy has length at most p. In the parse tree for s, the upper occurrence of R generates vxy. We choose R so that both occurrences fall within the bottom | V |+1 variables on the path (After R, no variable is repeated) and we chose the longest path in the parse tree, so the subtree where R generates vxy is at most | V |+1 high. A tree of this height can generate a string of length at most

42 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Using Pumping Lemma to Prove Languages not Context-Free Pumping lemma specifies a property that all CFL have. If a language L doesn't have this property, then L is not CFL. Using pumping lemma to prove non-context-free Assume L is a CFL and obtain a contradiction using conditions in pumping lemma: Choose a long string Show that s cannot be pumped. Examples not context free 42

43 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma 43

44 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma – cont’ 44

45 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma – cont’ 45

46 Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Closure Properties of CFLs CFLs are closed under union, concatenation, Kleene Closure (*), intersection with regular languages, … CFLs are not closed under intersection, complement, … 46


Download ppt "Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy."

Similar presentations


Ads by Google