|
CMSC 330, Spring 2009Organization of Programming LanguagesProject 4 - Regular Expression Interpreter11:59:59pm IntroductionIn this project, you will write an OCaml module that can parse regular expressions, construct an NFA from a regular expression, and simulate the execution of the NFA. Getting StartedDownloading
Along with files used to make direct submissions to the submit server (submit.jar, .submit , submit.rb), you will find the following project files:
For this project you are allowed to use the library functions found in the Pervasives module loaded by default, as well as the List, Char, and String modules. Project DescriptionYour job is to implement a module Nfa that includes an API for implementing and using both NFAs and regular expressions. The signature for the NFA module is provided. You may not change the NFA signature in any way, though of course your implementation may include more types and functions than are listed in the signature.Note that parts 1 & 2 may be implemented independently, but completing part 3 will require implementing both parts 1 & 2.
Part 1: Regular ExpressionsThe first part of this project is to implement regular expressions. The signature NFA contains the following declarations:
module type NFA =
sig
...
type regexp =
Empty_String
| Char of char
| Union of regexp * regexp
| Concat of regexp * regexp
| Star of regexp
(* Given a regular expression, return a string for regular expression in
postfix notation (as in project 2), with symbols and operators separated
by a single space. Always print the first regexp operand first, so
the output string will always be same for each regexp.
*)
val regexp_to_string : regexp -> string
(* Given a regular expression as string, parses it and returns the
equivalent regular expression represented as the type regexp.
*)
val string_to_regexp : string -> regexp
...
end
Here regexp is an OCaml datatype that represents regular expressions:
Note:
Part 2: NFAsFor the second part of this project, you will write a series of functions to simulate NFAs using OCaml.
module type NFA =
sig
(* Abstract type for NFAs *)
type nfa
(* Type of an NFA transition.
(s0, Some c, s1) represents a transition from state s0 to state s1
on character c
(s0, None, s1) represents an epsilon transition from s0 to s1
*)
type transition = int * char option * int
(* Returns a new NFA. make_nfa s fs ts returns an NFA with start
state s, final states fs, and transitions ts.
*)
val make_nfa : int -> int list -> transition list -> nfa
(* Takes a step in an NFA. step m ss c returns a list of states that m
could be in on seeing input c, starting from any state in ss.
There should be no duplicates in the output list of states.
If some state n is in the input list, then this function should
behave as if any states reachable from n via epsilon
transitions are also in the input list.
Similarly, if some state n is in the output list, then any
states reachable from n via epsilon transitions should also be
in the output list.
*)
val step : nfa -> int list -> char -> int list
(* Returns true if the NFA accepts the string, and false otherwise *)
val accept : nfa -> string -> bool
...
end
Here are descriptions of the elements of this signature, and what you need to do to implement them:
Hint: You need to be a bit careful whenever you combine NFA representations to be sure that state names (i.e., integers) don't conflict. You might use the following internal function as an aid in this process:
Part 3: Converting Regular Expressions to NFAsThe final part of this project is to combine parts 1 & 2 and implement the regular expression to NFA construction.
module type NFA =
sig
...
(* Given a regular expression, return an nfa that accepts the same
language as the regexp
*)
val regexp_to_nfa : regexp -> nfa
...
end
Your job for this part is to write the function regexp_to_nfa, which takes a regexp and returns an NFA that accepts the same language. You'll want to refer back to the slides from class on this construction. Unlike project 2, as long as your NFA accepts the correct language, the structure of the NFA does not matter (since the NFA produced by regexp_to_nfa will only be tested by seeing which strings it accepts). SubmissionYou can submit your project in two ways:
Academic IntegrityThe Campus Senate has adopted a policy asking students to include the following statement on each assignment in every course: "I pledge on my honor that I have not given or received any unauthorized assistance on this assignment." Consequently your program is requested to contain this pledge in a comment near the top. Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible cooperation on projects, use of disallowed materials or resources, or unauthorized use of computer accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension or expulsion from the University. Be sure you understand what you are and what you are not permitted to do in regards to academic integrity when it comes to project assignments. These policies apply to all students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating them. Full information is found in the course syllabus---please review it at this time. |