4 Pages

lec13

Course: CS 322, Fall 2009
School: Washington
Rating:
 
 
 
 
 

Word Count: 1900

Document Preview

322 CSE - Introduction to Formal Methods in Computer Science Introduction to Context-Free Grammars Dave Bacon Department of Computer Science & Engineering, University of Washington In the last few lectures we finished up talking about regular languages and regular expressions. An interesting take home message that you should have taken home with you (and put in a special place on the shelf) is that not...

Register Now

Unformatted Document Excerpt

Coursehero >> Washington >> Washington >> CS 322

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
322 CSE - Introduction to Formal Methods in Computer Science Introduction to Context-Free Grammars Dave Bacon Department of Computer Science & Engineering, University of Washington In the last few lectures we finished up talking about regular languages and regular expressions. An interesting take home message that you should have taken home with you (and put in a special place on the shelf) is that not all languages are regular. Thus there are language which cannot be recognized by deterministic finite automata. This leads us naturally to ask the question of: well are there other types of machines which will recognize some of these non-regular languages? To answer this we will begin not with constructing such machines first, but with constructing the languages that ultimately we will show are recognized by some sort of machine. The languages we are going to discuss are called context-free languages. These languages are a superset of regular languages. That is all regular languages are contextfree. But not necessarily vice versa. Lets begin with a simple example of a context-free grammar. A 0A1 A B B # Okay, so what is this beast, which we will call G? A context free grammar is made up of a collection of substitution rules. Each of the lines above is one of these substitution rules. Substitution rules are also sometimes called productions. Substitution rules are made up of three parts. The first is a symbol, the second is an arrow, and the third is a string. Sometimes we call the symbol which appears on the left hand side of the substitution rule the variable. The variables in the above context-free grammar are A and B. The string consisting of variables and other symbols, which are called terminals. Terminals in the above context free grammar include 0, 1, and #. You'll see the reason for this nomenclature in a second. The terminals are going to be taken from the alphabet over which the context-free language is constructed. One of the variables in the above specification is also taken to be the start variable. By convention, unless otherwise noted, the first variable in the specification will be the start variable. The start variable in the above context-free grammar is A. To generate strings from a specification of a grammar, you follow a simple procedure: 1. Begin with the start variable. Write it down. 2. Find a variable that has been written down, and a rule which begins with that particular variable. Then replace the variable which you picked with the right hand side of the rule you have also selected. Repeat this process until no variables remain. Lets do an example for the grammar G above. We start by writing down the start variable A. Now we apply, say, the first rule. This turns the expression into 0A1. Now there is only one variable to chose, so we choose A. Now let's pick a rule. Suppose we pick the first rule. Then we will end up with 00A11. Okay, great. Again there is only one variable, A, so we pick it. This time we use the second rule. This turns the expression into 00B11. Again we only have one variable B. There is only one rule for the B variable, so we apply it and end up with the string 00#11. Since there are no variables left, we are done. Thus we say that 00#11 is in the language generated by this context free grammar. The language of a grammar is the set of all strings which can be generated by the above process. For example it is easy to see that # is in the grammar, by applying the second rule and then the third rule. A little experimentation will tell you that the language of this context free grammar is {0k #1k |k 0}. This language is not regular (a quick use of the pumping lemma will verify this.) So we already see that context-free grammars can express languages beyond regular languages. One convenience that we adopt in denoting a context-free grammar is that for a given variable we denote multiple rules by separating them by a |. Thus, the above grammar, which has the two rules A 0A1 and A B, can be denoted by the line A 0A1|B. Think of the | as an "or." Now one question you might have at this point is a very important one. Why the heck are these things called "context-free"? Good question. Apparently the name comes because the substitutions you are allowed to make do not depend on the context: i.e. on what else is around the variable you are substituting for. 2 I. FORMAL DEFINITION Lets get a little more formal. Context-free grammars are going to be specified by a four tuple. Whew. This means we will have to write down less than we did with the DFAs and NFAs and their four tuples. Okay, so this four tuple is (V, , R, S), where these four things are 1. V is the variables. This is a finite set. 2. is the set of terminals. It is a finite set, like V , but is disjoint from V (disjoin means that they share no common elements.) 3. R is a finite set of rules. Every rule is made up of a variable and a string of variables and terminals. 4. S is the start variable. It is an element of V . For example, lets write down the formal definition for the context-free grammar we specified above. G = (V, , R, S). V = {A, B}. = {0, 1, #}. The set of rules R is made up of {A 0A1, A B, B #}. Finally the start variable S is equal to A. So G = B}, ({A, {0, 1, #}, {A 0A1, A B, B #}, A). A few more definitions. If x, y and z are strings of variables and terminals, and A x is a rule in the grammar we say that yAz yields yxz. We write this as yAz yxz. A further useful word is derives. We say that x derives y and write this as x y if x is equal to y or can be derived by a sequence of rules, i,e. there exist a sequence x1 , x2 , . . . , xk such that x x1 x2 xk y. This is kind of like the we defined previously. Having defined we can now define the language of the grammar. The language of the grammar is the language {x |S x}. We often call teh language L(G). Lets do another example of a context-free grammar. Let G = (V, , R, S) be the grammar. Let V = {A}, = {x, y, z, +, -, , /, (, )}. The start state is the only state, S = A. The rules are A x|y|z|A+A|A-A|AA|A/A|(A). What is this language? Well if you play with for a while you will see that it is valid expressions formed from the variables x, y, z. For example, x + y z is in the language of this grammar. Finally I'm getting tired of typing context-free grammar so we will denote this by CFG. Further we will denote context-free languages as CFL (not to be confused with the Canadian football league.) II. CONSTRUCTING CFGS WHICH ACCEPT REGULAR LANGUAGES Constructing CFGs is a art, a lot like the art of constructing DFAs and NFAs. Indeed it can be a bit harder, because for the later, we had an idea of the computation being carried out, but for CFGs we are constructing a kind of strange set of substitution rules. Here we will show how to construct a CFG for a language that is regular. Since the language is regular, there exists a DFA which recognizes the language. Suppose this is M = (Q, , , q0 , F ). The context free grammar we will construct has a variable Ri for every state qi Q. The rules consists of Ri aRj if (qi , a) = qj . Add the rule Ri if qi is an accept state of the DFA. Make Rj with q0 = qj the start variable. This CFG has a CFL which is exactly that of the DFA (you should think about why this is true.) III. PARSE TREES Recall that above we showed how the CFG G with A 0A1|B and B # with start variable A had a derivation A 0A1 00A11 000A1111 000B111 000#111. We can represent this derivation via a parse tree. Lets 3 look at the parse tree for this derivation as it will give you a quick idea of what the parse tree is A1 111 11 A 111 . 11 ... 11 11 ... 11 A .. 11 ** .. 11 .. *** 11 .. ** 11 * .. 11 A ** .. 11 ** .. 11 ** .. 11 ** .. 11 B ** .. 11 ** .. 11 * .# 1$ # 0 0 0 1 1 1 Note that the leaves of this tree are terminals, and the internal nodes of the tree are variables. Further for any node in the tree which is not a leaf, this node has children which are made up of valid rule substitutions via a rule from the grammar. Further note that parse trees retain an ordering: the children appearing are ordered from left to right and this correspondence is the same as for the rule the node represents. Parse trees are very important in compilers. The reason for this is that the tree structure of the program eases ...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science Chomsky Normal FormDave BaconDepartment of Computer Science & Engineering, University of WashingtonA useful form for dealing with context free grammars is the Chomksy normal form. This
Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science Pushdown AutomataDave BaconDepartment of Computer Science & Engineering, University of WashingtonOkay now that we've talked a little about context free grammars, a natural question to a
Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science Pushdown Automata And Context-Free LanguagesDave BaconDepartment of Computer Science & Engineering, University of WashingtonHaving introduced pushdown automata, we will now show that pu
Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science Closure Properties of Context-Free LanguagesDave BaconDepartment of Computer Science & Engineering, University of WashingtonPreviously we showed how regular operations were closed under
Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science Turing MachinesDave BaconDepartment of Computer Science & Engineering, University of WashingtonToday we finally make it up to.computers! Well at least to the model which best captures w
Washington - CS - 322
CSE 322 - Introduction to Formal Methods in Computer Science DecidabilityDave BaconDepartment of Computer Science & Engineering, University of WashingtonIn the last lecture we introduced the Turing machine. We talked about how it worked, and show
Washington - CS - 322
CSE 322: Formal Models in Computer ScienceMidterm TopicsThe midterm will be Friday, May 9th in class. It will be 50 minutes in length and will be closed book. The midterm will cover everything covered in class up to and including the first half o
Washington - CS - 322
CSE 322: Formal Models in Computer ScienceFinal TopicsThe nal will be Monday, June 9 from 2:30-4:20 in EEB 045. It will be 110 minutes in length and will be closed book. It will cover everything in the class with slightly more coming from the mat
Washington - CS - 322
CSE322: Formal Models in Computer Science Partial Solutions to Sample FinalSpring 2006This handout has (partial) solutions to some of the problems in the sample final exam that was handed out in class on Friday, May 26. I have not given solutions
Washington - CS - 322
1 CSE 322: Formal Models in Computer Science April 2, 2008 HandoutN,S,W,ENNN,EQQQ Fremont Troll o QQQ y QQQ QQQ QQQ N,S,W N QQQ Q@ E G Queen Anne Ave o Villa Sophia o W iRRR y RRR RRR N S RRR W R Space Needle o yNWGas Works Park o
Washington - CS - 322
CSE 322 Introduction to Formal Models in Computer ScienceDefining from In the definition of DFAs, the transition function explicitly describes, for each character a , the name of the state reached on a when started at state q. This is precisely
Washington - CS - 322
Washington - CS - 322
CSE 322: Formal Models in Computer Science Website: http:/www.cs.washington.edu/322 Lecture Times: MWF 1:30-2:30 in EE 045 (in the dungeon of EE.) Instructor: Dave Bacon Office: CSE 460 Email: dabacon@cs.washington.edu Phone: 206-221-6503 Office hour
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Winter 2005 Course InformationInstructor: TAs:Anna R. Karlin Neva Cherniavsky Ning ChenPGA 594 PGA 378 PGA 310karlin@cs.washington.edu nchernia@cs.washington.edu ning@cs.washington.edu543-9344 685-
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #0 January 3, 2005 Due: Wednesday, January 5Reading Assignment: Kleinberg and Tardos, Chapters 1 and 4 Questions: 1. For each of the following topics, indicate your level of comfort on a scale o
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #1 January 5, 2005 Due: Wednesday, January 12Reading Assignment: Kleinberg and Tardos, Chapters 1 and 4 Problems:1. Gale and Shapley published their paper on the stable marriage problem in 196
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #2 January 12, 2005 Due: Wednesday, January 19 Reading Assignment: K&T, Section 5.1, 5.2, 5.4, 5.6, 5.7, 5.9 Questions: 1. (Kleinberg and Tardos, Chapter 4, Problem 5) Let's consider a long, quiet
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #4 January 27, 2005 Due: Wednesday, February 2Reading Assignment: Kleinberg and Tardos, Network Flow, handout on linear programming, section 11.6 in new book. Problems:1. Let G = (V, E) be a g
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #5 February 2, 2005 Due: Wednesday, February 9Reading Assignment: Kleinberg and Tardos, Network Flow, handout on linear programming Problems: 1. Your friends have written a very fast piece of ma
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #6 Due: Wednesday, February 16Reading Assignment: Linear programming handouts, chapter on randomized algorithms, other randomized algorithms handouts (we'll send out mail about them.) Problems:
Washington - CS - 521
CSE 521: Design and Analysis of Algorithms Assignment #7 Due: Wednesday, Feb 23Problems:1. QuickSelect is the following simple algorithm for finding the k-th smallest element in an unsorted set S. QuickSelect(S, k): (a) Pick a pivot element p uni
Washington - CS - 521
Name:1 2 3 4 total/ 9 /10 /16 /39 /74CSE 521 Final ExamMarch 15, 2004 Instructions: You have 1 hour and 50 minutes to complete the exam. The exam is closed book, closed notes. Please do not turn the page until you are instructed to do so.
Washington - CS - 421
Measuring efficiency: The RAM modelnRAM = Random Access Machine Time # of instructions executed in an ideal assembly languagennComplexity and Representative ProblemsWinter 2005 Paul Beameneach simple operation (+,*,-,=,if,call) takes on
Washington - CS - 421
Undirected Graph G = (V,E)1 2 3 11 10Graph TraversalWinter 2005 Paul Beame12 4 5 6 7 8 13 9Directed Graph G = (V,E)1 2 3 11 12 4 5 6 7 8 13 9n n nGraph TraversalLearn the basic structure of a graph Walk from a fixed starting vertex s to
Washington - CS - 421
Greedy AlgorithmsnHard to define exactly but can give general propertiesn nGreedy AlgorithmsWinter 2005 Paul BeamenSolution is built in small steps Decisions on how to build the solution are made to maximize some criterion without looking t
Washington - CS - 421
Dynamic ProgrammingnDynamic ProgrammingnGive a solution of a problem using smaller sub-problems where all the possible sub-problems are determined in advance Useful when the same sub-problems show up again and again in the solutionDynamic Pro
Washington - CS - 421
Bipartite MatchingnnGiven: A bipartite graph G=(V,E)ME is a matching in G iff no two edges in M share a vertexnNetwork FlowWinter 2005 Paul BeameGoal: Find a matching M in G of maximum possible sizeBipartite MatchingBipartite Matchin
Washington - CS - 421
Dealing with NP-completenessWinter 2005 Paul BeameWhat to do if the problem you want to solve is NP-hardnYou might have phrased your problem too generallyne.g., in practice, the graphs that actually arise are far from arbitrary n maybe they
Washington - CSE - 590
Washington - CSEP - 590
Homeland Security / Cyber Security Autumn 2005N.B. CONTINUALLY IN FLUX Version 24: November 22 Course Requirements: Red Team Exercise (~25%): You are a member of a 5-6 person team of engineers and policy professionals that has been hired by the Depa
Washington - CSEP - 590
Homeland Security / Cyber Security Autumn 2005N.B. CONTINUALLY IN FLUX Version 24: November 22 Course Requirements: Red Team Exercise (~25%): You are a member of a 5-6 person team of engineers and policy professionals that has been hired by the Depa