common.anubis
24.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
The Anubis Project
Common Stuff for the Anubis Parser Generator.
Copyright (c) Alain Proute' 2006.
Author: Alain Proute'
Created March 2006
Revised March 2006
This file contains the public types and definitions which are common to the several
parts of the Anubis Parser Generator (APG).
This is the type of options for APG.
public type APG_Option:
verbose,
types,
trace,
time,
yacc,
tools,
force,
yards.
public type APG_Symbol_Value:
symbol_value (String name,
String value). // default is "_"
public type APG_Tail_Symbol_Value:
symbol_value (String name,
String value), // default is "_"
command (String text).
public type TailItem: // things that can be in the tail of a grammar rule
symbol_value (String name,
String value),
plus (List(TailItem) items,
String sym,
String value),
star (List(TailItem) items,
String sym,
String value),
maybe (List(TailItem) items,
String sym,
String value),
command (String text). // command within the body: { <term> }
public type APG_Extended_Grammar_Rule:
grammar_rule (Word32 id, // the grammar reader generates numbers
APG_Symbol_Value head,
List(TailItem) body,
Maybe(String) precedence).
public type APG_Grammar_Rule:
grammar_rule (Word32 id, // the grammar reader generates numbers
APG_Symbol_Value head,
List(APG_Tail_Symbol_Value) body,
Maybe(String) precedence).
This type gathers all the sorts of errors which may occur during execution of the
Anubis Parser Generator.
public type APG_Error:
unexpected_end_of_input (Word32 line),
no_parser_name (Word32 line),
keyword_was_expected (Word32 line),
unknown_declaration_keyword (Word32 line, String keyword),
type_description_expected (Word32 line),
tool_description_expected (Word32 line),
incorrect_symbol_value (Word32 line),
symbol_expected (Word32 line),
comma_expected (String sort, Word32 line),
left_par_expected (Word32 line),
arrow_expected (Word32 line),
colon_expected (Word32 line),
cannot_read_after_dollar (Word32 line),
dollar_not_followed_by_paren (Word32 line),
unexpected_end_of_file_in_command (Word32 line),
consecutive_commands (Word32 line),
incorrect_rule_precedence (Word32 line),
misclosed_rule_precedence (Word32 line),
incorrect_end_of_rule (Word32 line),
unexpected_char_after_closing_brace (Word32 line),
bad_end_of_precedence_declaration (Word32 line),
bad_end_of_type_declaration (Word32 line),
bad_end_of_tool_declaration (Word32 line),
bad_end_of_extra_declaration (Word32 line),
symbol_has_no_type (Word32 line, String symbol),
symbol_has_no_type (String symbol),
symbol_should_have_value (Word32 line, String symbol),
tool_not_declared (String tool_name),
not_enough_arguments_in_tool (APG_Grammar_Rule),
too_many_arguments_in_tool (APG_Grammar_Rule),
bad_argument_type (APG_Grammar_Rule,
String arg,
String actual_type,
String declared_type),
bad_target_type (APG_Grammar_Rule,
String head_type,
String target_type),
unreadable_tool (String tool),
no_include_file (Word32 line, String fname),
missing_filename (Word32 line).
The function `en_print` prints in English a message of type `APG_Error`
on the standard output.
public define One
en_print
(
APG_Error e
).
This is the type of (abstract) grammars. An object of this type is produced by the
function `read_APG_grammar` (see `read_grammar.anubis`), and given as an argument to
the function `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque.
public type APG_Grammar:...
public type APG_Extended_Grammar:...
read tools/streams.anubis
This type represents states in the abstract automaton as it is constructed by the
function `make_APG_automaton` (see `make_automaton.anubis`). The automaton itself is
just a list of such states. This is an opaque type.
public type APG_State:...
read tools/basis.anubis
public type APG_Precedence_Dec:
left (Word32 level, List(String) symbol_names),
right (Word32 level, List(String) symbol_names),
non_assoc (Word32 level, List(String) symbol_names).
public type APG_Type_Dec:
type_dec (String type,
List(String) symbol_names).
public type APG_Tool_Dec:
tool_dec (String target_type,
List(String) source_types,
List(String) symbol_names).
public type Extra:
extra(String type, String name).
public type APG_Extended_Grammar:
grammar (String preambule,
String parser_name,
List(APG_Precedence_Dec) precedence_declarations,
List(APG_Type_Dec) type_declarations,
List(APG_Extended_Grammar_Rule) grammar_rules,
Maybe(Extra) extra,
String postambule).
public type APG_Grammar:
grammar (String preambule,
String parser_name,
List(APG_Precedence_Dec) precedence_declarations,
List(APG_Type_Dec) type_declarations,
List(APG_Grammar_Rule) grammar_rules,
Maybe(Extra) extra,
String postambule).
public type APG_Transition:
transition(String symbol,
Word32 target_id).
public type APG_Propagation:
propagates_to_derived,
dont_propagate_to_derived.
public type APG_Symbol_Act:
symbol (String name), // grammar symbol appearing in the body of a rule
command (String text). // immediate command
public type APG_Scenario:
scenario(Word32 rule_id,
String head,
List(APG_Symbol_Act) before_dot,
List(APG_Symbol_Act) after_dot,
APG_Propagation propagation,
Var(Bool) has_generated,
Var(List(String)) lookaheads_v).
public type APG_Class:
class(String transition,
List(APG_Scenario) scenarios).
public type APG_State:
state(Word32 state_id,
//Maybe(String) preaction, // action $(term) to execute before reading the lookahead
List(APG_Class) classes,
List(APG_Transition) transitions).
public define Bool
less
(
APG_Transition t1,
APG_Transition t2
) =
symbol(t1) < symbol(t2).
Getting all symbol names from a grammar.
define List(APG_Symbol_Act)
names_in
(
TailItem i
) =
if i is
{
symbol_value(name,value) then [symbol(name)],
plus(items,sym,v) then no_doubles(flat(map(names_in,items))),
star(items,sym,v) then no_doubles(flat(map(names_in,items))),
maybe(items,sym,v) then no_doubles(flat(map(names_in,items))),
command(text) then [command(text)]
}.
public define List(APG_Symbol_Act)
drop_leading_command // if any
(
List(APG_Symbol_Act) l
) =
if l is
{
[ ] then [ ],
[h . t] then if h is
{
symbol(_) then l,
command(_) then t
}
}.
public define List(APG_Symbol_Act)
names_in
(
List(APG_Tail_Symbol_Value) l
) =
if l is
{
[ ] then [ ],
[h . t] then if h is
{
symbol_value(n,_) then [symbol(n) . names_in(t)],
command(text) then [command(text) . names_in(t)]
}
}.
public define List(String)
symbols_only
(
List(APG_Tail_Symbol_Value) l
) =
if l is
{
[ ] then [ ],
[h . t] then if h is
{
symbol_value(name,_) then [name . symbols_only(t)],
command(_) then symbols_only(t)
}
}.
public define List(String)
symbols_only
(
List(APG_Symbol_Act) l
) =
if l is
{
[ ] then [ ],
[h . t] then if h is
{
symbol(name) then [name . symbols_only(t)],
command(_) then symbols_only(t)
}
}.
public define List(String)
all_symbols_aux
(
List(APG_Grammar_Rule) rules
) =
if rules is
{
[ ] then [ ],
[rule1 . other_rules] then
if rule1 is grammar_rule(_,head,body,_) then
no_doubles(merge([name(head)],
merge(no_doubles(symbols_only(names_in(body))),all_symbols_aux(other_rules))))
}.
public define List(String)
all_symbols
(
List(APG_Grammar_Rule) rules
) =
qsort(all_symbols_aux(rules),string_less).
Getting all non terminals from a grammar.
public define List(String)
all_non_terminals
(
List(APG_Grammar_Rule) rules
) =
if rules is
{
[ ] then [ ],
[rule1 . other_rules] then
if rule1 is grammar_rule(_,head,_,_) then
merge([name(head)],all_non_terminals(other_rules))
}.
Tools for getting unused symbols.
public define List(String)
tokens_from_type_decs
(
List(APG_Type_Dec) decs
) =
if decs is
{
[ ] then [ ],
[h . t] then if h is type_dec(_,names) then
merge(names,tokens_from_type_decs(t))
}.
public define List(String)
tokens_from_precs
(
List(APG_Precedence_Dec) precs
) =
if precs is
{
[ ] then [ ],
[h . t] then
merge(symbol_names(h),tokens_from_precs(t))
}.
public type APG_Token:
used (String name),
unused (String name).
public define Bool
alphaless
(
APG_Token t1,
APG_Token t2
) =
name(t1) < name(t2).
define String
format
(
TailItem i
) =
if i is
{
symbol_value(name,value) then name+"("+value+")",
plus(items,sym,v) then "{"+concat(map(format,items)," ")+"}+("+sym+","+v+")",
star(items,sym,v) then "{"+concat(map(format,items)," ")+"}*("+sym+","+v+")",
maybe(items,sym,v) then "{"+concat(map(format,items)," ")+"}?("+sym+","+v+")",
command(text) then "$("+text+")"
}.
define String
format
(
APG_Symbol_Value i
) =
if i is
{
symbol_value(name,value) then name+"("+value+")",
}.
define String
format
(
APG_Tail_Symbol_Value i
) =
if i is
{
symbol_value(name,value) then name+"("+value+")",
command(text) then "$("+text+")"
}.
public define One
print
(
APG_Symbol_Act s
) =
if s is
{
symbol(x) then print(x),
command(text) then print("$("+text+")")
}.
define One
print
(
APG_Extended_Grammar_Rule r
) =
if r is grammar_rule(id,head,body,prec) then
print(id+" "+name(head)+"("+value(head)+"): ");
map_forget((TailItem i) |-> print(format(i)+" "), body);
if prec is
{
failure then unique,
success(p) then print(" ["+p+"]")
};
print(".\n").
define One
print
(
APG_Grammar_Rule r
) =
if r is grammar_rule(id,head,body,prec) then
print(id+" "+name(head)+"("+value(head)+"): ");
map_forget((APG_Tail_Symbol_Value i) |-> print(format(i)+" "), body);
if prec is
{
failure then unique,
success(p) then print(" ["+p+"]")
};
print(".\n").
public define One
en_print
(
APG_Error e
) =
if e is
{
unexpected_end_of_input(Word32 line) then
print(to_decimal(line)+": Unexpected end of input.\n"),
no_parser_name(Word32 line) then
print(to_decimal(line)+": No parser name.\n"),
keyword_was_expected(Word32 line) then
print(to_decimal(line)+": Keyword was expected.\n"),
unknown_declaration_keyword(Word32 line,String keyword) then
print(to_decimal(line)+": Unknown declaration keyword '"+keyword+"'.\n"),
type_description_expected(Word32 line) then
print(to_decimal(line)+": Type description expected.\n"),
tool_description_expected(Word32 line) then
print(to_decimal(line)+": Tool function description expected.\n"),
incorrect_symbol_value(Word32 line) then
print(to_decimal(line)+": Incorrect symbol value.\n"),
symbol_expected(Word32 line) then
print(to_decimal(line)+": Symbol expected.\n"),
comma_expected(String sort,Word32 line) then
print(to_decimal(line)+": Comma expected after '{...}"+sort+"(<symbol>'.\n"),
left_par_expected(Word32 line) then
print(to_decimal(line)+": Opening parenthese expected.\n"),
arrow_expected(Word32 line) then
print(to_decimal(line)+": An arrow '->' was expected.\n"),
colon_expected(Word32 line) then
print(to_decimal(line)+": Colon expected.\n"),
cannot_read_after_dollar(Word32 line) then
print(to_decimal(line)+": Cannot read after sign '$'.\n"),
dollar_not_followed_by_paren(Word32 line) then
print(to_decimal(line)+": Sign '$' not followed by '('.\n"),
unexpected_end_of_file_in_command(Word32 line) then
print(to_decimal(line)+": Unexpected end of file within an immediate command.\n"),
consecutive_commands(Word32 line) then
print(to_decimal(line)+": Consecutive immediate commands are forbidden.\n"),
incorrect_rule_precedence(Word32 line) then
print(to_decimal(line)+": Incorrect rule precedence.\n"),
misclosed_rule_precedence(Word32 line) then
print(to_decimal(line)+": Misclosed rule precedence.\n"),
incorrect_end_of_rule(Word32 line) then
print(to_decimal(line)+": Incorrect end of rule.\n"),
unexpected_char_after_closing_brace(Word32 line) then
print(to_decimal(line)+": Unexpected character after closing bracket.\n"),
bad_end_of_precedence_declaration(line) then
print(to_decimal(line)+": Incorrect end of precedence declaration.\n"),
bad_end_of_type_declaration(line) then
print(to_decimal(line)+": Incorrect end of type declaration.\n"),
bad_end_of_tool_declaration(line) then
print(to_decimal(line)+": Incorrect end of tool declaration.\n"),
bad_end_of_extra_declaration(line) then
print(to_decimal(line)+": Incorrect end of extra declaration.\n"),
symbol_has_no_type(line,symbol) then
print(to_decimal(line)+": Symbol '"+symbol+"' has no declared type.\n"),
symbol_has_no_type(symbol) then
print("Symbol '"+symbol+"' has no declared type.\n"),
symbol_should_have_value(line,symbol) then
print(to_decimal(line)+": Symbol '"+symbol+"' should have a value.\n")
tool_not_declared(name) then
print("Type of tool '"+name+"' not declared.\n")
not_enough_arguments_in_tool(r) then
print("Not enough arguments in tool:\n");
print(r),
too_many_arguments_in_tool(r) then
print("Too many arguments in tool:\n");
print(r),
bad_argument_type(r,arg,actual_type,declared_type) then
print("Bad argument type: ");
print(r);
print("Argument: "+arg+"\n");
print("Actual type: "+actual_type+"\n");
print("Declared type: "+declared_type+"\n"),
bad_target_type(r,head_type,target_type) then
print("Bad target type: ");
print(r);
print("Type of head: "+head_type+"\n");
print("Target type: "+target_type+"\n"),
unreadable_tool(s) then
print("Unreadable tool: "+s+"\n"),
no_include_file(l,n) then
print("File '"+n+"' to be included at line "+l+" cannot be found.\n"),
missing_filename(l) then
print("File name not found at line "+l+".\n")
}.
public define One print(Stream s, String t) = forget(write_string(s,t)).
define One
print
(
Stream s,
APG_Precedence_Dec d
) =
if d is
{
left(l,names) then map_forget((String x) |-> print(s," "+l+" left "+x+"\n"),names),
right(l,names) then map_forget((String x) |-> print(s," "+l+" right "+x+"\n"),names),
non_assoc(l,names) then map_forget((String x) |-> print(s," "+l+" non_assoc "+x+"\n"),names)
}.
public define String
right_pad
(
String s,
Int n
) =
with l = length(s),
if l >= n
then s
else s+constant_string(n-l,' ').
public define String
right_pad
(
Word32 s,
Int n
) =
right_pad(to_decimal(s),n).
public define One
print
(
Stream s,
APG_Symbol_Act x
) =
if x is
{
symbol(a) then print(s,a),
command(t) then print(s,"$("+t+")")
}.
define One
print
(
Stream s,
APG_Type_Dec d
) =
if d is type_dec(type,names) then
map_forget((String x) |-> print(s," "+right_pad(type,25)+" "+x+"\n"),names).
define One
print
(
Stream s,
APG_Symbol_Value sv,
Bool pad
) =
if sv is symbol_value(name,value) then
with x = name+"("+value+") ",
print(s,if pad then right_pad(x,25) else x).
define One
print
(
Stream s,
APG_Tail_Symbol_Value sv,
Bool pad
) =
with x = if sv is
{
symbol_value(name,value) then name+"("+value+") ",
command(text) then "$("+text+") "
},
print(s,if pad then right_pad(x,25) else x).
define One
print
(
Stream s,
APG_Grammar_Rule r
) =
if r is grammar_rule(id,head,body,prec) then
print(s,right_pad(" ("+id+") ",3));
print(s,head,true);
print(s,": ");
map_forget((APG_Tail_Symbol_Value sv) |-> print(s,sv,false),body);
print(s,".\n").
This function prints a grammar (of type `APG_Grammar`) on the standard output. The
preambule and postambule are not printed.
public define One
print_grammar
(
Stream s,
APG_Grammar g
).
public define One
print_grammar
(
Stream s,
APG_Extended_Grammar g
).
public define One
print_grammar
(
Stream s,
APG_Grammar g
) =
with prints = (String x) |-> print(s,x),
if g is grammar(preambule,
parser_name,
precedence_declarations,
type_declarations,
grammar_rules,
mb_extra,
postambule) then
prints("\n Parser name: "+parser_name+"\n");
prints("\n Precedence levels and association modes:\n");
map_forget((APG_Precedence_Dec d) |-> print(s,d),precedence_declarations);
prints("\n Types of all symbols:\n");
map_forget((APG_Type_Dec d) |-> print(s,d),type_declarations);
// all other symbols have type One by default:
print(s,type_dec("One",
all_symbols(grammar_rules) - flat(map(symbol_names,type_declarations))));
if mb_extra is
{
failure then prints("\n No Extra data.\n"),
success(e) then if e is extra(t,n) then
prints("\n Extra data: ("+t+") "+n+"\n")
};
prints("\n Grammar rules:\n");
map_forget((APG_Grammar_Rule r) |-> print(s,r),grammar_rules);
prints("\n").
public define One
print_grammar
(
Stream s,
APG_Extended_Grammar g
) =
with prints = (String x) |-> print(s,x),
if g is grammar(preambule,
parser_name,
precedence_declarations,
type_declarations,
grammar_rules,
mb_extra,
postambule) then
prints("\nParser name: "+parser_name+"\n");
prints("\nPrecedence levels and association modes:\n");
map_forget(prints,precedence_declarations);
prints("\nType declarations:\n");
map_forget(prints,type_declarations);
if mb_extra is
{
failure then prints("\nNo Extra data.\n"),
success(e) then if e is extra(t,n) then
prints("\nExtra data: ("+t+") "+n+"\n")
};
prints("\nGrammar rules:\n");
map_forget(prints,grammar_rules);
prints("\n").
public define One
print_grammar
(
APG_Extended_Grammar g
) = print_grammar(make_stream(stdout),g).
public define One
print_grammar
(
APG_Grammar g
) = print_grammar(make_stream(stdout),g).
define Maybe(List($T))
extract
(
$T x,
List($T) l
) =
if l is
{
[ ] then failure,
[h . t] then
if h = x
then success(t)
else if extract(x,t) is
{
failure then failure,
success(s) then success([h . s])
}
}.
public define Bool
same_set
(
List($T) l1,
List($T) l2
) =
if l1 is
{
[ ] then l2 = [ ],
[h1 . t1] then
if extract(h1,l2) is
{
failure then false,
success(t2) then same_set(t1,t2)
}
}.
read system/string.anubis
public define $T
show_time
(
String msg,
One -> $T term
) =
with start_time = (UTime)unow,
result = term(unique),
if unow - start_time is utime(s,m) then
print(msg+s+","+zero_pad_n(6,m)+" seconds\n");
result.
public define String
show_rule
(
APG_Extended_Grammar_Rule h
)=
if h is grammar_rule(id,head,body,prec) then
"("+to_decimal(id)+") "+name(head)+": "+
concat(map(format,body)," ") + "\n".