BPredicate.y 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @file BPredicate.y
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * {@link BPredicate} grammar file.
  32. */
  33. %{
  34. #include <stdlib.h>
  35. #include <predicate/BPredicate_internal.h>
  36. #include <predicate/BPredicate_parser.h>
  37. #define YYLEX_PARAM scanner
  38. static struct predicate_node * make_constant (int val)
  39. {
  40. struct predicate_node *n = malloc(sizeof(*n));
  41. if (!n) {
  42. return NULL;
  43. }
  44. n->type = NODE_CONSTANT;
  45. n->constant.val = val;
  46. return n;
  47. }
  48. static struct predicate_node * make_negation (struct predicate_node *op)
  49. {
  50. if (!op) {
  51. goto fail;
  52. }
  53. struct predicate_node *n = malloc(sizeof(*n));
  54. if (!n) {
  55. goto fail;
  56. }
  57. n->type = NODE_NEG;
  58. n->neg.op = op;
  59. return n;
  60. fail:
  61. if (op) {
  62. free_predicate_node(op);
  63. }
  64. return NULL;
  65. }
  66. static struct predicate_node * make_conjunction (struct predicate_node *op1, struct predicate_node *op2)
  67. {
  68. if (!op1 || !op2) {
  69. goto fail;
  70. }
  71. struct predicate_node *n = malloc(sizeof(*n));
  72. if (!n) {
  73. goto fail;
  74. }
  75. n->type = NODE_CONJUNCT;
  76. n->conjunct.op1 = op1;
  77. n->conjunct.op2 = op2;
  78. return n;
  79. fail:
  80. if (op1) {
  81. free_predicate_node(op1);
  82. }
  83. if (op2) {
  84. free_predicate_node(op2);
  85. }
  86. return NULL;
  87. }
  88. static struct predicate_node * make_disjunction (struct predicate_node *op1, struct predicate_node *op2)
  89. {
  90. if (!op1 || !op2) {
  91. goto fail;
  92. }
  93. struct predicate_node *n = malloc(sizeof(*n));
  94. if (!n) {
  95. goto fail;
  96. }
  97. n->type = NODE_DISJUNCT;
  98. n->disjunct.op1 = op1;
  99. n->disjunct.op2 = op2;
  100. return n;
  101. fail:
  102. if (op1) {
  103. free_predicate_node(op1);
  104. }
  105. if (op2) {
  106. free_predicate_node(op2);
  107. }
  108. return NULL;
  109. }
  110. static struct predicate_node * make_function (char *name, struct arguments_node *args, int need_args)
  111. {
  112. if (!name || (!args && need_args)) {
  113. goto fail;
  114. }
  115. struct predicate_node *n = malloc(sizeof(*n));
  116. if (!n) {
  117. goto fail;
  118. }
  119. n->type = NODE_FUNCTION;
  120. n->function.name = name;
  121. n->function.args = args;
  122. return n;
  123. fail:
  124. if (name) {
  125. free(name);
  126. }
  127. if (args) {
  128. free_arguments_node(args);
  129. }
  130. return NULL;
  131. }
  132. static struct arguments_node * make_arguments (struct arguments_arg arg, struct arguments_node *next, int need_next)
  133. {
  134. if (arg.type == ARGUMENT_INVALID || (!next && need_next)) {
  135. goto fail;
  136. }
  137. struct arguments_node *n = malloc(sizeof(*n));
  138. if (!n) {
  139. goto fail;
  140. }
  141. n->arg = arg;
  142. n->next = next;
  143. return n;
  144. fail:
  145. free_argument(arg);
  146. if (next) {
  147. free_arguments_node(next);
  148. }
  149. return NULL;
  150. }
  151. static struct arguments_arg make_argument_predicate (struct predicate_node *pr)
  152. {
  153. struct arguments_arg ret;
  154. if (!pr) {
  155. goto fail;
  156. }
  157. ret.type = ARGUMENT_PREDICATE;
  158. ret.predicate = pr;
  159. return ret;
  160. fail:
  161. ret.type = ARGUMENT_INVALID;
  162. return ret;
  163. }
  164. static struct arguments_arg make_argument_string (char *string)
  165. {
  166. struct arguments_arg ret;
  167. if (!string) {
  168. goto fail;
  169. }
  170. ret.type = ARGUMENT_STRING;
  171. ret.string = string;
  172. return ret;
  173. fail:
  174. ret.type = ARGUMENT_INVALID;
  175. return ret;
  176. }
  177. %}
  178. %pure-parser
  179. %locations
  180. %parse-param {void *scanner}
  181. %parse-param {struct predicate_node **result}
  182. %union {
  183. char *text;
  184. struct predicate_node *node;
  185. struct arguments_node *arg_node;
  186. struct predicate_node nfaw;
  187. struct arguments_arg arg_arg;
  188. };
  189. // token types
  190. %token <text> STRING NAME
  191. %token PEER1_NAME PEER2_NAME AND OR NOT SPAR EPAR CONSTANT_TRUE CONSTANT_FALSE COMMA
  192. // string token destructor
  193. %destructor {
  194. free($$);
  195. } STRING NAME
  196. // return values
  197. %type <node> predicate constant parentheses neg conjunct disjunct function
  198. %type <arg_node> arguments
  199. %type <arg_arg> argument
  200. // predicate node destructor
  201. %destructor {
  202. if ($$) {
  203. free_predicate_node($$);
  204. }
  205. } predicate constant parentheses neg conjunct disjunct function
  206. // argument node destructor
  207. %destructor {
  208. if ($$) {
  209. free_arguments_node($$);
  210. }
  211. } arguments
  212. // argument argument destructor
  213. %destructor {
  214. free_argument($$);
  215. } argument
  216. %left OR
  217. %left AND
  218. %nonassoc NOT
  219. %right COMMA
  220. %%
  221. input:
  222. predicate {
  223. *result = $1;
  224. }
  225. ;
  226. predicate: constant | parentheses | neg | conjunct | disjunct | function;
  227. constant:
  228. CONSTANT_TRUE {
  229. $$ = make_constant(1);
  230. }
  231. |
  232. CONSTANT_FALSE {
  233. $$ = make_constant(0);
  234. }
  235. ;
  236. parentheses:
  237. SPAR predicate EPAR {
  238. $$ = $2;
  239. }
  240. ;
  241. neg:
  242. NOT predicate {
  243. $$ = make_negation($2);
  244. }
  245. ;
  246. conjunct:
  247. predicate AND predicate {
  248. $$ = make_conjunction($1, $3);
  249. }
  250. ;
  251. disjunct:
  252. predicate OR predicate {
  253. $$ = make_disjunction($1, $3);
  254. }
  255. ;
  256. function:
  257. NAME SPAR EPAR {
  258. $$ = make_function($1, NULL, 0);
  259. }
  260. |
  261. NAME SPAR arguments EPAR {
  262. $$ = make_function($1, $3, 1);
  263. }
  264. ;
  265. arguments:
  266. argument {
  267. $$ = make_arguments($1, NULL, 0);
  268. }
  269. |
  270. argument COMMA arguments {
  271. $$ = make_arguments($1, $3, 1);
  272. }
  273. ;
  274. argument:
  275. predicate {
  276. $$ = make_argument_predicate($1);
  277. }
  278. |
  279. STRING {
  280. $$ = make_argument_string($1);
  281. }
  282. ;