@matdata/yasqe 4.6.1 → 4.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,37 +1,37 @@
1
- :-dynamic fo/2.
2
- :-dynamic fi/2.
3
- :-dynamic cf/2.
4
- :-dynamic change/0.
5
- :-dynamic m/3.
6
- :-dynamic tm/1.
7
- :-dynamic '==>'/2.
8
- :-dynamic '=>'/2.
9
- :-op(550,xfy,===>). % Rewrite rules on EBNF expressions
10
- :-op(500,xfy,==>). % EBNF grammar productions
11
- :-op(500,xfy,=>). % BNF grammar productions
12
- :-op(490,xfy,or).
13
- :-op(480,fy,*).
14
- :-op(470,xfy,\).
15
-
16
-
17
- :-reconsult('rewrite.pl').
18
- :-reconsult('ll1.pl').
19
- :-reconsult('prune').
20
- :-reconsult('output_to_javascript.pl').
21
-
22
- go:-
23
- ebnf_to_bnf,
24
- prune_for_top_symbol,
25
- ll1_tables,
26
- ll1_check,
27
- output_file(Out),
28
- write('Writing to '),write(Out),nl,
29
- tell(Out),
30
- output_table_js,
31
- output_keywords_js,
32
- output_punct_js,
33
- js_vars(Vars),
34
- output_vars_js(Vars),
35
- write('}'),
36
- told.
37
-
1
+ :-dynamic fo/2.
2
+ :-dynamic fi/2.
3
+ :-dynamic cf/2.
4
+ :-dynamic change/0.
5
+ :-dynamic m/3.
6
+ :-dynamic tm/1.
7
+ :-dynamic '==>'/2.
8
+ :-dynamic '=>'/2.
9
+ :-op(550,xfy,===>). % Rewrite rules on EBNF expressions
10
+ :-op(500,xfy,==>). % EBNF grammar productions
11
+ :-op(500,xfy,=>). % BNF grammar productions
12
+ :-op(490,xfy,or).
13
+ :-op(480,fy,*).
14
+ :-op(470,xfy,\).
15
+
16
+
17
+ :-reconsult('rewrite.pl').
18
+ :-reconsult('ll1.pl').
19
+ :-reconsult('prune').
20
+ :-reconsult('output_to_javascript.pl').
21
+
22
+ go:-
23
+ ebnf_to_bnf,
24
+ prune_for_top_symbol,
25
+ ll1_tables,
26
+ ll1_check,
27
+ output_file(Out),
28
+ write('Writing to '),write(Out),nl,
29
+ tell(Out),
30
+ output_table_js,
31
+ output_keywords_js,
32
+ output_punct_js,
33
+ js_vars(Vars),
34
+ output_vars_js(Vars),
35
+ write('}'),
36
+ told.
37
+
@@ -1,11 +1,11 @@
1
-
2
- top_symbol(sparql11).
3
- output_file('_tokenizer-table.js').
4
-
5
- js_vars([
6
- startSymbol='"sparql11"',
7
- acceptEmpty=true
8
- ]).
9
-
10
- :-reconsult(gen_ll1).
11
- :-reconsult('../sparql11-grammar.pl').
1
+
2
+ top_symbol(sparql11).
3
+ output_file('_tokenizer-table.js').
4
+
5
+ js_vars([
6
+ startSymbol='"sparql11"',
7
+ acceptEmpty=true
8
+ ]).
9
+
10
+ :-reconsult(gen_ll1).
11
+ :-reconsult('../sparql11-grammar.pl').
@@ -1,175 +1,175 @@
1
-
2
- /*
3
-
4
- Compute LL1 parse tables.
5
-
6
- fi(NT,T) means that T can be the first terminal in NT. The special
7
- token epsilon means that NT may rewrite to nothing
8
- (NT is "nullable").
9
-
10
- fo(NT,T) means that nonterminal NT can be followed by terminal T.
11
- - the final argument is for debugging only, and records
12
- the nonterminal from which the follower T was copied.
13
-
14
- stephen.cresswell@tso.co.uk
15
-
16
- */
17
-
18
-
19
- ll1_tables:-
20
- retractall(cf(_,_)),
21
- retractall(nt(_)),
22
- retractall(fi(_,_)),
23
- retractall(fo(_,_)),
24
- retractall(m(_,_,_)),
25
-
26
- validate_rules,
27
- remember1(change),
28
- iterate_first,
29
- remember1(change),
30
- iterate_follow,
31
- iterate_matrix.
32
-
33
- validate_rules:-
34
- NT=>_,
35
- \+ (_=>RHS, memberchk(NT,RHS) ),
36
- format("Warning: unused non-terminal: ~w~n",[NT]),
37
- fail.
38
- validate_rules:-
39
- % (Check the untranslated rules)
40
- LHS ==> RHS,
41
- \+RHS=[],
42
- \+RHS=[_|_],
43
- format("Warning: atomic RHS: ~w~n",[LHS=>RHS]),
44
- fail.
45
- validate_rules:-
46
- _ => RHS,
47
- member(T,RHS),
48
- \+ T => _,
49
- remember(tm(T)),
50
- fail.
51
- validate_rules:-
52
- tm(T),
53
- \+declared_terminal(T),
54
- format("Warning: undeclared terminal: ~w~n",[T]),
55
- fail.
56
- validate_rules:-
57
- declared_terminal(T),
58
- \+tm(T),
59
- format("Declared terminal not found in grammar: ~w~n",[T]),
60
- fail.
61
- validate_rules.
62
-
63
- declared_terminal(T):-
64
- tm_keywords(Ts),
65
- member(T,Ts).
66
- declared_terminal(T):-
67
- tm_regex(Ts),
68
- member(T,Ts).
69
- declared_terminal(T):-
70
- tm_punct(Ts),
71
- member(T=_,Ts).
72
-
73
- % Repeated until no change
74
- iterate_first:-
75
- change,
76
- !,
77
- retractall(change),
78
- iterate,
79
- iterate_first.
80
- iterate_first.
81
-
82
- % All possibilities
83
- iterate:-
84
- first(A,W),
85
- remember(fi(A,W)),
86
- fail.
87
- iterate.
88
-
89
- assert_terminals:-
90
- tm(Tm),
91
- assertz(fi(Tm,Tm)),
92
- fail.
93
- assert_terminals.
94
-
95
-
96
- first(Tm,Tm):-
97
- tm(Tm).
98
- first(Nonterm,F):-
99
- Nonterm=>RHS,
100
- first_list(RHS,F).
101
-
102
- first_list([],epsilon).
103
- first_list([W|RHS],F):-
104
- fi(W,epsilon),
105
- first_list(RHS,F).
106
- first_list([W|_],F):-
107
- fi(W,F),
108
- F \== epsilon.
109
-
110
-
111
- iterate_follow:-
112
- change,
113
- !,
114
- retractall(change),
115
- iterate_f,
116
- iterate_follow.
117
- iterate_follow.
118
-
119
- iterate_f:-
120
- follow,
121
- fail.
122
- iterate_f.
123
-
124
-
125
- follow:-
126
- B => RHS,
127
- follow_list(RHS,B).
128
-
129
- follow_list([X],B):-
130
- !,
131
- copy_follow(B,X).
132
- follow_list([A|RHS],B):-
133
- tm(A),!,
134
- follow_list(RHS,B).
135
- follow_list([A,X2|RHS],B):-
136
- first_list([X2|RHS],F),
137
- (F=epsilon ->
138
- copy_follow(B,A)
139
- ;
140
- remember(fo(A,F))
141
- ),
142
- follow_list([X2|RHS],B).
143
-
144
- % Whatever can follow B, can follow A
145
- % (applied when A can be final constituent of B)
146
- copy_follow(B,A):-
147
- \+tm(A),
148
- remember1(cf(B,A)),
149
- fo(B,Fo),
150
- remember(fo(A,Fo)),
151
- fail.
152
- copy_follow(_,_).
153
-
154
- iterate_matrix:-
155
- A => RHS,
156
- first_list(RHS,F),
157
- F \== epsilon,
158
- remember1(m(A,F,A=>RHS)),
159
- fail.
160
- iterate_matrix:-
161
- A => RHS,
162
- first_list(RHS,epsilon),
163
- fo(A,F),
164
- remember1(m(A,F,A=>RHS)),
165
- fail.
166
- iterate_matrix.
167
-
168
- ll1_check:-
169
- m(NT,T,R1),
170
- m(NT,T,R2),
171
- R1@<R2,
172
- format('LL(1) clash - ~w, ~w,~n R1=~w~n R2=~w~n',[NT,T,R1,R2]),
173
- fail.
174
- ll1_check:-
175
- write('LL(1) check complete.'),nl.
1
+
2
+ /*
3
+
4
+ Compute LL1 parse tables.
5
+
6
+ fi(NT,T) means that T can be the first terminal in NT. The special
7
+ token epsilon means that NT may rewrite to nothing
8
+ (NT is "nullable").
9
+
10
+ fo(NT,T) means that nonterminal NT can be followed by terminal T.
11
+ - the final argument is for debugging only, and records
12
+ the nonterminal from which the follower T was copied.
13
+
14
+ stephen.cresswell@tso.co.uk
15
+
16
+ */
17
+
18
+
19
+ ll1_tables:-
20
+ retractall(cf(_,_)),
21
+ retractall(nt(_)),
22
+ retractall(fi(_,_)),
23
+ retractall(fo(_,_)),
24
+ retractall(m(_,_,_)),
25
+
26
+ validate_rules,
27
+ remember1(change),
28
+ iterate_first,
29
+ remember1(change),
30
+ iterate_follow,
31
+ iterate_matrix.
32
+
33
+ validate_rules:-
34
+ NT=>_,
35
+ \+ (_=>RHS, memberchk(NT,RHS) ),
36
+ format("Warning: unused non-terminal: ~w~n",[NT]),
37
+ fail.
38
+ validate_rules:-
39
+ % (Check the untranslated rules)
40
+ LHS ==> RHS,
41
+ \+RHS=[],
42
+ \+RHS=[_|_],
43
+ format("Warning: atomic RHS: ~w~n",[LHS=>RHS]),
44
+ fail.
45
+ validate_rules:-
46
+ _ => RHS,
47
+ member(T,RHS),
48
+ \+ T => _,
49
+ remember(tm(T)),
50
+ fail.
51
+ validate_rules:-
52
+ tm(T),
53
+ \+declared_terminal(T),
54
+ format("Warning: undeclared terminal: ~w~n",[T]),
55
+ fail.
56
+ validate_rules:-
57
+ declared_terminal(T),
58
+ \+tm(T),
59
+ format("Declared terminal not found in grammar: ~w~n",[T]),
60
+ fail.
61
+ validate_rules.
62
+
63
+ declared_terminal(T):-
64
+ tm_keywords(Ts),
65
+ member(T,Ts).
66
+ declared_terminal(T):-
67
+ tm_regex(Ts),
68
+ member(T,Ts).
69
+ declared_terminal(T):-
70
+ tm_punct(Ts),
71
+ member(T=_,Ts).
72
+
73
+ % Repeated until no change
74
+ iterate_first:-
75
+ change,
76
+ !,
77
+ retractall(change),
78
+ iterate,
79
+ iterate_first.
80
+ iterate_first.
81
+
82
+ % All possibilities
83
+ iterate:-
84
+ first(A,W),
85
+ remember(fi(A,W)),
86
+ fail.
87
+ iterate.
88
+
89
+ assert_terminals:-
90
+ tm(Tm),
91
+ assertz(fi(Tm,Tm)),
92
+ fail.
93
+ assert_terminals.
94
+
95
+
96
+ first(Tm,Tm):-
97
+ tm(Tm).
98
+ first(Nonterm,F):-
99
+ Nonterm=>RHS,
100
+ first_list(RHS,F).
101
+
102
+ first_list([],epsilon).
103
+ first_list([W|RHS],F):-
104
+ fi(W,epsilon),
105
+ first_list(RHS,F).
106
+ first_list([W|_],F):-
107
+ fi(W,F),
108
+ F \== epsilon.
109
+
110
+
111
+ iterate_follow:-
112
+ change,
113
+ !,
114
+ retractall(change),
115
+ iterate_f,
116
+ iterate_follow.
117
+ iterate_follow.
118
+
119
+ iterate_f:-
120
+ follow,
121
+ fail.
122
+ iterate_f.
123
+
124
+
125
+ follow:-
126
+ B => RHS,
127
+ follow_list(RHS,B).
128
+
129
+ follow_list([X],B):-
130
+ !,
131
+ copy_follow(B,X).
132
+ follow_list([A|RHS],B):-
133
+ tm(A),!,
134
+ follow_list(RHS,B).
135
+ follow_list([A,X2|RHS],B):-
136
+ first_list([X2|RHS],F),
137
+ (F=epsilon ->
138
+ copy_follow(B,A)
139
+ ;
140
+ remember(fo(A,F))
141
+ ),
142
+ follow_list([X2|RHS],B).
143
+
144
+ % Whatever can follow B, can follow A
145
+ % (applied when A can be final constituent of B)
146
+ copy_follow(B,A):-
147
+ \+tm(A),
148
+ remember1(cf(B,A)),
149
+ fo(B,Fo),
150
+ remember(fo(A,Fo)),
151
+ fail.
152
+ copy_follow(_,_).
153
+
154
+ iterate_matrix:-
155
+ A => RHS,
156
+ first_list(RHS,F),
157
+ F \== epsilon,
158
+ remember1(m(A,F,A=>RHS)),
159
+ fail.
160
+ iterate_matrix:-
161
+ A => RHS,
162
+ first_list(RHS,epsilon),
163
+ fo(A,F),
164
+ remember1(m(A,F,A=>RHS)),
165
+ fail.
166
+ iterate_matrix.
167
+
168
+ ll1_check:-
169
+ m(NT,T,R1),
170
+ m(NT,T,R2),
171
+ R1@<R2,
172
+ format('LL(1) clash - ~w, ~w,~n R1=~w~n R2=~w~n',[NT,T,R1,R2]),
173
+ fail.
174
+ ll1_check:-
175
+ write('LL(1) check complete.'),nl.
@@ -1,75 +1,75 @@
1
- /* Convert computed LL(1) table into javascript source
2
- */
3
-
4
- output_table_js:-
5
- write('module.exports = {table:'),nl,
6
- setof(LHS, RHS^(LHS=>RHS), NTs),
7
- form_table(NTs,'{'),
8
- nl,write('},').
9
-
10
- output_terminals_js:-
11
- nl,nl,write('var terminal=['),nl,
12
- tm_regex(Ks),
13
- member(TM,Ks),
14
- format(' { name: "~w", regex:new RegExp("^"+~w) }, ~n',[TM,TM]),
15
- fail.
16
- output_terminals_js:-
17
- write('];'),nl,nl.
18
- output_keywords_js:-
19
- tm_keywords(Ps),
20
- findall(Reg,member(Reg,Ps),Regs),
21
- nl,nl,write('keywords:/^('),
22
- output_as_regex_disj(Regs,''),
23
- write(')/i ,'),nl.
24
- output_punct_js:-
25
- tm_punct(Ps),
26
- findall(Reg,member(_=Reg,Ps),Regs),
27
- nl,write('punct:/^('),
28
- output_as_regex_disj(Regs,''),
29
- write(')/ ,'),nl,nl.
30
- output_top_symbol_js:-
31
- start_symbol(TS),
32
- format('startSymbol:"~w";~n',[TS]).
33
- output_default_query_type_js:-
34
- default_query_type(QT),
35
- format('defaultQueryType:~w;~n',[QT]).
36
- output_lex_version_js:-
37
- lex_version(LV),
38
- format('lexVersion:"~w";~n',[LV]).
39
- output_accept_empty_js:-
40
- accept_empty(AE),
41
- format('acceptEmpty:"~w";~n',[AE]).
42
-
43
- output_vars_js([]).
44
- output_vars_js([Var=Val|Pairs]):-
45
- format('~w:~w,~n',[Var,Val]),
46
- output_vars_js(Pairs).
47
-
48
- output_as_regex_disj([],_).
49
- output_as_regex_disj([T|Ts],Prefix):-
50
- format('~w~w',[Prefix,T]),
51
- output_as_regex_disj(Ts,'|').
52
-
53
- form_table([],_).
54
- form_table([NT|NTs],Punc):-
55
- format('~w~n "~w" : ',[Punc,NT]),
56
- findall(First-RHS,m(NT,First,_=>RHS),Pairs),
57
- output_pairs(Pairs,'{'),
58
- write('}'),
59
- form_table(NTs,', ').
60
-
61
- output_pairs([],_).
62
- output_pairs([First-RHS|Pairs],Punc):-
63
- format('~w~n "~w": ',[Punc,First]),
64
- write_list_strings(RHS),
65
- output_pairs(Pairs, ', ').
66
-
67
- write_list_strings(Xs):-
68
- write('['),
69
- write_list_strings1(Xs,''),
70
- write(']').
71
-
72
- write_list_strings1([],_).
73
- write_list_strings1([X|Xs],Punc):-
74
- format('~w"~w"',[Punc,X]),
75
- write_list_strings1(Xs,',').
1
+ /* Convert computed LL(1) table into javascript source
2
+ */
3
+
4
+ output_table_js:-
5
+ write('module.exports = {table:'),nl,
6
+ setof(LHS, RHS^(LHS=>RHS), NTs),
7
+ form_table(NTs,'{'),
8
+ nl,write('},').
9
+
10
+ output_terminals_js:-
11
+ nl,nl,write('var terminal=['),nl,
12
+ tm_regex(Ks),
13
+ member(TM,Ks),
14
+ format(' { name: "~w", regex:new RegExp("^"+~w) }, ~n',[TM,TM]),
15
+ fail.
16
+ output_terminals_js:-
17
+ write('];'),nl,nl.
18
+ output_keywords_js:-
19
+ tm_keywords(Ps),
20
+ findall(Reg,member(Reg,Ps),Regs),
21
+ nl,nl,write('keywords:/^('),
22
+ output_as_regex_disj(Regs,''),
23
+ write(')/i ,'),nl.
24
+ output_punct_js:-
25
+ tm_punct(Ps),
26
+ findall(Reg,member(_=Reg,Ps),Regs),
27
+ nl,write('punct:/^('),
28
+ output_as_regex_disj(Regs,''),
29
+ write(')/ ,'),nl,nl.
30
+ output_top_symbol_js:-
31
+ start_symbol(TS),
32
+ format('startSymbol:"~w";~n',[TS]).
33
+ output_default_query_type_js:-
34
+ default_query_type(QT),
35
+ format('defaultQueryType:~w;~n',[QT]).
36
+ output_lex_version_js:-
37
+ lex_version(LV),
38
+ format('lexVersion:"~w";~n',[LV]).
39
+ output_accept_empty_js:-
40
+ accept_empty(AE),
41
+ format('acceptEmpty:"~w";~n',[AE]).
42
+
43
+ output_vars_js([]).
44
+ output_vars_js([Var=Val|Pairs]):-
45
+ format('~w:~w,~n',[Var,Val]),
46
+ output_vars_js(Pairs).
47
+
48
+ output_as_regex_disj([],_).
49
+ output_as_regex_disj([T|Ts],Prefix):-
50
+ format('~w~w',[Prefix,T]),
51
+ output_as_regex_disj(Ts,'|').
52
+
53
+ form_table([],_).
54
+ form_table([NT|NTs],Punc):-
55
+ format('~w~n "~w" : ',[Punc,NT]),
56
+ findall(First-RHS,m(NT,First,_=>RHS),Pairs),
57
+ output_pairs(Pairs,'{'),
58
+ write('}'),
59
+ form_table(NTs,', ').
60
+
61
+ output_pairs([],_).
62
+ output_pairs([First-RHS|Pairs],Punc):-
63
+ format('~w~n "~w": ',[Punc,First]),
64
+ write_list_strings(RHS),
65
+ output_pairs(Pairs, ', ').
66
+
67
+ write_list_strings(Xs):-
68
+ write('['),
69
+ write_list_strings1(Xs,''),
70
+ write(']').
71
+
72
+ write_list_strings1([],_).
73
+ write_list_strings1([X|Xs],Punc):-
74
+ format('~w"~w"',[Punc,X]),
75
+ write_list_strings1(Xs,',').