Download presentation
Presentation is loading. Please wait.
Published byIsabel Skinner Modified over 8 years ago
1
CSCI 2670 Introduction to Theory of Computing September 16, 2004
2
Agenda Yesterday –Prove the pumping lemma –Introduce context-free grammars Today –Quiz –Formally define CFG’s –Build CFG’s
3
Announcement The solution to 1.16a on the website is incorrect. I will fix it today.
4
Example S (S) | SS | () (()())(()) S SS (S)(S) (SS)(()) (()())(()) This sequence of substitutions is called a derivation
5
Parse tree S (S) | SS | () S SS SS () S )( S ()
6
Context-free grammar definition A context-free grammar is a 4-tuple (V, ,R,S), where 1.V is a finite set called the variables, 2. is a finite set, disjoint from V, called the terminals, 3.R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4.S V is the start variable.
7
More definitions If u, v, and w are strings of variables and terminals, and A w is a rule of the grammar, we say uAv yields uwv –Denoted uAv uwv
8
More definitions If a sequence of rules leads from u to v – i.e., u u 1 u 2 … v, we denote this u * v (I can’t do the actual notation in powerpoint – the * should be over the double bars) The language of the grammar is {w * | s * w}
9
Example A Ab | Bb B aBb | ab V = {A,B} = {a,b} R is the set of rules listed above S = A The language of this grammar is {w {a,b} * | w = a n b m, m > n > 0}
10
Designing CFG’s Requires creativity Write a CFG to accept the language {0 n 1 n | n = 0, 1, 2, …} Write a CFG for operators +, -, and () over the symbols x, y, and z
11
Construction 1 {w = 0 n 1 n | n = 0, 1, 2, … } S 0S1 | ε
12
Construction 2 CFG for operators +, -, and () over the symbols x, y, and z S S + S | S – S | (S) | x | y | z
13
Designing the union of two CFG’s Design each CFG separately with starting variables S 1, S 2, …, S k and combine using the rule S S 1 | S 2 | … | S k What is the CFG for the following language { w|w = a n b m, m > n > 0} {w|w = b n a m, m > n > 0}
14
Example First design {a n b m, m > n > 0} S 1 S 1 b | Ab A aAb | ab Then design {b n a m, m > n > 0} –(use different variables) S 2 S 2 a | Ba B bBa | ba Finally, add the “unifying” rule S S 1 | S 2
15
Converting DFA’s into CFG’s For each state q i in the DFA, make a variable R i for your CFG. For each transition rule (q i,a)=q k in your DFA, add the rule R i aR k to your CFG For each accept state q a in your DFA, add the rule R a ε If q 0 is the start state in your DFA, then R 0 is the starting variable in your CFG
16
Linked terminals Terminals may be “linked” to one another in that they have the same number of occurrences (or a related number) –{0 n 1 n }, {a n b m |m>n>0}, {x n y 2n } Add terminals simultaneously –S 0S1 | ε –S xSyy | ε
17
Recursive behavior Some languages may be built of pieces that are within the language –(()())(()) For these languages, you will want a recursive rule –S SS Not all recursive rules will be that easy!
18
Example Construct a CFG accepting all strings in {0,1} * that have equal numbers of 0’s and 1’s S S0S1S | S1S0S | ε
19
Ambiguity Consider the CFG ({S},{0,1},R,S), where the rules of R are S 0 | 1 | S + S | S * S Derive the string 0 * 1 + 1
20
Ambiguity S 0 | 1 | S + S | S * S 0 * 1 + 1 S SS* S+S 0 11 S SS+ S*S 1 01 Different parse trees!
21
Next week Chomsky normal form for CFG’s Pushdown automata Non-context-free languages –Pumping lemma for CFG’s
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.