Question: In Ocaml: Implement: Below are all the types and functions that are already implemented: type regexp _ t = | Empty _ String | Char

In Ocaml:
Implement:
Below are all the types and functions that are already implemented:
type regexp_t =
| Empty_String
| Char of char
| Union of regexp_t * regexp_t
| Concat of regexp_t * regexp_t
| Star of regexp_t
Empty_String represents the regular expression recognizing the empty string (not the empty set!). Formally, the empty string can be represented as epsilon.
Char c represents the regular expression that accepts the single character c. Written as a formal regular expression, this would be c.
Union (r1, r2) represents the regular expression that is the union of r1 and r2. For example, Union(Char 'a', Char'b') is the same as the formal regular expression a|b.
Concat (r1, r2) represents the concatenation of r1 followed by r2. For example, Concat(Char 'a', Char 'b') is the same as the formal regular expression ab.
Star r represents the Kleene closure of regular expression r. For example, Star (Union (Char 'a', Char 'b')) is the same as the formal regular expression (a|b)*.
let fresh =
let cntr = ref 0 in
fun ()->
cntr :=!cntr +1 ;
!cntr;;
string_to_nfa s
Type: string -> nfa
Description: This function takes a string for a regular expression, parses the string, converts it into a regexp, and transforms it to an nfa, using your regexp_to_nfa function. As such, for this function to work, your regexp_to_nfa function must be working. In the starter files, we have provided the function string_to_regexp that parses strings into regexp values, described next.
string_to_regexp s
Type: string -> regexp
Description: This function takes a string for a regular expression, parses the string, and outputs its equivalent regexp. If the parser determines that the regular expression has illegal syntax, it will raise an IllegalExpression exception.
Examples:
string_to_regexp "a"= Char 'a'
string_to_regexp "(a|b)"= Union (Char 'a', Char 'b')
string_to_regexp "ab"= Concat(Char 'a', Char 'b')
string_to_regexp "aab" = Concat(Char 'a',Concat(Char 'a', Char 'b'))
string_to_regexp "(a|E)*"= Star(Union(Char 'a',Empty_String))
string_to_regexp "(a|E)*(a|b)"= Concat(Star(Union(Char 'a',Empty_String)),Union(Char 'a',Char 'b'))
type ('q,'s) transi ='q *'s option *'q
type ('q,'s) nfa_y ={ sigma : 's list; qs : 'q list; q0 : 'q; fs : 'q list; delta : ('q,'s) transi list; }
So: let nfa ={ sigma =['z']; qs =[1; 2; 3]; q0=1; fs =[3]; delta =[(1, Some 'z',2); (2, None, 3)]}
let dfa ={sigma =['z'; 'y'; 'x']; qs =[1; 2; 3]; q0=1; fs =[3]; delta =[(1, Some 'z',2); (2, Some 'y',1); (2, Some 'x',3)]};;
mo n ql t, of Type: ('q,'s) nfa_y ->'q list ->'s option ->'q list, takes an NFA n, a set of initial states ql, and a symbol option t. It returns a set of states that the NFA might be in after starting from any state in ql and making one transition on the symbol t.
So: mo nfa [1](Some 'z')=[2]
mo nfa [2](Some 'z')=[]
mo nfa [3](Some 'z')=[]
mo nfa [1;2](Some 'z')=[2]
mo nfa [2] None =[3]
e_clo n ql, of Type: ('q,'s) nfa_y ->'q list ->'q list, takes an NFA n and a set of initial states ql. It returns a set of states that the NFA might be in after making zero or more epsilon transitions from any state in ql.
So: e_clo nfa [1]=[1]
e_clo nfa [2]=[2;3]
e_clo nfa [3]=[3]
e_clo nfa [1;2]=[1;2;3]
acce n t, of Type: ('q, char) nfa_y -> string -> bool, which takes an NFA nfa and a string s, and returns true if the NFA accepts the string.
So: acce dfa ""= false;; (* dfa_ex is the NFA defined above *)
acce dfa "zx"= true;;
acce dfa "zyx"= false;;
acce dfa "zyzx"= true;;
let explode (s: string) : char list =
let rec exp i l =
if i <0 then l else exp (i -1)(s.[i] :: l)
in
exp (String.length s -1)[];;
let rec elem x a =
match a with
| h::t ->(h = x)||(elem x t)
|[]-> false;;
let rec insert x a =
if not (elem x a) then x::a else a;;
let insert_all xs a =
List.fold_right insert xs a;;
let rec subset a b =
match a with
| h::t ->(elem h b) && (subset t b)
|[]-> true;;
let rec eq a b =(subset a b) && (subset b a);;
let rec remove x a =
match a with
| h::t -> if h = x then t else h::(remove x t)
|[]->[];;
let rec diff a b =
match b with
|[]-> a
| h::t -> diff (remove h a) t;;
(* Wrapper for old P3*)
let rec minus a b = diff a b;;
let rec union a b =
match a with
| h::t -> insert h (union t b)
|[]->
(match b with
| h::t -> insert h (union [] t)
|[]->[]);;
let rec intersection a b =
match a with
| h::t -> if elem h b then insert h (intersection t b) else (intersection t b)
|[]->[];;
let rec product a b =
let rec product_help x b =
match b with
| h::t -> insert (x, h)(product_help x t)
|[]->[] in
match a with
| h::t -> union (product_help h b)(product t b)
|[]->[];;
let rec cat x a =
match a with
|[]->[]
| h::t ->(x,h)::(cat x t);;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!