-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.htm
415 lines (393 loc) · 12.3 KB
/
Parser.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!DOCTYPE HTML>
<html>
<head>
<title>Création de module Parser</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<style>
#eResult {
clear: both;
}
#eGrammarInput {
height: 100px;
width: 250px;
}
#ePhraseInput {
width: 300px;
}
#eExport {
font-size: 0.8em;
height: auto;
width: 90%;
margin: 1em;
}
FIELDSET {
float: left;
margin: 0 0 1em 1em;
}
FIELDSET DL {
margin: 0;
}
#eResultTable,
#eResultTree {
float: left;
margin: 0 0 1em 1em;
}
HR {
clear: left;
}
</style>
</head>
<body spellcheck="false">
<a href="index.htm">Index</a>
<h1>Création de module Parser</h1>
<h2>Grammaire</h2>
<fieldset><legend>Entrée</legend>
<dl>
<dt><label for="eTest">Exemple: </label>
<select id="eTest"></select></dt>
<dt><label for="eParser">Parser: </label>
<select id="eParser">
<option value="LL1">LL(1)</option>
<option value="LR0" selected>LR(0)</option>
<option value="SLR">SLR</option>
<option value="LR1">LR(1)</option>
</select>
<a href="#" onclick="grammarSyntax()">syntaxe</a>
</dt>
<dt><label for="eGrammarInput">Règles: </label>
<textarea id="eGrammarInput" style="width:100%;">E -> E + T | T
T -> T * F | F
F -> ( E ) | id
</textarea>
</dt>
<dt><input type="button" value="Analyser" onclick="getGrammar( document.getElementById('eGrammarInput').value )"></dt>
</dl>
</fieldset>
<fieldset><legend>Résultat</legend>
<dl>
<dt><input id="eShowParsingTable" type="checkbox" checked>
<label for="eShowParsingTable">Parsing table.</label></dt>
<dt><input id="eShowFFTable" type="checkbox">
<label for="eShowFFTable">First / Follow table.</label></dt>
<dt><input id="eShowGrammar" type="checkbox">
<label for="eShowGrammar">Grammaire.</label></dt>
<dt><input id="eShowDFA" type="checkbox">
<label for="eShowDFA">Automate.</label></dt>
</dl>
</fieldset>
<div id="eParsingTable"></div>
<div id="eFFTable"></div>
<div id="eGrammar"></div>
<div id="eDFA"></div>
<hr>
<h2>Test</h2>
<fieldset><legend>Entrée</legend>
<dl>
<dt><input type="text" id="ePhraseInput" value="id * id + id"></dt>
<dt><input type="button" value="Analyser" onclick="parsePhrase()"></dt>
</dl>
</fieldset>
<fieldset><legend>Résultat</legend>
<dl>
<dt><input id="eShowResultTree" type="checkbox">
<label for="eShowResultTree">Arbre de syntaxe (CST).</label></dt>
<dt><input id="eShowResultTable" type="checkbox" checked>
<label for="eShowResultTable">Analyse décriptée.</label></dt>
</dl>
</fieldset>
<pre id="eResultTree"></pre>
<pre id="eResultTable"></pre>
<hr>
<h2>Export JavaScript</h2>
<input type="button" value="Exporter" onclick="exportEngine( ENGINE, eWhiteSpaces.checked )">
<input id="eWhiteSpaces" type="checkbox" checked><label for="eWhiteSpaces">Espaces blanc.</label>
<textarea id="eExport" wrap="off"></textarea>
<div><label>Nombre de caractères : </label><b id="eExportSize"></b></div>
<!-- FRAMEWORK... -->
<script src="js/framework.js"></script>
<script src="js/lexer.class.js"></script>
<script src="js/lexer.automaton.js"></script>
<script src="js/lexer.automaton.modules.js"></script>
<!-- TODO: Rajouter les '
<script src="src/grammar/node.js"></script>
<script src="src/grammar/lexer.js"></script>
<script src="src/grammar/parser.js"></script>
-->
<script src="js/grammar.js"></script>
<script src="js/grammar.examples.js"></script>
<script>
_( 'eShowGrammar,eShowDFA,eShowFFTable,eShowParsingTable' )
_( 'eTest,eParser,eGrammar,eDFA,eFFTable,eParsingTable,eGrammarInput,ePhraseInput' )
_( 'eShowResultTable,eShowResultTree' )
_( 'eResultTable,eResultTree' )
_( 'eExport,eWhiteSpaces' )
showFrom( eShowGrammar, eGrammar )()
showFrom( eShowDFA, eDFA )()
showFrom( eShowFFTable, eFFTable )()
showFrom( eShowParsingTable, eParsingTable )()
showFrom( eShowResultTree, eResultTree )()
showFrom( eShowResultTable, eResultTable )()
var ENGINE
var Export ={
clear :function(){
eExport.value = ''
eExportSize.innerHTML = 0
},
set :function( sResult ){
eExport.value = sResult
eExport.rows = sResult.countLines()
eExportSize.innerHTML = sResult.length
}
}
var exportEngine =function( ENGINE, bWhiteSpaces ){
if( ! ENGINE ) return eExport.innerHTML = 'Pas de machine...'
if( eParser.value=='LL1' ) return eExport.innerHTML = "TODO: y créer..."
var tmp = []
ENGINE.PRODUCTIONS.every( function( P, i ){
return tmp.push( i+':['+ JSON.stringify(P.LHS) +','+ JSON.stringify(P.RHS) +']' )
})
tmp.shift()
var sProductions = bWhiteSpaces
? '{\n\t\t'+ tmp.join(',\n\t\t') +'\n\t\t}'
: '{'+ tmp.join(',') +'}'
var SYMBOLS = ['END_TOKENS'].concat( ENGINE.T_and_N )
var TableSymbols=(function(){
var nIndex = 0
var aByIndex = []
var oBySymbol = {}
return {
indexOf :function( symbol ){ return oBySymbol[symbol] },
symbolAt :function( index ){ return aByIndex[index] },
push :function( symbol ){
if( oBySymbol[symbol]!==undefined ) return false
oBySymbol[symbol] = nIndex
aByIndex[nIndex] = symbol
nIndex++
return true
},
toString :function( bWhiteSpaces ){
var a = []
var s = bWhiteSpaces ? '\n\t\t' : ''
for(var i=1; i<nIndex; i++)
a.push( JSON.stringify( aByIndex[i])+':'+i )
return '{'+ s + a.join(','+ s ) + s + '}'
},
}
})()
SYMBOLS.every( function( TorN ){ return TableSymbols.push( TorN )})
var tmp = []
var M = []
ENGINE.STATES.every( function( S, i ){
i = i + 1
SYMBOLS.every( function( TorN ){
if( ENGINE.M[i]){
var action = ENGINE.M[i][ TorN ]
if( action ){
M[i] = M[i] || []
action = action.constructor==String ? action : action[0]
action = action.constructor==Array ? action[0] : action
M[i][ TableSymbols.indexOf( TorN ) ]="'"+action+"'"
}
}
return 1
})
return 1
})
ENGINE.STATES.every( function( S, i ){
i = i + 1
if( M[ i ]) tmp.push( i +":["+ M[ i ].join(',') +"]" )
return 1
})
var sMatrice = bWhiteSpaces
? '{\n\t\t'+ tmp.join(',\n\t\t') +'\n\t\t}'
: '{'+ tmp.join(',') +'}'
var fPartialTree = function( mParent, mChild ){
var e = ! mParent
? document.createDocumentFragment()
:( mParent.appendChild ? mParent : document.createElement( mParent )
)
if( ! e.className ) e.className = 'myNode '+ e.nodeName.toLowerCase()
if( ! e.title ) e.title = e.nodeName
if( mChild.constructor==Array )
for(var i=0, ni=mChild.length; i<ni; i++ )
if( mChild[i]) e.appendChild( mChild[i])
else e.appendChild( mChild )
return e
}
var sASTFunction = 'function( sProd, LHS, RHS ){\n\
var f ='+ fPartialTree.toString() + '\n\
switch( sProd ){\n\
{options}\n\
}\n\
}'
var aOptions = []
ENGINE.PRODUCTIONS.every( function( P, prodID ){
aOptions.push( '\t\tcase "('+ prodID +') '+ P.LHS +' -> '+ P.RHS.join(' ')+'": return f( LHS, RHS )')
return 1
})
Export.set(
'var ENGINE = {'+
'\n\tSYMBOLS:'+ TableSymbols.toString( bWhiteSpaces ) +','+
'\n\tPRODUCTIONS:'+ sProductions +','+
'\n\tMATRICE:'+ sMatrice +','+
'\n\tAST:'+ sASTFunction.replace( '{options}', aOptions.join('\n')).split('\n').join('\n\t') +
'\n\t}' )
}
var getGrammar =function( sText ){
ENGINE = Grammar( sText, eParser.value )
eGrammar.innerHTML = showGrammar( ENGINE )
eDFA.innerHTML = showDFA( ENGINE )
eFFTable.innerHTML = showFFTable( ENGINE )
eParsingTable.innerHTML = window[{
LL1:'showLLTable',
LR0:'showLRTable',
SLR:'showLRTable',
LR1:'showLRTable'
}[ eParser.value ]]( ENGINE )
}
var parsePhrase =function(){
var oResultat = parse( ePhraseInput.value )
eResultTable.innerHTML = oResultat.table
eResultTree.innerHTML = ''
if( oResultat.tree ) eResultTree.appendChild( oResultat.tree )
}
var parse =function( sText ){
return Grammar[{
LL1:'parseLL',
LR0:'parseLR',
SLR:'parseLR',
LR1:'parseLR'
}[ eParser.value ]]( sText ? sText.split(' ') : [] , ENGINE )
}
function showGrammar ( ENGINE ){
Bufferize.init( '<div class="grammar">' )
Bufferize( '<dl class="G"><dt>G = ( T, N, s, δ )</dt>' )
ENGINE.G.every(function( a ){
return Bufferize( '<dd>'+a[0]+' → '+a[1]+'</dd>' )
})
Bufferize( '</dl>' )
Bufferize( '<dl class="delta"><dt>δ</dt>' )
ENGINE.PRODUCTIONS.every(function( a, i ){
return Bufferize( '<dd> <i><small>('+ i +')</small></i> '+ a.toHTML(1) +'</dd>' )
})
Bufferize( '</dl>' )
Bufferize( '<dl class="T"><dt>T</dt><dd>'+ ENGINE.T.join(' ') +'</dd></dl>' )
Bufferize( '<dl class="N"><dt>N</dt><dd>'+ ENGINE.N.join(' ') +'</dd></dl>' )
return Bufferize( '</div>' )
}
function showDFA ( ENGINE ){
Bufferize.init( '<div class="grammar">' )
if( ENGINE.E.toString() ){
Bufferize( '<dl class="transitions">' )
Bufferize( '<dt>Transitions</dt>' )
Bufferize( '<dd><pre>'+ ENGINE.E.toString() +'</pre></dd>' )
Bufferize(' </dl>' )
}
ENGINE.STATES.every( function( State ){
Bufferize( '<dl class="state">' )
Bufferize( '<dt>'+ State.stateID +'</dt>' )
Bufferize( '<dd><pre>'+ State.toString() +'</pre></dd>' )
return Bufferize( '</dl>' )
})
return Bufferize( '</div>' )
}
function showFFTable ( ENGINE ){
Bufferize.init( '<table cellspacing="0" class="Table FirstFollow">' )
Bufferize( '<thead>' )
Bufferize( '<tr><th colspan="4">Table First/Follow</th></tr>' )
Bufferize( '<tr><th>Symbol</th><th>Nullable</th><th>First</th><th>Follow</th></tr>' )
Bufferize( '</thead>' )
Bufferize( '<tbody>' )
Bufferize(' <tr><th>ε</th><td>'+ ENGINE.NULLABLE['ε'] +'</td><td class="nowrap">'+ ENGINE.FIRST['ε'].join(' ') +'</td><td></td></tr>' )
ENGINE.T_and_N.every( function( s ){
var sNULLABLE = ENGINE.NULLABLE[s]===undefined ? '?' : ENGINE.NULLABLE[s]
var aFIRST = ENGINE.FIRST[s] || []
var aFOLLOW = ENGINE.FOLLOW[s] || []
return Bufferize( '<tr><th>'+ s +'</th><td>'+ sNULLABLE +'</td><td class="nowrap">'+ aFIRST.join(' ') +'</td><td class="nowrap">'+ aFOLLOW.join(' ') +'</td></tr>' )
})
Bufferize( '</tbody>' )
return Bufferize('</table>')
}
function showLRTable ( ENGINE ){
var a = ENGINE.T.concat([ENGINE.end], ENGINE.N )
Bufferize.init( '<table cellspacing="0" cellpadding="0" class="Table">' )
Bufferize( '<thead>' )
Bufferize( '<tr><th colspan="'+(a.length+1)+'">Table</th></tr>' )
Bufferize( '<tr><td></td>' )
a.every( function( S ){ return Bufferize( "<th>"+ S +"</th>" )})
Bufferize( '</tr>' )
Bufferize( '</thead>' )
Bufferize( '<tbody>' )
ENGINE.STATES.every( function( State, i ){
var nStateID = State.stateID
Bufferize( "<tr><th>"+ nStateID +"</th>" )
a.every( function( X ){
var aActions = ENGINE.M[nStateID][X]
return Bufferize( '<td class="nowrap '+ (aActions && aActions.length>1 ? 'bg_orange' : '' ) +'">'+ ( aActions || ' ' ) +"</td>" )
})
return Bufferize( "</tr>" )
})
Bufferize( '</tbody>' )
return Bufferize( "</table>" )
}
function showLLTable ( ENGINE ){
var a = ENGINE.T.concat([ ENGINE.end, ENGINE.epsilon ])
Bufferize.init( '<table cellspacing="0" class="Table LL1">' )
Bufferize( '<thead>' )
Bufferize( '<tr><th colspan="'+(a.length+1)+'">Table LL(1)</th></tr>' )
Bufferize( '<tr><td></td>' )
a.every( function( S ){
return Bufferize( "<th>"+ S +"</th>" )
})
Bufferize( '</tr>' )
Bufferize( '</thead>' )
Bufferize( '<tbody>' )
ENGINE.N.every( function( sN ){
Bufferize( "<tr><th>"+ sN +"</th>" )
a.every( function( sS ){
return Bufferize( '<td class="nowrap '+ (ENGINE.M[sN][sS] && ENGINE.M[sN][sS].length>1 ? 'bg_orange' : '' ) +'">'+ ( ENGINE.M[sN][sS] || ' ' ) +"</td>" )
})
return Bufferize( "</tr>" )
})
Bufferize( '</tbody>' )
return Bufferize( "</table>" )
}
eParser.onchange =function(){
eGrammar.innerHTML = ''
eFFTable.innerHTML = ''
eParsingTable.innerHTML = ''
eResultTree.innerHTML = ''
eResultTable.innerHTML = ''
getGrammar( document.getElementById('eGrammarInput').value )
parsePhrase()
}
eTest.onchange =function(){
var aTest = aExamples[eTest.value]
, bMultipleTest = aTest[2].constructor==Array
eGrammarInput.value = aTest[1]
ePhraseInput.value = bMultipleTest ? aTest[2][0] : aTest[2]
eParser.value = aTest[3]
eParser.onchange()
if( bMultipleTest ){
var sResult = ''
aTest[2].every( function( sTest, i ){
if( i>0 ){
var oResultat = parse( sTest )
sResult += oResultat.table
eResultTree.appendChild( oResultat.tree )
}
return 1
})
eResultTable.innerHTML += sResult
}
}
aExamples.every( function( a, i ){
var e = document.createElement( 'option' )
e.innerHTML = a[0]
e.value = i
e.selected = a[4]
return eTest.appendChild( e )
})
</script>
</body>
</html>