Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- toDfa(): FA {
- const closure: FA[] = this.fillClosure();
- const p: Set<number> = new Set<number>();
- for (const ffa of closure) {
- p.add(0);
- for (const t of ffa.transitions) {
- p.add(t.min);
- if (t.max < 0x10ffff) {
- p.add(t.max + 1);
- }
- }
- }
- const points: number[] = [...p];
- points.sort((x, y) => x - y);
- const sets: Map<Set<FA>, Set<FA>> = new Map<Set<FA>, Set<FA>>();
- const working: Set<FA>[] = [];
- const dfaMap: Map<Set<FA>, FA> = new Map<Set<FA>, FA>();
- const initial: Set<FA> = new Set<FA>();
- initial.add(this);
- sets.set(initial, initial);
- working.push(initial);
- const result: FA = new FA();
- for (const ffa of initial) {
- if (ffa.isAccepting()) {
- result.acceptSymbol = ffa.acceptSymbol;
- break;
- }
- }
- dfaMap.set(initial, result);
- while (working.length > 0) {
- const s: Set<FA> = working[0];
- working.shift();
- const dfa: FA = dfaMap.get(s)!;
- for (const q of s) {
- if (q.isAccepting()) {
- dfa.acceptSymbol = q.acceptSymbol;
- break;
- }
- }
- let i: number = 0;
- for (const pnt of points) {
- let set: Set<FA> = new Set<FA>();
- for (const c of s) {
- for (let trns of c.transitions) {
- if (trns.min <= pnt && pnt <= trns.max) {
- set.add(trns.to);
- }
- }
- }
- if (!sets.has(set)) {
- sets.set(set, set);
- working.push(set);
- let newFa: FA = new FA();
- dfaMap.set(set, newFa);
- }
- const dst: FA = dfaMap.get(set)!;
- const first: number = pnt;
- let last: number;
- if (i + 1 < points.length) {
- last = points[i + 1] - 1;
- } else {
- last = 0x10ffff;
- }
- dfa.transitions.push(new FATransition(first, last, dst));
- ++i;
- }
- }
- for (const ffa of result.fillClosure()) {
- const itrns: FATransition[] = [...ffa.transitions];
- for (const trns of itrns) {
- const acc: FA[] = trns.to.fillClosure().filter((value: FA, index: number, array: FA[]) => value.isAccepting());
- if (acc.length == 0) {
- ffa.transitions.splice(ffa.transitions.indexOf(trns), 1);
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement