-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCW2.html
146 lines (132 loc) · 3.24 KB
/
CW2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>NFA, DFA and Regex for (1|0)*00 </title>
</head>
<body>
<h2 id=title></h2>
<p>NFA, DFA and Regex</p>
w = <input id=input type=text value=50
onChange="test()">  
F = <input id=final type=text value=c
onChange="test()" style="width:30px">
<pre id=out></pre>
<hr />
<b style="float:left">Logic</b>
<style>
table {
border-collapse: collapse;
margin: 0 50px;
}
th {
color: blue;
padding: 5px 12px;
}
td {
border: 1px solid blue;
padding: 5px 12px;
text-align: center;
}
</style>
<table style="float:left">
<tr><th></th><th>0</th><th>1</th></tr>
<tr><th>> a</th><td>ab</td><td>a</td></tr>
<tr><th> b</th><td>c</td><td>ϕ</td></tr>
<tr><th>* c</th><td>ϕ</td><td>ϕ</td></tr>
</table>
<table >
<tr><th></th><th>0</th><th>1</th></tr>
<tr><th>> a</th><td>B</td><td>A</td></tr>
<tr><th> b</th><td>C</td><td>A</td></tr>
<tr><th>* c</th><td>C</td><td>A</td></tr>
</table>
<pre id=sample></pre>
<hr />
<script>
"use strict";
function union(a, b) { //set operation using strings
let s = a
for (let x of b) if (!a.includes(x)) s += x
return s
}
function intersect(a, b) {
let s = ''
for (let x of b) if (a.includes(x)) s += x
return s
}
function deltaNfa(q, c) { // (1|0)*10
if (q=='a' && c=='0') return 'ab'
if (q=='a' && c=='1') return 'a'
if (q=='b' && c=='0') return 'c'
return ''; //default -- no transition
}
function deltaDfa(q, c) { // (1|0)*00
if (q=='A' && c=='0') return 'B'
if (q=='A' && c=='1') return 'A'
if (q=='B' && c=='0') return 'C'
if (q=='B' && c=='1') return 'A'
if (q=='C' && c=='0') return 'C'
if (q=='C' && c=='1') return 'A'
return ''; //default -- no transition
}
function acceptNfa(w, F='c', Q='a') {
//w: input String
//F: final state(s)
//Q: current state(s)
let i = 0, txt = Q
while (i < w.length) {
let c = w[i], T=''
for (let q of Q)
T = union(T, deltaNfa(q, c))
Q = T
if (Q == '') break
i++;
}
input.selectionStart = i
input.selectionEnd = i+1
let a = intersect(Q, F).length > 0
return a;
}
function acceptDfa(w, F='C', q='A') {
//w: input String
//F: final state(s)
//q: current state
let i = 0, txt = q
while (i < w.length) {
q = deltaDfa(q, w[i])
if (q == '') break
i++;
}
input.selectionStart = i
input.selectionEnd = i+1
let a = (q!='' && F.includes(q))
return a;
}
function regex(w){
var myReg = new RegExp(/00$/);
var a = myReg.test(w);
return a;
}
function parser(ww,f){
let nfa = []
let dfa = []
let rgx = []
for (let n=1; n<ww; n++) {
let w = n.toString(2) // to binary
if (acceptNfa(w)) nfa.push(n);
if (acceptDfa(w)) dfa.push(n);
if (regex(w)) rgx.push(n);
}
out.innerHTML = "DFA: "+dfa+"\n"+"NFA: "+nfa+"\n"+"Regex: "+rgx;
}
function test() {
parser(input.value, final.value);
}
title.innerText = document.title;
sample.innerText = deltaNfa+'\n'+deltaDfa+'\n'+regex;
test();
</script>
</body>
</html>