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, SV In the form of A with AV 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|n0} V={S} ={a, b} S aSb aaSbb a3Sb3 anSbn anbn
Example L(G)=? G = (V, , P, S) L(G)={anbn|n0} Is the expression power of context-free grammar stronger than regular expression? V={S} ={a, b} L(G)={anbn|n0} 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 uvnwL 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 wL(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 AV and (V )* S : start symbol, SV
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 AV and (V )* with , (V )+ and || ||. S : start symbol, SV
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 AV and (V )* A with , (V )*, AV and (V )+. with , (V )+ and || ||. S : start symbol, SV
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}