cfg - CONTEXT FREE LANGUAGES CONTEXT-FREE LANGUAGES...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CONTEXT FREE LANGUAGES CONTEXT-FREE LANGUAGES CONTEXT-FREE REGULAR • Context-Free Grammars • Push-Down Automata IDEA Remember ”balanced parantheses” example from structural induction? IDEA Remember ”balanced parantheses” example from structural induction? S= ￿ ￿ (S )S IDEA Remember ”balanced parantheses” example from structural induction? S= ￿ ￿ (S )S If we write it this way: S −→ (S )S | ￿ IDEA Remember ”balanced parantheses” example from structural induction? S= ￿ ￿ (S )S If we write it this way: S −→ (S )S | ￿ it is a context-free grammar generating a context-free language containing all balanced parantheses strings. CONTEXT-FREE GRAMMARS A context-free grammar is a tuple G = (V, T, P, S ) where • V is finite set of variables or nonterminals. • T is a finite set of terminals. • V ∩T =∅ • S ∈ V is a unique start symbol. • P is a finite set of productions (rules). CONTEXT-FREE GRAMMARS A context-free grammar is a tuple G = (V, T, P, S ) where • V is finite set of variables or nonterminals. • T is a finite set of terminals. • V ∩T =∅ • S ∈ V is a unique start symbol. • P is a finite set of productions (rules). A −→ α CONTEXT-FREE GRAMMARS A context-free grammar is a tuple G = (V, T, P, S ) where • V is finite set of variables or nonterminals. • T is a finite set of terminals. • V ∩T =∅ • S ∈ V is a unique start symbol. • P is a finite set of productions (rules). A −→ α A∈V α ∈ (V ∪ T )∗ EXAMPLE G = ({S }, {0, 1}, , S) EXAMPLE G = ({S }, {0, 1}, S −→ 0 S −→ ￿ , S) S −→ 1 S −→ 0S 0 S −→ 1S 1 EXAMPLE G = ({S }, {0, 1}, S −→ 0 S −→ ￿ , S) S −→ 1 S −→ 0S 0 Generates Palindromes! S −→ 1S 1 EXAMPLE G = ({S }, {0, 1}, S −→ 0 S −→ ￿ , S) S −→ 1 S −→ 0S 0 Generates Palindromes! S −→ 1S 1 S −→ 0S 0 | 1S 1 | ￿ | 1 | 0 HOW DO WE DEFINE THE LANGUAGE OF A GRAMMAR? DERIVATIONS DERIVATIONS Let α1 , α2 ∈ (V ∪ T )∗ , we say: α1 =⇒G α2 DERIVATIONS Let α1 , α2 ∈ (V ∪ T )∗ , we say: If: α1 =⇒G α2 ∃A ∈ V ∃β , γ , λ ∈ (V ∪ T )∗ DERIVATIONS Let α1 , α2 ∈ (V ∪ T )∗ , we say: If: α1 =⇒G α2 ∃A ∈ V such that α1 = β Aγ ∃β , γ , λ ∈ (V ∪ T )∗ α2 = βλγ DERIVATIONS Let α1 , α2 ∈ (V ∪ T )∗ , we say: If: α1 =⇒G α2 ∃A ∈ V such that α1 = β Aγ And ∃β , γ , λ ∈ (V ∪ T )∗ α2 = βλγ A −→ λ is in P DERIVATIONS Let α1 , α2 ∈ (V ∪ T )∗ , we say: If: α1 =⇒G α2 ∃A ∈ V such that α1 = β Aγ And ∃β , γ , λ ∈ (V ∪ T )∗ α2 = βλγ A −→ λ is in P α2 can be derived from α1 in a single production application. LANGUAGE OF A GRAMMAR Let =⇒∗ be reflexive and transitive closure of =⇒G . G LANGUAGE OF A GRAMMAR Let =⇒∗ be reflexive and transitive closure of =⇒G . G α1 =⇒∗ α2 iff α2 can be derived from α1 in finite number (even zero) G of applications of productions in P LANGUAGE OF A GRAMMAR Let =⇒∗ be reflexive and transitive closure of =⇒G . G α1 =⇒∗ α2 iff α2 can be derived from α1 in finite number (even zero) G of applications of productions in P L(G) = {w : w ∈ T and S ∗ ∗ =⇒G w} LANGUAGE OF A GRAMMAR Let =⇒∗ be reflexive and transitive closure of =⇒G . G α1 =⇒∗ α2 iff α2 can be derived from α1 in finite number (even zero) G of applications of productions in P L(G) = {w : w ∈ T and S ∗ ∗ =⇒G w} The language generated by G is all strings of terminals derivable from the start symbol. LANGUAGE OF A GRAMMAR Let =⇒∗ be reflexive and transitive closure of =⇒G . G α1 =⇒∗ α2 iff α2 can be derived from α1 in finite number (even zero) G of applications of productions in P L(G) = {w : w ∈ T and S ∗ ∗ =⇒G w} The language generated by G is all strings of terminals derivable from the start symbol. Context-Free Languages = {L : ∃ grammar (CFG) G s.t. L(G) = L} EXAMPLE L = {0n 1n | n ≥ 0} EXAMPLE L = {0n 1n | n ≥ 0} S −→ 0S 1 | ￿ EXAMPLE {w : w has equal number of 0’s and 1’s} ...
View Full Document

This note was uploaded on 04/11/2010 for the course CSC CSC236 taught by Professor Farzanazadeh during the Spring '10 term at University of Toronto.

Ask a homework question - tutors are online