-
Notifications
You must be signed in to change notification settings - Fork 3
/
alma_parser.c
130 lines (118 loc) · 4.43 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "alma_parser.h"
#include <pthread.h>
mpc_parser_t* Alma;
mpc_parser_t* Almacomment;
mpc_parser_t* Almaformula;
mpc_parser_t* Sentence;
mpc_parser_t* Formula;
mpc_parser_t* FFormula;
mpc_parser_t* FFormConc;
mpc_parser_t* BFormula;
mpc_parser_t* Conjform;
mpc_parser_t* Literal;
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;
pthread_mutex_t count_mutex;
int parser_ref_count = 0;
void parse_init(void) {
pthread_mutex_lock(&count_mutex);
parser_ref_count++;
pthread_mutex_unlock(&count_mutex);
Alma = mpc_new("alma");
Almacomment = mpc_new("almacomment");
Almaformula = mpc_new("almaformula");
Sentence = mpc_new("sentence");
Formula = mpc_new("formula");
FFormula = mpc_new("fformula");
FFormConc = mpc_new("fformconc");
BFormula = mpc_new("bformula");
Conjform = mpc_new("conjform");
Literal = mpc_new("literal");
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");
mpca_lang(MPCA_LANG_DEFAULT,
" "
" alma : /^/ (<almaformula> | <almacomment>)* /$/ ; "
" almacomment : /%[^\n]*\n/ ; "
" almaformula : <sentence> '.' ; "
" sentence : <fformula> | <bformula> | <formula> ; "
" formula : \"and(\" <formula> ',' <formula> ')' "
" | \"or(\" <formula> ',' <formula> ')' "
" | \"if(\" <formula> ',' <formula> ')' "
" | \"not(\" <formula> ')' "
" | <literal> ; "
" fformula : \"fif(\" <conjform> ',' <fformconc> ')' ; "
" fformconc : \"and(\" <fformconc> ',' <fformconc> ')' "
" | <fformula> | \"not(\" <literal> ')' "
" | <literal> ; "
" bformula : \"bif(\" <formula> ',' <formula> ')' ; "
" conjform : \"and(\" <conjform> ',' <conjform> ')' "
" | \"not(\" <literal> ')' "
" | <literal> ; "
" literal : <predname> '(' <listofterms> ')' "
" | <predname> ; "
" listofterms : <term> (',' <term>)* ; "
" term : \"quote\" '(' <sentence> ')' "
" | <funcname> '(' <listofterms> ')' "
" | <variable> | <constant> ; "
" predname : <prologconst> ; "
" constant : <prologconst> ; "
" funcname : <prologconst> ; "
" variable : '`' <variable> | /[A-Z][a-zA-Z0-9_]*/; "
" prologconst : /[a-z0-9_][a-zA-Z0-9_]*/ ; ",
Alma, Almacomment, Almaformula, Sentence, Formula, FFormula,
FFormConc, BFormula, Conjform, Literal, 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;
//mpc_ast_print(*ast);
//exit(0);
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) {
pthread_mutex_lock(&count_mutex);
parser_ref_count--;
pthread_mutex_unlock(&count_mutex);
if (parser_ref_count == 0) {
mpc_cleanup(17, Alma, Almacomment, Almaformula, Sentence, Formula,
FFormula, FFormConc, BFormula, Conjform, Literal, Listofterms,
Term, Predname, Constant, Funcname, Variable, Prologconst);
}
}