-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbnf.pl
More file actions
455 lines (412 loc) · 11.1 KB
/
bnf.pl
File metadata and controls
455 lines (412 loc) · 11.1 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/speedy -w -- -gwiki -M5 -r30
# License: Creative Commons Attribution-ShareAlike 4.0, http://creativecommons.org/licenses/by-sa/4.0/
# Creator repo: https://github.com/sl236/BNF
use strict;
use CGI;
use PageFetch;
use vars qw($maxiter $maxlen %options %includes %tags $validtag $commandchar);
sub ResetGlobals
{
# hard options
$maxiter = 16000; # maximum number of tags to resolve before giving up
$maxlen = 64000; # maximum length of generated string
# defaults for mutable options
%options =
(
'spaces' => 1, # put spaces between elements
'debug' => 0, # generate debug trace
'evil' => 0 # append Google ad
);
%includes = ( );
# bnf syntax
# name ::= |-separated-list of list of name or string or "string"
# root tag is bnf
# implemented as hash of array of array
%tags = ( );
$validtag = qw/(?:(?:(?:<)[A-Za-z0-9_-]+?(?:>))|(?:[A-Za-z0-9_-]+))/;
$commandchar = '!';
# commandchar must be a character that's not legal at the start of tag names (i.e. not present in $validtag)
}
sub parseBnfLine
{
my ($line) = @_;
$line =~ /^ *($validtag) *::= *(.*) *$/ or return;
my $name = $1;
$line = $2;
if($name eq 'option')
{
if($line =~ /([A-Za-z_]+) *= *([^ ]+)/)
{
$options{$1} = $2;
}
}
my $sequences = [ ];
my $curr_sequence = [ ];
while(length $line > 0)
{
$line =~ s/^ *//; # strip leading spaces
if($line =~ /^(\".*?[^\\]\")(.*)/)
{
# we have a quoted string as the next symbol
my $symbol = $1;
$line = $2;
$symbol =~ s/\\(.)/$1/g;
push @$curr_sequence, $symbol;
}
else
{
my $symbol = $line;
if($line =~ /^([^ ]+) (.*)$/)
{
$symbol = $1;
$line = $2;
}
else
{
$line = '';
}
$symbol =~ s/\\(.)/$1/g;
if($symbol eq "|")
{
if($#$curr_sequence >= 0)
{
push @$sequences, $curr_sequence;
$curr_sequence = [ ];
}
}
else
{
if($symbol =~ /^$commandchar/)
{
# they're starting a symbol with an unquoted command char
# we can't have that: quote it
# we know there's no bad effects to this because commandchar
# is not legal in tag names
$symbol = '"' . $symbol . '"';
}
push @$curr_sequence, $symbol;
}
}
}
if($#$curr_sequence >= 0)
{
push @$sequences, $curr_sequence;
}
$tags{$name} = $sequences;
}
sub dumpBNF
{
foreach my $k (keys %tags)
{
print "$k ::= ";
my $i = -1;
while(++$i < $#{$tags{$k}})
{
my $seq = $tags{$k}->[$i];
foreach my $item(@$seq)
{
my $titem = $item;
if($titem =~ / /)
{
$titem = '"' . $titem . '"';
}
print "$titem ";
}
print "| ";
}
{
my $seq = $tags{$k}->[$#{$tags{$k}}];
foreach my $item(@$seq)
{
my $titem = $item;
if($titem =~ / /)
{
$titem = '"' . $titem . '"';
}
print "$titem ";
}
}
print "<BR>\n";
}
print "<BR>\n";
}
sub resolveConcats
{
my ($tag) = @_;
while($tag =~ /^($validtag)\#\#($validtag)(.*?)$/)
{
my $root = $1;
my $ccat = $2;
my $rest = '';
if(defined($3))
{
$rest = $3;
}
$options{'debug'} and print '<i>Concatenation:</i> '. $tag .
#' (' . $root . ',' . $ccat . ',' . $rest . ')' .
' --> ';
if(
defined($tags{$ccat}) &&
($#{$tags{$ccat}} == 0) &&
($#{$tags{$ccat}->[0]} == 0) &&
($tags{$ccat}->[0][0] =~/^$validtag$/)
)
{
$tag = $root . '_' . $tags{$ccat}->[0][0] . $rest;
}
else
{
$tag = $root . '_' . $ccat . $rest;
}
$options{'debug'} and print $tag;
$options{'debug'} and print " <BR>\n";
}
return $tag;
}
sub generateString
{
my @results = ( '' );
my @unresolved = ( "bnf" );
my $iter = 0;
while($#unresolved >= 0)
{
my $tag = shift @unresolved;
my $silent = '';
my $iterresult = [ ];
my @postassign = ( );
# -------- note silent assigns and postfix assigns
if($tag =~ /^<($validtag(?:\#\#$validtag)*)::=($validtag(?:\#\#$validtag)*)>$/)
{
# silent assign
$silent = resolveConcats($1);
$tag = $2;
}
elsif($tag =~ /^<($validtag(?:\#\#$validtag)*)::=("[^"]*")>$/)
{
# quoted silent assign
$silent = resolveConcats($1);
$tag = $2;
}
elsif($tag =~ /^<<($validtag(?:\#\#$validtag)*)::=($validtag(?:\#\#$validtag)*)>>$/)
{
# deep silent assign
$tag = $2;
my $deepname = resolveConcats($1);
$options{'debug'} and print "Start deep calculation of $tag to assign to $deepname<BR>\n";
my $ctag = $commandchar . 'deepname' . ' ' . $deepname;
unshift @unresolved, $ctag;
unshift @results, '';
}
$tag =~ /^".*"$/ or $tag = resolveConcats($tag);
# do we have any postfix assigns?
while($tag =~ /^($validtag){($validtag(?:\#\#$validtag)*)}(.*)$/ or $tag =~ /^($validtag){{($validtag(?:\#\#$validtag)*)}}(.*)$/)
{
if ($tag =~ /^($validtag){($validtag(?:\#\#$validtag)*)}(.*)$/)
{
# shallow postfix assign
push @postassign, $2;
$tag = $1;
defined($3) and $tag = $tag . $3;
}
elsif ($tag =~ /^($validtag){{($validtag(?:\#\#$validtag)*)}}(.*)$/)
{
# deep postfix assign
# because we only want to output to one result at once, convert this
# from foo{{bar}} to <<bar::=foo>> bar
$options{'debug'} and print "Start deep calculation of $1 to assign to $2<BR>\n";
unshift @unresolved, $2; # prepend a bar
my $ctag = $commandchar . 'deepname' . ' ' . $2;
unshift @unresolved, $ctag; # then prepend the command to assign to bar
unshift @results, '';
$tag = $1;
defined($3) and $tag = $tag . $3;
}
}
# -------- Resolve the tag
if (($iter++ > $maxiter) || (length($results[$#results]) > $maxlen))
{
return $results[0] . "<BR>(...)";
}
$options{'debug'} and print "Iteration $iter: ";
if ( $tag =~ /^($commandchar)/ )
{
# this is a command
#print "Found command '$tag'! ";
if ( $tag =~ /^($commandchar)deepname (.*)$/ )
{
# we've completed a deep assign
my $deepassignvar = shift(@results);
# all the tags come out of a deep assign flattened into a string,
# so we have to wrap them twice in []s to get the required array of arrays
$tags{$2} = [ [ $deepassignvar ] ];
if($options{'debug'})
{
print 'Deep assign: (' . $2 . '::=' . $deepassignvar . ")<br>\n";
}
}
}
elsif( $tags{$tag} )
{
# select a possible sequence at random
my $index = int( rand( $#{$tags{$tag}} + 1 ) );
defined $index or $index = 0;
# add the items in the sequence to the unresolved list
if($options{'debug'})
{
print "<i>Lookup:</i> $tag ::=";
foreach my $item(@{${$tags{$tag}}[$index]})
{
print " $item";
}
print "<BR>\n";
}
if(defined(${$tags{$tag}}[$index]))
{
foreach my $item(reverse @{${$tags{$tag}}[$index]})
{
if($silent eq '')
{
unshift @unresolved, $item;
}
unshift @$iterresult, $item;
}
}
}
else
{
my $ttag = $tag;
$ttag =~ s/^"(.*)"$/$1/;
if($silent eq '')
{
if( ((length($results[0])>0) && (length($ttag)>0) && ($options{'spaces'})) )
{
$results[0] .= " ";
}
$results[0] .= $ttag;
}
push @$iterresult, $tag;
}
if($silent ne '')
{
if($options{'debug'})
{
print '<i>Silent assign:</i> (' . $silent . '::=' ;
foreach my $item(@$iterresult )
{
print $item;
}
print ")<BR>\n";
}
$tags{$silent} = [ $iterresult ];
}
# else
{
foreach my $item(@postassign)
{
if($options{'debug'})
{
print '<i>Postfix assign:</i> {' . $item . '::=' ;
foreach my $i(@$iterresult )
{
print $i;
}
print "}<BR>\n";
}
$tags{$item} = [ $iterresult ];
}
}
}
return $results[0];
}
sub WikiLink
{
my ($name) = @_;
return '<A HREF="http://www.toothycat.net/wiki/wiki.pl?' .
$name . '">' . $name . '</A>';
}
sub main()
{
ResetGlobals();
srand();
my $seed = int(rand()*(1<<31));
my $q = new CGI;
print $q->header, $q->start_html("BNF generator");
my $s = $q->param('seed');
if(defined($s) && ($s =~ /([0-9-]+)/))
{
$seed = $1;
}
print '<!-- Seed: ' . $seed . ' -->'."\n";
srand($seed);
if(defined($q->param('page')))
{
my @text = split(/[\r\n]/, FetchPageText('/home/sham/root/wiki/data',
$q->param('page')));
while($#text>-1)
{
my $item = shift @text;
$item =~ /[^ ]/ or next;
$options{'include'} = 0;
&parseBnfLine($item);
if($options{'include'} && !defined($includes{$options{'include'}}))
{
my @newtext = split(/[\r\n]/,
FetchPageText('/home/sham/root/wiki/data', $options{'include'}));
@text = (@newtext, @text);
$includes{$options{'include'}} = 1;
}
}
if(defined($q->param('debug')))
{
$options{'debug'} = 1;
}
if(defined $tags{"bnf"}) # success
{
$options{'debug'} and print '<B>Here, in no particular order, are the rules I found:</B><BR>';
$options{'debug'} and &dumpBNF();
$options{'debug'} and print '<BR><HR>';
$options{'debug'} and print '<B>This is what I did with them:</B><BR>';
my $result = &generateString();
$options{'debug'} and print '<BR><HR>';
$options{'debug'} and print '<B>..and here\'s what I got:</B><BR>';
print $result;
print '<BR><HR>';
print &WikiLink($q->param('page'));
}
else
{
print 'No "bnf" tag was found. See ' . &WikiLink("MoonShadow/GeneratorGenerator") . ' for more details.<BR>';
#print join("<BR>", @text);
}
}
else
{
print 'Invoke using a link to http://www.toothycat.net/wiki/bnf.pl?page=<I>HomePage/SubPageName</I>. See ' . &WikiLink("MoonShadow/GeneratorGenerator") . ' for more details.';
}
if( $options{'evil'} )
{
print <<'AD'
<div style="float: right">
<script type="text/javascript"><!--
google_ad_client = "pub-3160732673995309";
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = "234x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "EEEEEE";
google_color_bg = "FFFFFF";
google_color_link = "AAAAAA";
google_color_url = "CCCCCC";
google_color_text = "999999";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
AD
;
}
print $q->end_html;
}
&main();