/usr/lib/swi-prolog/boot/dcg.pl is in swi-prolog-nox 5.10.4-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2009, University of Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module('$dcg',
[ dcg_translate_rule/2,
phrase/2,
phrase/3
]).
/********************************
* GRAMMAR RULES *
*********************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The DCG compiler. The original code was copied from C-Prolog and written
by Fernando Pereira, EDCAAD, Edinburgh, 1984. Since then many people
have modified and extended this code. It's a nice mess now and it should
be redone from scratch. I won't be doing this before I get a complete
spec explaining all an implementor needs to know about DCG. I'm a too
basic user of this facility myself (though I learned some tricks from
people reporting bugs :-)
The original version contained '$t_tidy'/2 to convert ((a,b), c) to
(a,(b,c)), but as the SWI-Prolog compiler doesn't really care (the
resulting code is simply the same), I've removed that.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
dcg_translate_rule(((LP,MNT)-->RP),(H:-B)) :- !,
'$set_source_module'(M, M),
'$extend'(LP, S0, SR, H),
'$t_body'(RP, M:M, S0, S1, B0),
'$t_body'(MNT, M:M, SR, S1, B1),
'$body_optimized'((B0,B1),B2,S0),
'$body_optimized'(B2,B,SR).
dcg_translate_rule((LP-->RP), (H:-B)) :-
'$extend'(LP, S0, S, H),
'$set_source_module'(M, M),
'$t_body'(RP, M:M, S0, S, B0),
'$body_optimized'(B0,B,S0).
'$body_optimized'(B0,B,S0) :-
( B0 = (S00=X, B), % map a(H,T) :- H = [a,b|T], b(T)
S00 == S0
-> S0 = X % into a([a,b|T0]) :- b(T0, T).
; B0 = (S00=X), % map a(H,T) :- H = [a,b|T]
S00 == S0
-> S0 = X, % into a([a,b|T], T)
B = true
; B0 = B
).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
On the DCG Translation of {}
a --> x, {y}.
There are two options. In traditional systems we see:
a(A, B) :- x(A, B), y.
And in modern system we see:
a(A, B) :- x(A, C), y, B=C.
Martin Sondergaard's grammar was breaking down on
=================================================
s --> v, star0, {write('You can not do that')}.
star0 --> [].
star0 --> [_], star0.
meaning to write a message for any sentence starting with a `v',
skipping the remainder. With delayed binding this causes a large number
of messages as star0 only eats one token on backtracing.
You can fix this using remaining as below rather then star0.
remaining --> [_], !, remaining.
remaining --> [].
Without delayed unification of the tail we get the following trouble
====================================================================
(comment from Richard O'Keefe)
Suppose I have
p --> [a], !, {fail}. p --> [].
That is, p//0 is suppose to match the empty string as long as it is not
followed by a. Now consider
p([a], [a])
If the first clause is translated as
p(S0, S) :- S0 = [a|S1], !, fail, S = S1.
then it will work *correctly*, and the call p([a], [a]) will fail as it
is supposed to. If the first clause is translated as
p(S0, S) :- S0 = [a|S], !, fail.
then the call p([a], [a]) will succeed, which is quite definitely wrong.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
'$t_body'(Var, Q, S, SR, phrase(QVar, S, SR)) :-
var(Var), !,
qualify(Q, Var, QVar).
'$t_body'(M:X, _:C, S, SR, Ct) :- !,
'$t_body'(X, M:C, S, SR, Ct).
'$t_body'([], _, S, SR, S=SR) :- !. % inline lists
'$t_body'(List, _, S, SR, C) :-
( List = [_|_]
-> !,
( is_list(List)
-> '$append'(List, SR, OL),
C = (S = OL)
; C = '$append'(List, SR, S) % Deals with [H|T] in body
)
; string(List) % double_quotes = string
-> !,
string_to_list(List, Codes),
'$append'(Codes, SR, OL),
C = (S = OL)
).
'$t_body'(!, _, S, SR, ( !, SR = S) ) :- !.
'$t_body'({}, _, S, S, true) :- !.
'$t_body'({T}, Q, S, SR, (QT, SR = S)) :- !, % (*)
qualify(Q, T, QT).
%'$t_body'({T}, S, S, T) :- !. % (*)
'$t_body'((T, R), Q, S, SR, (Tt, Rt)) :- !,
'$t_body'(T, Q, S, SR1, Tt),
'$t_body'(R, Q, SR1, SR, Rt).
'$t_body'((T;R), Q, S, SR, (Tt;Rt)) :- !,
'$t_body'(T, Q, S, S1, T1), '$t_fill'(S, SR, S1, T1, Tt),
'$t_body'(R, Q, S, S2, R1), '$t_fill'(S, SR, S2, R1, Rt).
'$t_body'((T|R), Q, S, SR, (Tt;Rt)) :- !,
'$t_body'(T, Q, S, S1, T1), '$t_fill'(S, SR, S1, T1, Tt),
'$t_body'(R, Q, S, S2, R1), '$t_fill'(S, SR, S2, R1, Rt).
'$t_body'((C->T), Q, S, SR, (Ct->Tt)) :- !,
'$t_body'(C, Q, S, SR1, Ct),
'$t_body'(T, Q, SR1, SR, Tt).
'$t_body'((C*->T), Q, S, SR, (Ct*->Tt)) :- !,
'$t_body'(C, Q, S, SR1, Ct),
'$t_body'(T, Q, SR1, SR, Tt).
'$t_body'((\+ C), Q, S, SR, (\+ Ct, SR = S)) :- !,
'$t_body'(C, Q, S, _, Ct).
'$t_body'(T, Q, S, SR, QTt) :-
'$extend'(T, S, SR, Tt),
qualify(Q, Tt, QTt).
'$t_fill'(S, SR, S1, T, (T, SR=S)) :-
S1 == S, !.
'$t_fill'(_S, SR, SR, T, T).
qualify(M:C, X0, X) :-
M == C, !,
X = X0.
qualify(M:_, X, M:X).
% '$extend'(+Head, +Extra1, +Extra2, -NewHead)
%
% Extend Head with two more arguments (on behalf DCG compilation).
% The solution below is one option. Using =.. and append is the
% alternative. In the current version (5.3.2), the =.. is actually
% slightly faster, but it creates less garbage.
:- dynamic '$extend_cache'/4.
:- volatile '$extend_cache'/4.
'$dcg_reserved'([]).
'$dcg_reserved'([_|_]).
'$dcg_reserved'({_}).
'$dcg_reserved'({}).
'$dcg_reserved'(!).
'$dcg_reserved'((\+_)).
'$dcg_reserved'((_,_)).
'$dcg_reserved'((_;_)).
'$dcg_reserved'((_|_)).
'$dcg_reserved'((_->_)).
'$dcg_reserved'((_*->_)).
'$dcg_reserved'((_-->_)).
'$extend'(V, _, _, _) :-
var(V), !,
throw(error(instantiation_error,_)).
'$extend'(M:OldT, A1, A2, M:NewT) :- !,
'$extend'(OldT, A1, A2, NewT).
'$extend'(OldT, A1, A2, NewT) :-
'$extend_cache'(OldT, A1, A2, NewT), !.
'$extend'(OldT, A1, A2, NewT) :-
( callable(OldT) -> true ; throw(error(type_error(callable,OldT),_)) ),
( '$dcg_reserved'(OldT) -> throw(error(permission_error(define,dcg_nonterminal,OldT),_)) ; true ),
functor(OldT, Name, Arity),
functor(CopT, Name, Arity),
NewArity is Arity+2,
functor(NewT, Name, NewArity),
'$copy_args'(1, Arity, CopT, NewT),
A1Pos is Arity+1,
A2Pos is Arity+2,
arg(A1Pos, NewT, A1C),
arg(A2Pos, NewT, A2C),
assert('$extend_cache'(CopT, A1C, A2C, NewT)),
OldT = CopT,
A1C = A1,
A2C = A2.
'$copy_args'(I, Arity, Old, New) :-
I =< Arity, !,
arg(I, Old, A),
arg(I, New, A),
I2 is I + 1,
'$copy_args'(I2, Arity, Old, New).
'$copy_args'(_, _, _, _).
%% phrase(:RuleSet, ?List).
%% phrase(:RuleSet, ?List, ?Rest).
%
% Interface to DCGs
:- meta_predicate
phrase(2, ?),
phrase(2, ?, ?).
:- noprofile((phrase/2,
phrase/3)).
phrase(RuleSet, Input) :-
phrase(RuleSet, Input, []).
phrase(RuleSet, Input, Rest) :-
strip_module(RuleSet, M, Plain),
( var(Plain) -> throw(error(instantiation_error,_)) ; true ),
'$t_body'(Plain, M:M, S0, S, Body),
Input = S0, Rest = S,
M:Body.
|