Spring 2009 CMSC 330 Midterm 1 Solutions: Regular Expressions, Finite Automata, and Ruby, Exams of Programming Languages

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-8zr-1
koofers-user-8zr-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330 Spring 2009 Midterm 1 (SOLUTIONS)
1. Regular expressions and finite automata
a. (4 pts) Explain what we are referring to when we say regular expressions and
finite automata are equivalent.
Regular expressions and finite automata are equivalent in that they
can be used to describe/recognize the same set of strings (regular
language), and may be converted into the opposite format and
recognize the same language.
b. (4 pts) Give a regular expression equivalent to the following finite automaton.
0
1
1 1
42 3
0
1
1 1
4422 33
0 | 01 | 011 or 0( є | 1 | 11)
2. (12 pts) Regular expression to NFA
Create a NFA for the regular expression a|(bc)* using construction method from class.
ε
εε
ε
c
1 2
ε
εε
ε
ε
εε
ε ε
εε
ε
a
ε
εε
ε
4 53
ε
εε
εε
εε
ε
b
6
ε
εε
ε
9
ε
εε
ε
10
87
ε
εε
ε
c
1 2
ε
εε
ε
ε
εε
ε ε
εε
ε
a
ε
εε
ε
4 53
ε
εε
εε
εε
ε
b
6
ε
εε
ε
9
ε
εε
ε
1010
87
3. (16 pts) NFA to DFA
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
ε
εε
ε
ε
εε
ε
a
4
ε
εε
ε
5 6
b
78
ε
εε
ε
ε
εε
ε
1 2
ε
εε
ε
ε
εε
ε
ε
εε
ε
ε
εε
ε
3
ε
εε
ε
ε
εε
ε
a
4
ε
εε
ε
5 6
b
78 8
1,3,4
5,7,8
1,2
4,8
6,8
a
b
a
1,3,4
5,7,8
1,2
4,8
6,8
a
b
a
pf3
pf4

Partial preview of the text

Download Spring 2009 CMSC 330 Midterm 1 Solutions: Regular Expressions, Finite Automata, and Ruby and more Exams Programming Languages in PDF only on Docsity!

CMSC 330 Spring 2009 Midterm 1 (SOLUTIONS)

  1. Regular expressions and finite automata a. (4 pts) Explain what we are referring to when we say regular expressions and finite automata are equivalent. Regular expressions and finite automata are equivalent in that they can be used to describe/recognize the same set of strings (regular language), and may be converted into the opposite format and recognize the same language. b. (4 pts) Give a regular expression equivalent to the following finite automaton.

1

2 3 4

1

22 33 44

0 | 01 | 011 or 0( є | 1 | 11)

  1. (12 pts) Regular expression to NFA

Create a NFA for the regular expression a|(bc)* using construction method from class.

εεεε

c

εε εε

εεεε εεεε

a

εε εε

b^ εεεε^ εεεε 6

εε εε

εε^ εε

εεεε

c

εε εε

εεεε εεεε

a

εε εε

b^ εεεε^ εεεε 6

εε εε

εε^ εε

  1. (16 pts) NFA to DFA

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

a

4

5 6

b

(^7 )

1 2

3

a

4

5 6

b

7 88 1,3, 5,7,

1, 4,

6,

a

b

a

1,3, 5,7,

1, 4,

6,

a

b

a

  1. (16 pts) DFA minimization

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.

P1 P

P1 P

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.

P1 P2 P

P1 P2 P

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 }.

P1 P2 P4 P

P1 P2 P4 P

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