Download CS 421 Fall 2006 Homework 3: Finite State Automata, Regular Expressions, and Grammars - Pr and more Assignments Computer Science in PDF only on Docsity! HW 3 – Finite State Automata, Regular Expressions, and Grammars CS 421 – Fall 2006 Revision 1.3 Assigned Wednesday, October 25, 2006 Due Wednesday November 8, 2006, 9:00 AM To be turned in at 2106 Siebel Center, office of Andrea Whitesell Extension 48 hours (20% penalty) 1 Change Log 1.0 Initial Release. 1.1 Corrected some typos, mainly changing intersection to union. 1.2 Added terminals for <id> in Problem 3. 1.3 Corrected a mis-placed production in the solution to Problem 4. 2 Turn-In Procedure Your answers to the following questions are to be hand-written neatly or printed on one or more sheets of paper, each with your name in the upper right corner. The homework is to be turned in to my administrative assistant, Andrea Whitesell in 2106 Siebel Center by 9:00am of Wednesday 8 November 2006. Alternately, you may hand it to Prof. Elsa Gunter in person before the deadline. 3 Objectives and Background The purpose of this HW is to test your understanding of: • How to use regular expressions and finite state automata to formally express sets of strings (called languages) given by an English language description • How to use Context Free Grammars to formally express languages given by an English language description • How to create datatypes to represent the tokens and parse trees needed to parse strings in the language generated by a given grammar. • How to disambiguate a grammar. Another purpose of HW3 is to provide you with experience answering non-programming written questions of the kind you may experience on the final. 1 4 Problems 1. (20 points) For each of the following languages (ie, sets of strings), write a regular expression generating the set, and draw a finite state automaton accepting the set: a. (10 points) The set of all strings of a’s and b’s such that every b in each string is immediately preceded by at least two a’s. Solution: (a*aab)*a* - ? a -a -a -b - ? a 6 b. (10 points) The set of all strings of 0’s, 1’s, and 2’s, such that before each 2 the combined number of 0’s and 1’s is always even. Solution: (((0∨1)(0∨1))*2)*(0∨1)* - U 0 1 U0 1 " !6 -2 - 0 6 1 # ? 2 & % 2. (27 points) Recall the definition of a regular expression given in class: Given an alphabet Σ of letters, the set of regular expressions over Σ is the smallest set of strings over Σ ∪ {(, ), ∨, ∗, } (where Σ ∩ {(, ), ∨, ∗, } = { }) satisfying the following: • is a regular expression, representing the set containing the empty string. • If a ∈ Σ, then a is a regular expression, representing the set containing the single string containing the single letter a. • If x is a regular expression, then (x) is a regular expression, representing the same set of strings as x does. • If x and y are regular expressions, then xy is a regular expression, representing the set of all strings made from a string from the set represented by x concatenated with a string from the set represented by y. • If x and y are regular expressions, then x ∨ y is a regular expression, representing the union of the sets represented be each of x and y. • If x is a regular expression, then x∗ is a regular expression, representing the set of all strings made from concatenating zero or more strings from the set represented by x. a. (12 points) Assuming Σ = {a, b, c}, write a context free grammar over the alphabet Σ ∪ {(, ), ∨, ∗, } that generates all regular expressions. Solution: < letter > ::= a | b | c < reg exp > ::= | < letter > | (< reg exp >)| < reg exp >< reg exp > | < reg exp > ∨ < reg exp > | < reg exp > ∗ 2