-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFD.aggregator.htm
346 lines (318 loc) · 9.22 KB
/
AFD.aggregator.htm
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE HTML>
<html>
<head>
<title>Aggrégation d'AFD</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<style>
#eRETree {
margin: 0.5em;
padding: 2px;
text-align: center;
}
#eRESyntax {
border:1px solid #000;
padding: 0.5em;
width:100%;
}
#eExportResult {
border:1px solid #000;
padding: 0.5em;
height:100px;
width:90%;
}
FIELDSET {
float: left;
margin: 0 1% 1em 0;
padding: 1%;
}
FIELDSET LEGEND {
font-weight: bold;
}
FIELDSET DL {
margin: 0;
}
</style>
</head>
<body spellcheck="false">
<div id="eTITLE">
<a href="index.htm">Index</a>
<h1>Aggrégation d'AFD</h1>
</div>
<div id="eMenu">
<label for="eRegExpList">Tokens : </label>
<select id="eRegExpList"></select>
<input type="button" id="eBtnAdd" value="Ajouter" title="">
<input type="button" id="eBtnClear" value="Initialiser" title="">
<hr>
<label for="eTokenID" title="">Nom de token : </label>
<input type="text" id="eTokenID">
<label for="eTokenRE" title="">Expression réguliére : </label>
<input type="text" id="eTokenRE">
<input type="button" id="eBtnAddToken" value="Ajouter Token" title="">
</div>
<hr style="clear:both;">
<input type="button" value="Visualiser l'AFD" onclick="if( eExportResult.value ) previewDFA()" title="Matrice et graphe de l'AFD.">
<input type="button" id="eBtnExport" value="Exporter" style="display:none;">
<input type="checkbox" id="eExportWhiteSpace"><label for="eExportWhiteSpace">Espaces blancs.</label>
<input type="checkbox" id="eExportCompressed"><label for="eExportCompressed">Compressé.</label>
<a href="AFD.compression.htm" target="_INFO">info</a>
<textarea id="eExportResult"></textarea>
<div><label>Nombre de caractères : </label><b id="eExportSize"></b></div>
<hr>
<fieldset><legend>Zone teste</legend>
<dl>
<dt>
<label for="eInput">Texte : </label>
<textarea id="eInput">1aabbaaab</textarea>
</dt>
<dt>
<input type="button" id="eBtnMatch" value="Analyser">
<input type="checkbox" id="eAutoMatch"><label for="eAutoMatch">Analyser automatiquement.</label>
</dt>
<dt><pre id="eResult"></pre></dt>
</dl>
</fieldset>
<fieldset><legend>AFD en RE <sup style="color:red;">experimental</sup></legend>
<dl>
<dt><input type="button" id="eBtnToRE" value="Analyser"></dt>
<dt><textarea id="eDFAToRE"></textarea></dt>
</dl>
</fieldset>
<!-- FRAMEWORK... -->
<script src="js/framework.js"></script>
<script>
var Lexeme =function( o ){
var e = document.createElement('B')
e.getChildNodes =function(){
var a = []
for(var eChild=e.firstChild; eChild ; ){
var eSibling = eChild
eChild=eChild.nextSibling
if( eSibling.nodeName=="B" ) a.push( eSibling )
}
return a
}
e.toString =function(){
return e.symbol
}
e.oValue = o
e.value = o.value
switch( o.token ){
case 'PIPE':
e.symbol = "|"
e.title = e.value
break;
case 'ANY':
e.symbol = e.value = 'ANY'
e.title = 'ANY_CHARACTER'
break;
case 'CHAR_ESCAPED':
case 'CHAR':
e.symbol = 'c'
e.title = e.value
break;
case 'QUANTIFIER1':
var a = o.value.split( ',' )
if( a.length==1){
e.n = o.value.slice( 1,-1)
e.symbol = '{n}'
}
else {
e.n = a[0].slice( 1 )
e.m = a[1].slice( 0,-1)
e.symbol = e.m ? '{n,m}' : '{n,}'
}
e.title = 'n:'+ e.n +', m:'+ e.m
break;
default:
e.symbol = o.value
e.title = e.value
}
e.innerHTML = '<DIV class="symbol">' + e.symbol + '</DIV>'
//.str_replace( [&,<,>], [&,<,>])
e.className = ( o.css||o.token.toLowerCase()) + ' myNode'
return e
}
</script>
<script src="js/lexer.class.js"></script>
<script src="js/lexer.automaton.js"></script>
<script src="js/lexer.automaton.modules.js"></script>
<script src="js/parserLR.js"></script>
<script src="src/regexp/node.js"></script>
<script src="src/regexp/lexer.js"></script>
<script src="src/regexp/parser.js"></script>
<script src="js/automaton.js"></script>
<!-- DIVERS... -->
<script src="js/regexp.examples.js"></script>
<script>
_( 'eTITLE,eMenu' )
_( 'eBtnClear' )
_( 'eBtnMatch,eInput,eAutoMatch,eResult' )
_( 'eRegExpList,eBtnAdd' )
_( 'eTokenID,eTokenRE,eBtnAddToken' )
_( 'eBtnExport,eExportResult,eExportWhiteSpace,eExportCompressed,eExportSize' )
_( 'eBtnToRE,eAutoToRE,eDFAToRE' )
eBtnClear.onclick =function(){ init()}
eBtnMatch.onclick =function(){ match()}
eBtnAdd.onclick =function(){
var sToken = eRegExpList.value
parseRE( sToken, oRegExp[ sToken ])
}
eBtnAddToken.onclick =function(){
var sNewToken = eTokenID.value
oRegExp[ sNewToken ] = eTokenRE.value
var eNewOption = document.createElement('OPTION')
eNewOption.value = eNewOption.innerHTML = sNewToken
eRegExpList.insertBefore( eNewOption, eRegExpList.firstChild )
}
eBtnToRE.onclick =function(){
eDFAToRE.value = oDFA.toRE()
}
var exportBasic =function(){ exportDFA( eExportWhiteSpace.checked, !eExportCompressed.checked )}
eExportWhiteSpace.onchange =exportBasic
eExportCompressed.onchange =exportBasic
var oRegExp= {}
;( function( mSelected ){
var _add =function( a ){
var eChild = document.createElement('OPTION')
oRegExp[ a[0]] = a[1].source
eChild.innerHTML = eChild.value = a[0]
eRegExpList.appendChild( eChild )
}
mSelected = mSelected || null
for( var i=0, ni=aRegExpList.length; i<ni; i++ ) _add( aRegExpList[i])
eRegExpList.value = mSelected
return eRegExpList
})().onchange = function(){
var sToken = eRegExpList.value
parseRE( sToken, oRegExp[ sToken ])
}
</script>
<script>
var oNFA, oDFA, oDFAAggregated, nStateIDCounter
previewDFA = (function(){
var f = function(){ previewFA({type:'DFA', oFA:oDFA}) }
f.refresh = function(){ previewFA.refresh({type:'DFA', oFA:oDFA}) }
return f
})()
var init =function(){
oDFA = oDFAAggregated = { I:1, F:[], A:[], S:[0,1], T:[] }
nStateIDCounter = 2
Export.clear()
previewDFA.refresh()
}
var parseRE =function( sToken, sRegExp ){
var oTree = RegExp2AST( sRegExp )
oNFA = NFA( oTree )
oDFA = ( new DFA( oNFA.validateAlphabet(), sToken )).minimize()
oDFA.renameStates( nStateIDCounter, true )
Automate.setUniqueID( oDFA.S[ oDFA.S.length-1 ]+1 )
oDFAAggregated = oDFA = DFA.aggregate( oDFAAggregated, oDFA )
if( oDFA.sError ) alert( oDFA.sError )
nStateIDCounter = oDFAAggregated.S[ oDFAAggregated.S.length-1 ]+1
exportBasic()
previewDFA.refresh()
if( eAutoMatch.checked ) match()
else clearTest()
}
var clearTest =function(){
eResult.innerHTML = ''
}
var match =function( s, n ){
var oDFA = oDFAAggregated
clearTest()
if( ! oDFA.M ) return ;
var sInput = s || eInput.value
, nIndex= n || 0
, nStart = nIndex
, sChar
, sState=oDFA.I
, LongestMatch = null
Bufferize.init( 'input: "<b>'+ sInput +'</b>"\n' )
if( sInput.length!=0 )
while( true ){
sChar = sInput[ nIndex ]
nIndex++
sNextState = oDFA.M.nextState( sState, sChar )
Bufferize( '('+ sState +') "'+ sChar +'" → ('+ sNextState +')\n' )
sState = sNextState
if( nIndex==sInput.length ) break;
if( sState<=0 ) break;
if( oDFA.F.have( sState )) LongestMatch = { state:sState, matched:sInput.substring( nStart, nIndex )}
}
if( oDFA.F.have( sState )) LongestMatch = { state:sState, matched:sInput.substring( nStart, nIndex )}
if( LongestMatch )
for(var i=0, aToken; aToken=oDFA.aTokensID[i]; i++)
if( aToken[1].have( LongestMatch.state )){
LongestMatch.token = aToken[0]
break;
}
if( ! sState ) Bufferize( '<small style="color:blue;">DEAD_NODE</small>\n' )
eResult.innerHTML = Bufferize(
'<b style="color:'+
( LongestMatch
? 'green;">MATCH "' + LongestMatch.matched.str_replace( ['&','<','>'], ['&','<','>'])
+'" '+ LongestMatch.token
: 'orange;">MATCH_FAILED')
+ '</b>\n'
// +\n+ eResult.innerHTML
)
}
var Export ={
clear :function(){
eExportResult.value = ''
eExportSize.innerHTML = 0
},
set :function( sResult ){
eExportResult.value = sResult
eExportSize.innerHTML = sResult.length
}
}
var exportDFA =function( bWhiteSpace, bUnCompressed ){
Export.clear()
var sResult = oDFAAggregated.toJS( bWhiteSpace, bUnCompressed )
Export.set( sResult )
if( aParentItemCalcul && window.parent ){
window.parent.Calcul.add( aParentItemCalcul, oDFAAggregated, sResult, 'nextAggregation' )
aItemCalcul = false
}
}
init()
</script>
<!-- URL QUERY -->
<script>
$_GET =(function(){
var o = {}
if( window.location.search.length > 1 ){
for(var m, n=0, aCouples = window.location.search.substr(1).split("&"); m=aCouples[n]; n++ ){
m = m.split("=")
o[unescape(m[0])] = m.length>1 ? unescape(m[1]) : ''
}
}
return o
})()
if( $_GET['calcul']){
eTITLE.innerHTML = '<h1>...</h1>'
eMenu.style.display = 'none'
}
var addDFA =function( sToken, oDFAAdded ){
oDFAAdded.renameStates( nStateIDCounter, true )
Automate.setUniqueID( oDFAAdded.S[ oDFAAdded.S.length-1 ]+1 )
oDFAAdded.aTokensID = [[ sToken, oDFAAdded.F ]]
oDFAAggregated = oDFA = DFA.aggregate( oDFAAggregated, oDFAAdded )
nStateIDCounter = oDFAAggregated.S[ oDFAAggregated.S.length-1 ]+1
exportDFA()
previewDFA.refresh()
}
var aItemCalcul
var aParentItemCalcul
var calculAggregation =function( parentItem, item ){
eMenu.style.display='none'
eTITLE.innerHTML = '<h1>Automate composé '+ parentItem[0] + '</h1>'
aItemCalcul = item
aParentItemCalcul = parentItem
addDFA( item.token, item.oFA )
}
</script>
</body>
</html>