120 likes | 234 Views
Midterm 1 Breakdown. >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7. Project 1. CS 3240: Languages and Computation. Context-Free Languages. What are Grammars?. A grammar G is defined by (V, T , P, S), where V is a finite set of variables
E N D
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7
CS 3240: Languages and Computation Context-Free Languages
What are Grammars? • A grammar G is defined by (V, T, P, S), where • V is a finite set of variables • Tis a finite set of terminal symbols • P is a finite set of productions or ules • SV is the special start variable • Each grammar G defines a language L(G), which is the set of strings in T* (=Σ*) that G can generate from S. • Automata are actuated by the transition table, grammars by the production rules.
Context-Free Grammars • A context-free grammar (V, T, P, S) is a grammar where all production rules are of the form: A → x, with AV and x(VT)*A string in (VT)* is called sentential form • E.g., let G = ({S}, {a,b}, P , S) with for P: • S→aSa • S→bSb • S→. • Some derivations from this grammar: • S aSa aaSaa aabSbaa aabbaa • S bSb baSab baab, and so on. • In general S …. wwR for w{a,b}*.
Another CFG Example Consider the CFG G=({S,Z},{0,1}, P, S) with P: S 0S1 | 0Z1 Z 0Z | What is the language generated by this G? Answer: L(G) = {0i1j | ij } Specifically, S yields the 0j+k1j according to:S 0S1 … 0jS1j 0jZ1j 0j0Z1j … 0j+kZ1j 0j+kε1j = 0j+k1j
Exercise • Design CFGs for the following languages: • { 0n1n : n≥0} • { 0n1m : n,m≥0} • { (0|1)* : # of 0s > # of 1s} • Answers: • S → 0S1 | • S → 0S | R and R → 1R | • S → T0T and T → TT | 0T1 | 1T0 | 0 |