Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6: Context-Free Languages

Similar presentations


Presentation on theme: "Lecture 6: Context-Free Languages"— Presentation transcript:

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

2 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

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

4 Recognizer vs. Generator
Parsing Machines Grammas Generation

5 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

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

7 Regular Expressions As Grammars
Example:

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

9 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 )*

10 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.

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

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

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

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

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

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

17 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

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

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

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

21 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)*

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

23 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,

24 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,

25 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

26 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

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

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

29 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

30 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

31 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

32 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

33 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

34 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

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

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

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

38 Example A deterministic PDA

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

40 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

41 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

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

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

44 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

45 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

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

47  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 = ?

48 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

49 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)

50  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

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

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

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

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

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

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

57 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.

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

59 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.

60 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.

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

62  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.

63 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?

64 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

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

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

67 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

68 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

69 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

70 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){}

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

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

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

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

75 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

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

77 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}


Download ppt "Lecture 6: Context-Free Languages"

Similar presentations


Ads by Google