1
/**************************************************************/
2
/* ********************************************************** */
6
/* ********************************************************** */
7
/**************************************************************/
10
/**************************************************************/
11
/* * This program is free software; you can redistribute * */
12
/* * it and/or modify it under the terms of the FreeBSD * */
15
/* * This program is distributed in the hope that it will * */
16
/* * be useful, but WITHOUT ANY WARRANTY; without even * */
17
/* * the implied warranty of MERCHANTABILITY or FITNESS * */
18
/* * FOR A PARTICULAR PURPOSE. See the LICENCE file * */
19
/* * for more details. * */
21
/**************************************************************/
32
#define TPTP2DFG__VERSION "0.1"
34
/*-----------------------------------------------------------------------------*/
36
static void tptp_FPrintDFGProblem(FILE*, DFGDESCRIPTION, LIST, LIST, LIST, LIST, LIST, BOOL);
38
/*-----------------------------------------------------------------------------*/
40
static void tptp_DeleteStrListOfStrPair(POINTER pair) {
41
string_StringFree((char *)list_PairFirst(pair));
42
list_DeleteWithElement(list_PairSecond(pair),(void (*)(POINTER))string_StringFree);
46
/*-----------------------------------------------------------------------------*/
48
int main(int argc, const char* argv[]) {
50
LIST Clauses, Axioms, Conjectures, SortDecls, UserDefinedPrecedence, UserDefinedSelection, ClAxRelation, Includes;
51
PRECEDENCE Precedence;
53
DFGDESCRIPTION Description;
60
memory_Init(memory__UNLIMITED);
63
flag_Init(flag_SPASS);
64
flag_Init(flag_TPTP2DFG); /* declare include flag */
67
Flags = flag_CreateStore();
68
flag_InitStoreByDefaults(Flags);
70
Precedence = symbol_CreatePrecedence();
72
fol_Init(TRUE, Precedence);
76
if (!cmdlne_Read(argc, argv) ||
77
cmdlne_GetInputFile() == (char*)NULL ||
78
cmdlne_GetOutputFile() == (char*)NULL) {
80
fputs("\n\t tptp2dfg Version ", stdout);
81
fputs(TPTP2DFG__VERSION, stdout);
82
puts("\n\t Usage: tptp2dfg [-include] <input-file> <output-file>\n");
84
puts("-include makes the parser parse the files in include directive,");
85
puts("\tby default the directives are just translated.");
89
if (!cmdlne_SetFlags(Flags))
95
Conjectures = list_Nil();
96
SortDecls = list_Nil();
97
UserDefinedPrecedence = list_Nil();
98
UserDefinedSelection = list_Nil();
99
ClAxRelation = list_Nil();
100
Includes = list_Nil();
102
Description = desc_Create();
104
InFile = tptp_OpenFile(cmdlne_GetInputFile(),NULL);
105
OutFile = misc_OpenFile(cmdlne_GetOutputFile(),"w");
106
tptp_SetParserOutput(OutFile); /*for the header comments*/
109
if (flag_GetFlagIntValue(Flags, flag_INCLUDE) == flag_INCLUDEON) {
110
Clauses = tptp_TPTPParser(InFile, Flags, Precedence, Description,
111
&Axioms, &Conjectures, &SortDecls,
112
&UserDefinedPrecedence, &UserDefinedSelection, &ClAxRelation,
115
Clauses = tptp_TPTPParserIncludesExplicit(InFile, Flags, Precedence, Description,
116
&Axioms, &Conjectures, &SortDecls,
117
&UserDefinedPrecedence, &UserDefinedSelection, &ClAxRelation,
122
tptp_FPrintDFGProblem(OutFile,Description,Clauses,ClAxRelation,Axioms,Conjectures,Includes,tptp_WasParsingCNF());
124
misc_CloseFile(InFile,cmdlne_GetInputFile());
125
misc_CloseFile(OutFile,cmdlne_GetOutputFile());
127
/* Free the variables */
128
desc_Delete(Description);
130
list_DeleteWithElement(Clauses, (void (*)(POINTER))clause_Delete);
131
list_DeleteWithElement(Axioms, tptp_DeleteLabelTermPair);
132
list_DeleteWithElement(Conjectures, tptp_DeleteLabelTermPair);
133
list_DeleteWithElement(Includes, tptp_DeleteStrListOfStrPair);
135
dfg_DeleteClAxRelation(ClAxRelation);
138
if (list_Exist(SortDecls) || list_Exist(UserDefinedPrecedence)
139
|| list_Exist(UserDefinedSelection)) {
140
misc_StartUserErrorReport();
141
misc_ErrorReport("SortDecls, UserDefinedPrecedence, and UserDefinedSelection should all be empty!\n");
142
misc_FinishUserErrorReport();
149
flag_DeleteStore(Flags);
150
symbol_DeletePrecedence(Precedence);
152
symbol_FreeAllSymbols();
160
static void tptp_FPrintFormulaList(FILE* File, LIST Formulas, const char* Name)
161
/**************************************************************
162
INPUT: A file, a list of formulas, a name.
163
EFFECTS: Print a list formulas in DFG format, with given list name.
164
**************************************************************/
168
fputs("list_of_formulae(", File);
171
for (scan = Formulas; !list_Empty(scan); scan= list_Cdr(scan)) {
172
LIST formula_pair = list_Car(scan);
174
fputs("\tformula(", File);
175
fol_FPrintDFG(File, list_PairSecond(formula_pair));
177
fputs((char*)list_PairFirst(formula_pair), File);
180
fputs("end_of_list.\n\n", File);
183
static void tptp_FPrintClauseWithLabel(FILE* File, CLAUSE Clause, const char* label) {
189
n = clause_Length(Clause);
191
fputs("\tclause(", File);
192
Variables = list_Nil();
194
for (j = 0; j < n; j++)
196
list_NPointerUnion(Variables,
197
term_VariableSymbols(clause_GetLiteralAtom(Clause,j)));
199
if (!list_Empty(Variables)) {
200
symbol_FPrint(File, fol_All());
202
for (Iter = Variables; !list_Empty(Iter); Iter = list_Cdr(Iter)) {
203
symbol_FPrint(File, (SYMBOL) list_Car(Iter));
204
if (!list_Empty(list_Cdr(Iter)))
210
symbol_FPrint(File, fol_Or());
213
for (j = 0; j < n; j++) {
214
Lit = clause_GetLiteral(Clause,j);
215
Atom = clause_LiteralSignedAtom(Lit);
216
term_FPrintPrefix(File,Atom);
221
symbol_FPrint(File,fol_False());
223
if (!list_Empty(Variables)) {
224
list_Delete(Variables);
228
fprintf(File, "),%s", label);
233
static void tptp_FPrintClauseList(FILE* File, LIST Clauses,
234
LIST ClAxRelation, BOOL PrintConjectures, const char* Name)
235
/**************************************************************
236
INPUT: A file, a list of clauses, a flag and a name.
237
EFFECTS: Print a list formulas in DFG format, with given list name.
238
Prints the fromula only when
239
("the formula is a conjecture clause" == PrintConjectures)
240
**************************************************************/
244
fputs("list_of_clauses(", File);
246
fputs(",cnf).\n", File);
248
for (Scan=Clauses; !list_Empty(Scan);Scan=list_Cdr(Scan)) {
253
Clause = (CLAUSE) list_Car(Scan);
256
if (list_Empty(ClAxRelation)) {
257
misc_StartUserErrorReport();
258
misc_ErrorReport("ClAxRelation too short!\n");
259
misc_FinishUserErrorReport();
263
ClAx = (LIST) list_Car(ClAxRelation);
266
if ((int)list_Car(ClAx) != clause_Number(Clause)) {
267
misc_StartUserErrorReport();
268
misc_ErrorReport("Incompatible ClAxRelation!\n");
269
misc_FinishUserErrorReport();
273
Labels = list_Cdr(ClAx);
276
if (list_Empty(Labels)) {
277
misc_StartUserErrorReport();
278
misc_ErrorReport("Empty ClAxRelation entry!\n");
279
misc_FinishUserErrorReport();
283
if (PrintConjectures == clause_GetFlag(Clause,CONCLAUSE))
284
tptp_FPrintClauseWithLabel(File,Clause,(char*)list_Car(Labels) /*the first and the only one*/);
286
ClAxRelation = list_Cdr(ClAxRelation);
288
fputs("end_of_list.\n\n", File);
291
static void tptp_FPrintInclude(FILE* File, char* filename, LIST selection)
292
/**************************************************************
293
INPUT: A file, name of file included, list of formule secelection.
294
EFFECTS: Print a line in DFG format denoting the given inclusion.
295
**************************************************************/
299
fputs("\tinclude('",File);
301
len = strlen(filename);
302
if ((len >= 2) && (filename[len-1] == 'p') && (filename[len-2] == '.')) { /*changing *.p to *.dfg */
303
filename[len-1] = '\0';
304
fputs(filename,File);
306
filename[len-1] = 'p';
307
} else if ((len >= 3) && (filename[len-1] == 'x') && (filename[len-2] == 'a') && (filename[len-3] == '.')) { /*changing *.ax to *.ax.dfg */
308
fputs(filename,File);
311
fputs(filename,File);
316
if (list_Exist(selection)) {
318
fputs((char*)list_Car(selection),File);
319
selection = list_Cdr(selection);
321
while(list_Exist(selection)) {
323
fputs((char*)list_Car(selection),File);
324
selection = list_Cdr(selection);
332
static __inline__ const char* Null2Empty(char * str) {
333
return str ? str : "";
336
static void tptp_FPrintDFGProblem(FILE* File, DFGDESCRIPTION Description,
337
LIST Clauses, LIST ClAxRelation,
338
LIST Axioms, LIST Conjectures,
339
LIST Includes, BOOL IsCnf)
340
/**************************************************************
341
INPUT: A file, some info strings, two lists of formulas, and a boolean flag
342
EFFECTS: Prints a complete DFG file containing these lists.
343
**************************************************************/
345
fputs("begin_problem(Unknown).\n\n", File);
347
fputs("list_of_descriptions.\n", File);
348
fprintf(File,"name({*%s*}).\n",Null2Empty(Description->name));
349
fprintf(File,"author({*%s*}).\n",Null2Empty(Description->author));
350
if (Description->version)
351
fprintf(File,"version({*%s*}).\n",Null2Empty(Description->version));
352
if (Description->logic)
353
fprintf(File,"logic({*%s*}).\n",Null2Empty(Description->logic));
355
fprintf(File,"status(%s).\n",desc_StatusString(Description));
357
fprintf(File,"description({*%s*}).\n",Null2Empty(Description->description));
358
if (Description->date)
359
fprintf(File,"date({*%s*}).\n",Null2Empty(Description->date));
360
fputs("end_of_list.\n\n", File);
362
fputs("list_of_symbols.\n", File);
363
symbol_FPrintDFGSignature(File);
364
fputs("end_of_list.\n\n", File);
367
tptp_FPrintFormulaList(File, Axioms, "axioms");
368
tptp_FPrintFormulaList(File, Conjectures, "conjectures");
370
tptp_FPrintClauseList(File, Clauses, ClAxRelation, FALSE, "axioms");
371
tptp_FPrintClauseList(File, Clauses, ClAxRelation, TRUE, "conjectures");
374
if (list_Exist(Includes)) {
377
fputs("list_of_includes.\n", File);
379
for (Scan = Includes; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
380
LIST pair = list_Car(Scan);
381
tptp_FPrintInclude(File,list_PairFirst(pair),list_PairSecond(pair));
384
fputs("end_of_list.\n\n", File);
387
fputs("end_problem.\n", File);