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 regexpt
EmptyString
Char of char
Union of regexpt regexpt
Concat of regexpt regexpt
Star of regexpt
EmptyString 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 r r represents the regular expression that is the union of r and r For example, UnionChar a Char'b' is the same as the formal regular expression ab
Concat r r represents the concatenation of r followed by r For example, ConcatChar 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 ab
let fresh
let cntr ref in
fun
cntr :cntr ;
cntr;;
stringtonfa 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 regexptonfa function. As such, for this function to work, your regexptonfa function must be working. In the starter files, we have provided the function stringtoregexp that parses strings into regexp values, described next.
stringtoregexp 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:
stringtoregexp a Char a
stringtoregexp ab Union Char a Char b
stringtoregexp ab ConcatChar a Char b
stringtoregexp "aab" ConcatChar aConcatChar a Char b
stringtoregexp aE StarUnionChar aEmptyString
stringtoregexp aEab ConcatStarUnionChar aEmptyStringUnionChar aChar b
type qs transi q s option q
type qs nfay sigma : s list; qs : q list; q : q; fs : q list; delta : qs transi list;
So: let nfa sigma z; qs ; ; ; q; fs ; delta Some z; None,
let dfa sigma z; y; x; qs ; ; ; q; fs ; delta Some z; Some y; Some x;;
mo n ql t of Type: qs nfay 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 Some z
mo nfa Some z
mo nfa Some z
mo nfa ;Some z
mo nfa None
eclo n ql of Type: qs nfay 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: eclo nfa
eclo nfa ;
eclo nfa
eclo nfa ;;;
acce n t of Type: q char nfay string bool, which takes an NFA nfa and a string s and returns true if the NFA accepts the string.
So: acce dfa false;; dfaex 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 then l else exp i si :: l
in
exp Stringlength s ;;
let rec elem x a
match a with
h::t h xelem x t
false;;
let rec insert x a
if not elem x a then x::a else a;;
let insertall xs a
List.foldright 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 P
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 producthelp x b
match b with
h::t insert x hproducthelp x t
in
match a with
h::t union producthelp h bproduct t b
;;
let rec cat x a
match a with
h::t xh::cat x t;;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
