forked from mclumd/alma-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alma_parser.c
106 lines (98 loc) · 3.73 KB
/
alma_parser.c
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
#include "alma_parser.h"
mpc_parser_t* Alma;
mpc_parser_t* Almaformula;
mpc_parser_t* Formula;
mpc_parser_t* FFormula;
mpc_parser_t* BFormula;
mpc_parser_t* Conjform;
mpc_parser_t* Literal;
mpc_parser_t* Neglit;
mpc_parser_t* Poslit;
mpc_parser_t* Listofterms;
mpc_parser_t* Term;
mpc_parser_t* Predname;
mpc_parser_t* Constant;
mpc_parser_t* Funcname;
mpc_parser_t* Variable;
mpc_parser_t* Prologconst;
void parse_init(void) {
Alma = mpc_new("alma");
Almaformula = mpc_new("almaformula");
Formula = mpc_new("formula");
FFormula = mpc_new("fformula");
BFormula = mpc_new("bformula");
Conjform = mpc_new("conjform");
Literal = mpc_new("literal");
Neglit = mpc_new("neglit");
Poslit = mpc_new("poslit");
Listofterms = mpc_new("listofterms");
Term = mpc_new("term");
Predname = mpc_new("predname");
Constant = mpc_new("constant");
Funcname = mpc_new("funcname");
Variable = mpc_new("variable");
Prologconst = mpc_new("prologconst");
// TODO: Add to, allowing Prolog comments to be ignored, and other more general features
mpca_lang(MPCA_LANG_DEFAULT,
" "
" alma : /^/ <almaformula>* /$/ ; "
" almaformula : (<fformula> | <bformula> | <formula>) '.' ; "
" formula : \"and(\" <formula> ',' <formula> ')' "
" | \"or(\" <formula> ',' <formula> ')' "
" | \"if(\" <formula> ',' <formula> ')' "
" | <literal> ; "
" fformula : \"fif(\" <conjform> ',' \"conclusion(\" "
" <poslit> ')' ')' ; "
" bformula : \"bif(\" <formula> ',' <formula> ')' ; "
" conjform : \"and(\" <conjform> ',' <conjform> ')' "
" | <literal> ; "
" literal : <neglit> | <poslit> ; "
" neglit : \"not(\" <poslit> ')' ; "
" poslit : <predname> '(' <listofterms> ')' "
" | <predname> ; "
" listofterms : <term> (',' <term>)* ; "
" term : <funcname> '(' <listofterms> ')' "
" | <variable> | <constant> ; "
" predname : <prologconst> ; "
" constant : <prologconst> ; "
" funcname : <prologconst> ; "
" variable : /[A-Z_][a-zA-Z0-9_]*/ ; "
" prologconst : /[a-zA-Z0-9_]*/ ; ",
Alma, Almaformula, Formula, FFormula, BFormula, Conjform, Literal, Neglit,
Poslit, Listofterms, Term, Predname, Constant, Funcname, Variable,
Prologconst, NULL);
}
// Attempts to open filename and parse contents according to ALMA language
// Boolean return for success or failure of parse
// If the parse was successful, AST is a valid pointer to the result
int parse_file(char *filename, mpc_ast_t **ast) {
mpc_result_t r;
int result = mpc_parse_contents(filename, Alma, &r);
if (result) {
*ast = r.output;
return 1;
}
else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
return 0;
}
}
int parse_string(char *string, mpc_ast_t **ast) {
mpc_result_t r;
int result = mpc_parse("stringname", string, Alma, &r);
if (result) {
*ast = r.output;
return 1;
}
else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
return 0;
}
}
void parse_cleanup(void) {
mpc_cleanup(16, Alma, Almaformula, Formula, FFormula, BFormula, Conjform,
Literal, Neglit, Poslit, Listofterms, Term, Predname, Constant,
Funcname, Variable, Prologconst);
}