Context-Free Grmmrs
Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge. Finite utomt recognize strings in the lnguge. Perform computtion to determine whether specifc string is in the lnguge. Regulr expressions mtch strings in the lnguge. Descrie the generl shpe of ll strings in the lnguge.
Context-Free Grmmrs A context-free grmmr (or CFG) is n entirely diferent formlism for defning clss of lnguges. Gol: Give description of lnguge y recursively descriing the structure of the strings in the lnguge. CFGs re est explined y exmple...
Arithmetic Expressions Suppose we wnt to descrie ll legl rithmetic expressions using ddition, sutrction, multipliction, nd division. Here is one possile CFG: E int E E Op E E (E) Op + Op - Op * Op / E E Op E E Op (E) E Op (E Op E) E * (E Op E) int * (E Op E) int * (int Op E) int * (int Op int) int * (int + int)
Arithmetic Expressions Suppose we wnt to descrie ll legl rithmetic expressions using ddition, sutrction, multipliction, nd division. Here is one possile CFG: E int E E Op E E (E) Op + Op - Op * Op / E E Op E E Op int int Op int int / int
Context-Free Grmmrs Formlly, context-free grmmr is collection of four items: A set of nonterminl symols (lso clled vriles), A set of terminl symols (the lphet of the CFG) A set of production rules sying how ech nonterminl cn e replced y string of terminls nd nonterminls, nd A strt symol (which must e nonterminl) tht egins the derivtion. E int E E Op E E (E) Op + Op - Op * Op /
Context-Free Grmmrs Formlly, context-free grmmr is collection of four items: A set of nonterminl symols (lso clled vriles), A set of terminl symols (the lphet of the CFG) A set of production rules sying how ech nonterminl cn e replced y string of terminls nd nonterminls, nd A strt symol (which must e nonterminl) tht egins the derivtion. E int E E Op E E (E) Op + Op - Op * Op /
Some CFG Nottion In tody s slides, cpitl letters in Bold Red Uppercse will represent nonterminls. e.g. A, B, C, D Lowercse letters in lue monospce will represent terminls. e.g. t, u, v, w Lowercse Greek letters in gry itlics will represent ritrry strings of terminls nd nonterminls. e.g. α, γ, ω You don't need to use these conventions on your own; just mke sure whtever you do is redle.
A Nottionl Shorthnd E int E E Op E E (E) Op + Op - Op * Op /
A Nottionl Shorthnd E int E Op E (E) Op + - * /
Derivtions E E Op E int (E) Op + * - / E E Op E E Op (E) E Op (E Op E) E * (E Op E) int * (E Op E) int * (int Op E) int * (int Op int) int * (int + int) A sequence of steps where nonterminls re replced y the right-hnd side of production is clled derivtion. If string α derives string ω, we write α * ω. In the exmple on the left, we see E * int * (int + int).
The Lnguge of Grmmr If G is CFG with lphet Σ nd strt symol S, then the lnguge of G is the set L(G) = { ω Σ* S * ω } Tht is, ( L G) is the set of strings of terminls derivle from the strt symol.
If If G is is CFG CFG with with lphet lphet Σ nd nd strt strt symol symol S, S, then then the the lnguge of of G is is the the set set L(G) L(G) = { ω Σ* Σ* S * * ω } Consider the the following CFG CFG G over over Σ = {, {,,, c, c, d}: d}: S S S dt dt T T T c How How mny mny of of the the following strings re re in in L (( G)? G)? dc dc cd cd c c dt Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Context-Free Lnguges A lnguge L is clled context-free lnguge (or CFL) if there is CFG G such tht L = L( G). Questions: Wht lnguges re context-free? How re context-free nd regulr lnguges relted?
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S *
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S *
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S * A A ε
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S * A A ε
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S A A A ε
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S ( c*)
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S ( c*)
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S ( c*) X c*
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S ( c*) X c*
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S X X c*
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S X X c*
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S X X c* C Cc ε
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S X X c* C Cc ε
From Regexes to CFGs CFGs consist purely of production rules of the form A ω. They do not hve the regulr expression opertors * or. However, we cn convert regulr expressions to CFGs s follows: S X X C C Cc ε
Regulr Lnguges nd CFLs Theorem: Every regulr lnguge is context-free. Proof Ide: Use the construction from the previous slides to convert regulr expression for L into CFG for L. Prolem Set 8 Exercise: Insted, show how to convert DFA/NFA into CFG.
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte?
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? S
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte?
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte?
The Lnguge of Grmmr Consider the following CFG G: S S ε Wht strings cn this generte? L(G) = { n n n N }
Regulr Lnguges CFLs All Lnguges
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε S
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε
Why the Extr Power? Why do CFGs hve more power thn regulr expressions? Intuition: Derivtions of strings hve unounded memory. S S ε
Time-Out for Announcements!
Midterm Exm Logistics The next midterm is tonight from 7:00PM 10:00PM. Loctions re divvied up y lst (fmily) nme: A-I: Go to Cuerley Auditorium. J-Z: Go to Cemex Auditorium. The exm focuses on Lecture 06 13 (inry reltions through induction) nd PS3 PS5. Finite utomt onwrd is not tested. Topics from erlier in the qurter (proofwriting, frst-order logic, set theory, etc.) re lso fir gme, ut tht s primrily ecuse the lter mteril uilds on this erlier mteril. The exm is closed-ook, closed-computer, nd limitednote. You cn ring doule-sided, 8.5 11 sheet of notes with you to the exm, decorted however you d like.
Our Advice Et dinner tonight. You re not rin in jr. You re rich, complex, eutiful iologicl system. Plese tke cre of yourself. Red ll the questions efore diving into them. Tunnel vision cn hurt you on n exm. There s evidence tht spreding your time out leds to etter outcomes. Refect on how fr you ve come. How mny of these questions would you hve een le to understnd two months go? Tht s the mrk tht you re lerning something!
Three Questions Wht is something you know now tht, t the strt of the qurter, you knew you didn t know? Wht is something you know now tht, t the strt of the qurter, you didn t know tht you didn t know? Wht is something you don t know tht, t the strt of the qurter, you didn t know tht you didn t know?
Bck to CS103!
Designing CFGs Like designing DFAs, NFAs, nd regulr expressions, designing CFGs is crft. When thinking out CFGs: Think recursively: Build up igger structures from smller ones. Hve construction pln: Know in wht order you will uild up the string. Store informtion in nonterminls: Hve ech nonterminl correspond to some useful piece of informtion.
Designing CFGs Let Σ = {, } nd let L = {w Σ* w is plindrome } We cn design CFG for L y thinking inductively: Bse cse: ε,, nd re plindromes. If ω is plindrome, then ω nd ω re plindromes. No other strings re plindromes. S ε S S
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Some smple strings in L: ((())) (())() (()())(()()) ((((()))(()))) ε ()()
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Let's think out this recursively. Bse cse: the empty string is string of lnced prentheses. Recursive step: Look t the closing prenthesis tht mtches the frst open prenthesis. ((()(()))(()))(())((()))
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Let's think out this recursively. Bse cse: the empty string is string of lnced prentheses. Recursive step: Look t the closing prenthesis tht mtches the frst open prenthesis. (( ( ) ( ( ) ) )( ( ) ))(())(( ( ) ))
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Let's think out this recursively. Bse cse: the empty string is string of lnced prentheses. Recursive step: Look t the closing prenthesis tht mtches the frst open prenthesis. (( ( ) ( ( ) ) )( ( ) ))(())(( ( ) ))
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Let's think out this recursively. Bse cse: the empty string is string of lnced prentheses. Recursive step: Look t the closing prenthesis tht mtches the frst open prenthesis. ( ( ) ( ( ) ) )( ( ) ) (())(( ( ) ))
Designing CFGs Let Σ = {(, )} nd let L = {w Σ* w is string of lnced prentheses } Let's think out this recursively. Bse cse: the empty string is string of lnced prentheses. Recursive step: Look t the closing prenthesis tht mtches the frst open prenthesis. Removing the frst prenthesis nd the mtching prenthesis forms two new strings of lnced prentheses. S (S)S ε
Designing CFGs Let Σ = {, } nd let L = {w Σ* w hs the sme numer of 's nd 's } How How mny mny of of the the following following CFGs CFGs hve hve lnguge lnguge L? L? S S S ε S S S ε S S S ε S SS SS ε Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Designing CFGs Let Σ = {, } nd let L = {w Σ* w hs the sme numer of 's nd 's } How How mny mny of of the the following following CFGs CFGs hve hve lnguge lnguge L? L? S S S ε S S S ε S S S ε S SS SS ε Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Designing CFGs Let Σ = {, } nd let L = {w Σ* w hs the sme numer of 's nd 's } How How mny mny of of the the following following CFGs CFGs hve hve lnguge lnguge L? L? S S S ε S S S ε S S S ε S SS SS ε Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Designing CFGs Let Σ = {, } nd let L = {w Σ* w hs the sme numer of 's nd 's } How How mny mny of of the the following following CFGs CFGs hve hve lnguge lnguge L? L? S S S ε S S S ε S S S ε S SS SS ε Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Designing CFGs Let Σ = {, } nd let L = {w Σ* w hs the sme numer of 's nd 's } How How mny mny of of the the following following CFGs CFGs hve hve lnguge lnguge L? L? S S S ε S S S ε S S S ε S SS SS ε Answer Answer t t PollEv.com/cs103 PollEv.com/cs103 or or text text CS103 CS103 to to 22333 22333 once once to to join, join, then then numer. numer.
Designing CFGs: A Cvet When designing CFG for lnguge, mke sure tht it genertes ll the strings in the lnguge nd never genertes string outside the lnguge. The frst of these cn e tricky mke sure to test your grmmrs! You'll design your own CFG for this lnguge on Prolem Set 8.
CFG Cvets II Is the following grmmr CFG for the lnguge { n n n N }? S S Wht strings in {, }* cn you derive? Answer: None! Wht is the lnguge of the grmmr? Answer: Ø When designing CFGs, mke sure your recursion ctully termintes!
Designing CFGs When designing CFGs, rememer tht ech nonterminl cn e expnded out independently of the others. Let Σ = {, } nd let L = {n n n N }. Is the following CFG for L? S X X X X ε S X X X X X X X X
Finding Build Order Let Σ = {, } nd let L = { n n n N }. To uild CFG for L, we need to e more clever with how we construct the string. If we uild the strings of 's independently of one nother, then we cn't enforce tht they hve the sme length. Ide: Build oth strings of 's t the sme time. Here's one possile grmmr sed on tht ide: S S S S S S
Function Prototypes Let Σ = {void, int, doule, nme, (, ),,, ;}. Let's write CFG for C-style function prototypes! Exmples: void nme(int nme, doule nme); int nme(); int nme(doule nme); int nme(int, int nme, int); void nme(void);
Function Prototypes Here's one possile grmmr: S Ret nme (Args); Ret Type void Type int doule Args ε void ArgList ArgList OneArg ArgList, OneArg OneArg Type Type nme Fun question to think out: wht chnges would you need to mke to support pointer types?
Summry of CFG Design Tips Look for recursive structures where they exist: they cn help guide you towrd solution. Keep the uild order in mind often, you'll uild two totlly diferent prts of the string concurrently. Usully, those prts re uilt in opposite directions: one's uilt left-to-right, the other right-to-left. Use diferent nonterminls to represent diferent structures.
Applictions of Context-Free Grmmrs
CFGs for Progrmming Lnguges BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR identifier constnt EXPR + EXPR EXPR EXPR EXPR * EXPR...
Grmmrs in Compilers One of the key steps in compiler is fguring out wht progrm mens. This is usully done y defning grmmr showing the high-level structure of progrmming lnguge. There re certin clsses of grmmrs (LL(1) grmmrs, LR(1) grmmrs, LALR(1) grmmrs, etc.) for which it's esy to fgure out how prticulr string ws derived. Tools like ycc or ison utomticlly generte prsers from these grmmrs. Curious to lern more? Tke CS143!
Nturl Lnguge Processing By uilding context-free grmmrs for ctul lnguges nd pplying sttisticl inference, it's possile for computer to recover the likely mening of sentence. In fct, CFGs were frst clled phrse-structure grmmrs nd were introduced y Nom Chomsky in his seminl work Syntctic Structures. They were then dpted for use in the context of progrmming lnguges, where they were clled Bckus- Nur forms. Stnford's CoreNLP project is one plce to look for n exmple of this. Wnt to lern more? Tke CS124 or CS224N!
Biogrphy Minute: Nom Chomsky Invented CFGs! Helped found felds of linguistics nd cognitive science PC: Hns Peters / Anefo (vi Wikimedi) Tody, perhps more well known for politicl writing thn linguistics Mde it onto President Nixon s Enemies List Anti-cpitlism, nti-imperilism, nti-wr Drwing on linguistics expertise, written extensively on stte propgnd (Mnufcturing Consent)
Next Time Turing Mchines Wht does computer with unounded memory look like? How would you progrm it?