Skip to content

Instantly share code, notes, and snippets.

@martinwairegi
Created March 27, 2023 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinwairegi/58293818a98e512105acf2f3461e3012 to your computer and use it in GitHub Desktop.
Save martinwairegi/58293818a98e512105acf2f3461e3012 to your computer and use it in GitHub Desktop.
class Regex {
constructor() {
this.generate();
}
/**
* Generate new regular expression and corresponding NFA
*/
generate() {
this.postfix = "";
this.regex = this.#kleene(7, 0.5, 0.2, 0.1);
this.nfa = this.#regexToNfa(this.postfix);
}
/**
* Convert regular expression into equivalent NFA using Thompson's construction
* @param {String} regex A regular expression in postfix
* @returns {
* Array<{
* table:
* Array<{
* stateID: Number,
* symbol: String,
* stateIDs: Array<Number>
* }>,
* start: Number,
* end: Array<Number>
* }>
* } State transition table for NFA accepting regex
* (alongside start state and accept states)
*/
#regexToNfa(regex) {
// ...
}
/**
* Generate a random regular expression using probabilistic methods
* @param {Number} n Maximum depth of the regular expression tree
* @param {Number} p Literal probability
* @param {Number} s Concatenation probability
* @param {Number} k Kleene star probability
* @returns {String} Random regular expression
*/
#kleene(n, p, s, k) {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment