-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.pl
151 lines (132 loc) · 2.65 KB
/
parser.pl
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
:- module(parser,
[program/3,
problog_prob/3]).
:- use_module(library(dcg/basics)).
:- use_module(tokenize).
program(X) -->
clauses(X).
clauses(C) -->
[cntrl("\n")],
clauses(C).
clauses([H|T]) -->
clause(H),
clauses(T).
clauses([]) --> [].
clause(visible([(Pred,N)|R])) -->
[punct("%")],
[word("visible")],
[punct(":")],
[word(PredS)],
[punct("/")],
[word(NS)],
{atom_string(Pred,PredS),term_string(N,NS)},
pred_ast(R),
[cntrl("\n")].
clause(unsafe([(Pred,N)|R])) -->
[punct("%")],
[word("unsafe")],
[punct(":")],
[word(PredS)],
[punct("/")],
[word(NS)],
{atom_string(Pred,PredS),term_string(N,NS)},
pred_ast(R),
[cntrl("\n")].
clause(comment(C)) -->
[punct("%")],
comment_ast(C),
[cntrl("\n")].
clause(comment(C)) -->
[punct("%")],
comment_ast(C).
clause(query(Atom)) -->
[word("query")],
[punct("(")],
atom_ast(Atom),
[punct(")")],
[punct(".")].
clause(cl(Prob,Atom,[])) -->
[word(N1)],
[punct(".")],
[word(N2)],
[punct(":")],
[punct(":")],
{string_concat(N1,".",Temp),string_concat(Temp,N2,N),term_string(Prob,N)},
atom_ast(Atom),
[punct(".")].
clause(cl(1,Atom,[])) -->
atom_ast(Atom),
[punct(".")].
clause(cl(Prob,H,[B|T])) -->
[word(N1)],
[punct(".")],
[word(N2)],
[punct(":")],
[punct(":")],
{string_concat(N1,".",Temp),string_concat(Temp,N2,N),term_string(Prob,N)},
atom_ast(H),
[punct(":")],
[punct("-")],
atom_ast(B),
body_ast(T),
[punct(".")].
clause(cl(1,H,[B|T])) -->
atom_ast(H),
[punct(":")],
[punct("-")],
atom_ast(B),
body_ast(T),
[punct(".")].
body_ast([]) --> [].
body_ast([Atom|T]) -->
[punct(",")],
atom_ast(Atom),
body_ast(T).
atom_ast(Atom) -->
[word(X)],
{atom_codes(Atom,X)}.
atom_ast(Atom) -->
[word(P)],
[punct("(")],
args_ast(Args),
[punct(")")],
{atom_codes(Pred,P),Atom=..[Pred|Args]}.
%args_ast([]) --> [].
args_ast([Arg]) -->
[word(H)],
{word_to_term(H,Arg)}.
args_ast([Arg|T]) -->
[word(H)],
{word_to_term(H,Arg)},
[punct(",")],
args_ast(T).
comment_ast([]) --> [].
comment_ast([H|T]) -->
[word(H)],
comment_ast(T).
comment_ast([H|T]) -->
[punct(H)],
comment_ast(T).
pred_ast([]) --> [].
pred_ast([(Pred,N)|R]) -->
[punct(",")],
[word(PredS)],
[punct("/")],
[word(NS)],
{atom_string(Pred,PredS),term_string(N,NS)},
pred_ast(R).
word_to_term(H,var(H)) :-
term_string(Term,H),
var(Term).
word_to_term(H,Term) :-
term_string(Term,H),
nonvar(Term).
problog_prob(prob(Prob)) -->
atom_ast(_),
[punct(":")],
[cntrl("\t")],
[word(N1)],
[punct(".")],
[word(N2)],
{string_concat(N1,".",Temp),string_concat(Temp,N2,N),term_string(Prob,N)},
[cntrl("\n")].