birnam_translate.pl
4.77 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
%
% Predykat translaterule/3 tłumaczący pojedynczą regułę gramatyczną na
% klauzulę prologową.
%
%
% Copyright (C) 1997-2007 Marcin Woliński
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% 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 General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA
%
% In addition, as a special exception, the copyright holder gives
% permission to link the code of this program with the Morfeusz library
% (see http://www.nlp.ipipan.waw.pl/~wolinski/morfeusz), and distribute
% linked combinations including the two. You must obey the GNU General
% Public License in all respects for all of the code used other than
% Morfeusz. If you modify this file, you may extend this exception to
% your version of the file, but you are not obligated to do so. If you
% do not wish to do so, delete this exception statement from your
% version.
translaterule( LHSin, RHSin, LHSout, RHSout, First ) :-
conjtolist( RHSin, RHSlist, [] ),
firstrhs( RHSlist, First, NrR, RHSlist2 ),
addargs( First, Wpocz, W0, FId, LHSout ),
addargs( LHSin, Wpocz, Wn1, TrId, Head ),
transrhs( RHSlist2, PP, W0, Wn1, RHSoutlist,
[ register(LHSin, Wpocz, Wn1, NrR, [FId/First | PP], TrId),
Head] ),
% listtoconj( [first(LHSin,Goal)|RHSoutlist], RHSout ).
listtoconj( RHSoutlist, RHSout ).
% conjtolist/3:
% Poniższy predykat obchodzi drzewo przecinkowe w głąb i generuje
% listę jego liści.
conjtolist( X, [X|LL], LL ) :- var(X), !.
conjtolist( (X,Y), L, LL ) :- !,
conjtolist( X, L, L1 ),
conjtolist( Y, L1, LL ).
conjtolist( X, [X|LL], LL ).
% listtoconj/3:
% zamiana listy na term przecinkowy.
% listtoconj( [X], X ).
% listtoconj( [X|XX], (X,Y) ) :- listtoconj( XX, Y ).
% addargs/8:
% dodaje argumenty administracyjne do nieterminala NT
addargs(NT, In, Out, TrId, NTArg) :-
NT =.. [Head | Args],
NTArg =.. [Head, In, Out, TrId | Args].
% firstrhs:
% ujmuje z prawej strony reguły pierwszy nieterminal i ewentualnie numer
% reguły.
% !!! metawywołania!
firstrhs( [s(NrR) | R], First, NrR, RR) :- !,
firstrhs( R, First, NrR, RR).
firstrhs( [{X} | R], F, NrR, RR ) :- !,
format(user_error,
"~NCannot start rule with a condition {~p}. Ignoring.~n", [X]),
firstrhs( R, F, NrR, RR ).
firstrhs( [[T] | RR], terminal(T), NrR, RR ) :- !,
(nonvar(NrR) ; NrR='UNKNOWN').
firstrhs( [First | RR], First, NrR, RR) :-
nonvar(NrR) ; NrR='UNKNOWN'.
firstrhs( [], _, _, _ ) :-
format(user_error, '~NDetected epsilon rule !~n', []), nl,
fail.
% transrhs:
% tłumaczenie prawej strony reguły.
transrhs( [s(NrR) | RR], PP, W0, Wn, O, OO ) :- !,
format(user_error, "~NMisplaced rule number (~p). Ignoring.~n", [NrR]),
transrhs( RR, PP, W0, Wn, O, OO).
%transrhs( [przec | RR], [quasicomma|PP], W0, Wn, [comma(W0) | O], OO ) :-
% transrhs( RR, PP, W0, Wn, O, OO ).
transrhs( [{X} | RR], PP, W0, Wn, O, OOO ) :- !,
conjtolist( X, O, OO ),
transrhs( RR, PP, W0, Wn, OO, OOO ).
transrhs( [[X] | RR], [W0,TrId/terminal(X)|PP], W0, Wn,
[goal(terminal(X), W0, W1, TrId) | O], OO) :- !,
transrhs( RR, PP, W1, Wn, O, OO ).
transrhs( [wymagania( IW, W, OW, FF, FW ) | RR ], P, W0, Wn,
[wymagania( P, PP, W0, W1, IW, W, OW, FF, FW ) | O], OO) :- !,
transrhs( RR, PP, W1, Wn, O, OO ).
transrhs( [wymagane( W, OW, FW ) | RR ], P, W0, Wn,
[wymagane( P, PP, W0, W1, W, OW, FW ) | O], OO) :- !,
transrhs( RR, PP, W1, Wn, O, OO ).
transrhs( [sequence_of( Elems ) | RR ], P, W0, Wn,
[do_sequence_of( P, PP, W0, W1, ElemsCode ) | O], OO) :- !,
przygotuj_sekwencję(Elems, ElemsCode),
transrhs( RR, PP, W1, Wn, O, OO ).
transrhs( [optional( NT, TC, FC ) | RR ], P, W0, Wn,
[optional( P, PP, W0, W1, NT, TC, FC ) | O], OO) :- !,
transrhs( RR, PP, W1, Wn, O, OO ).
transrhs( [ NT | RR], [W0,TrId/NT|PP], W0, Wn,
[goal(NT, W0, W1, TrId) | O], OO ) :-
transrhs( RR, PP, W1, Wn, O, OO).
transrhs( [], [], W, W, O, O ).
save_rulehead( NT ) :-
functor( NT, Name, Arity ),
Arity4 is Arity + 3,
functor( Head, Name, Arity4 ),
(rulehead( Head ), !
;
assertz( rulehead(Head) )).
% gen_rule( Head ) :-
% Head =.. [NT, Goal, P, P, S, S | Args],
% Goal =.. [NT | Args],
% portray_clause( Head ), nl,
% fail.
gen_rule( LHS ) :-
(
rule( LHS, RHS )
*->
portray_clause( (LHS:-RHS) ), nl
;
portray_clause( (LHS:-fail) ), nl
),
fail.
%%% Local Variables:
%%% coding: utf-8
%%% mode: prolog
%%% End: