


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to the first midterm exam of the spring 2009 computer science 330 course at the university of x. The exam covers topics such as regular expressions, finite automata, and ruby programming. It includes exercises on explaining the equivalence of regular expressions and finite automata, creating nfas and dfas, minimizing dfas, and working with ruby arrays and hashes.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



CMSC 330 Spring 2009 Midterm 1 (SOLUTIONS)
1
2 3 4
1
22 33 44
0 | 01 | 011 or 0( є | 1 | 11)
Create a NFA for the regular expression a|(bc)* using construction method from class.
εεεε
c
εε εε
εεεε εεεε
a
εε εε
b^ εεεε^ εεεε 6
εε εε
εε^ εε
εεεε
c
εε εε
εεεε εεεε
a
εε εε
b^ εεεε^ εεεε 6
εε εε
εε^ εε
Apply the subset construction algorithm to convert the resulting NFA to a DFA. Show the NFA states associated with each state in your DFA.
1 2
3
4
5 6
(^7 )
1 2
3
4
5 6
7 88 1,3, 5,7,
1, 4,
6,
1,3, 5,7,
1, 4,
6,
Apply Hopcroft minimization to the following DFA to generate a minimized DFA. List the DFA states in each partition created, and explain why the partition will or will not be split by the minimization algorithm, and list the DFA states in each resulting partition if the partition is split.
1
2 3 4
1
22 33 44
Initially states are split into two partitions, { 1 } for non-final states, and { 2, 3, 4 } for final states.
Partition { 2, 3, 4 } will be split into { 2, 3 } and { 4 } because behavior is different for input=1, since 2 & 3 stay in the partition but 4 rejects.
Partition { 2, 3 } will be split into { 2 } and { 3 } because behavior is different for input=1, since 2 will stay in the partition but 3 will enter new partition { 4 }.
Result is original DFA, which was already minimized.
description may change. You may assume that all symbols in the alphabet are lowercase letters between a and z, and that they are always preceded by a single space in the output file.
For (10 pts) extra credit, you may also have your program print the list of states & final states in numerical order. You may assume that all states are numbers greater or equal to 0. Helpful functions: File.new(…) // opens file File.eof? // at end of file? File.readline( ) // read single line from file String.scan(…) // finds patterns in String String.to_i(…) // converts String to int print(…) // prints arguments w/o newline
Input Output Extra Credit Output % Start 9 % Final { 10 9 } % States { 11 9 10 } % Alphabet { b a } % Transitions { % ( 9 a 11 ) … % ( 9 10 ) % }
% Start 9 % Final { 10 9 } % States { 11 9 10 } % Alphabet { a b } % Transitions { % (9 a 11) … % (9 10) % }
% Start 9 % Final { 9 10 } % States { 9 10 11 } % Alphabet { a b } % Transitions { % (9 a 11) … % (9 10) % }
file = File.new(ARGV[0], "r") // ARGV[0] file name 3 pts until file.eof? do // open file & read 3 pts line = file.readline // read lines from file 3 pts if line =~ /Alphabet/ // action for Alphabet 3 pts a = line.scan(/ [a-z]/) // extract all symbols 8 pts a.sort! // sort symbols 3 pts print("% Alphabet {") // print out symbols 4 pts a.each { |x| print(x) } print " }\n" elsif line =~ /(Final|States)/ // action for Final & States 1 pts stateName = $1 // remember name 1 pts a = [] line.scan(/\d+/) { |x| a.push(x.to_i) } // extract all state #s 3 pts a.sort! // sort on int value 4 pts print("% ", stateName, " { ") // print out states 1 pts a.each { |x| print(x, " ") } print "}\n" else puts line // no action otherwise 3 pts end end