expr.y 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Simple expression parser */
  2. %{
  3. #include "util.h"
  4. #include "util/debug.h"
  5. #define IN_EXPR_Y 1
  6. #include "expr.h"
  7. #include "smt.h"
  8. #include <string.h>
  9. #define MAXIDLEN 256
  10. %}
  11. %define api.pure full
  12. %parse-param { double *final_val }
  13. %parse-param { struct parse_ctx *ctx }
  14. %parse-param { const char **pp }
  15. %lex-param { const char **pp }
  16. %union {
  17. double num;
  18. char id[MAXIDLEN+1];
  19. }
  20. %token <num> NUMBER
  21. %token <id> ID
  22. %token MIN MAX IF ELSE SMT_ON
  23. %left MIN MAX IF
  24. %left '|'
  25. %left '^'
  26. %left '&'
  27. %left '-' '+'
  28. %left '*' '/' '%'
  29. %left NEG NOT
  30. %type <num> expr if_expr
  31. %{
  32. static int expr__lex(YYSTYPE *res, const char **pp);
  33. static void expr__error(double *final_val __maybe_unused,
  34. struct parse_ctx *ctx __maybe_unused,
  35. const char **pp __maybe_unused,
  36. const char *s)
  37. {
  38. pr_debug("%s\n", s);
  39. }
  40. static int lookup_id(struct parse_ctx *ctx, char *id, double *val)
  41. {
  42. int i;
  43. for (i = 0; i < ctx->num_ids; i++) {
  44. if (!strcasecmp(ctx->ids[i].name, id)) {
  45. *val = ctx->ids[i].val;
  46. return 0;
  47. }
  48. }
  49. return -1;
  50. }
  51. %}
  52. %%
  53. all_expr: if_expr { *final_val = $1; }
  54. ;
  55. if_expr:
  56. expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
  57. | expr
  58. ;
  59. expr: NUMBER
  60. | ID { if (lookup_id(ctx, $1, &$$) < 0) {
  61. pr_debug("%s not found\n", $1);
  62. YYABORT;
  63. }
  64. }
  65. | expr '|' expr { $$ = (long)$1 | (long)$3; }
  66. | expr '&' expr { $$ = (long)$1 & (long)$3; }
  67. | expr '^' expr { $$ = (long)$1 ^ (long)$3; }
  68. | expr '+' expr { $$ = $1 + $3; }
  69. | expr '-' expr { $$ = $1 - $3; }
  70. | expr '*' expr { $$ = $1 * $3; }
  71. | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; }
  72. | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; }
  73. | '-' expr %prec NEG { $$ = -$2; }
  74. | '(' if_expr ')' { $$ = $2; }
  75. | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
  76. | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
  77. | SMT_ON { $$ = smt_on() > 0; }
  78. ;
  79. %%
  80. static int expr__symbol(YYSTYPE *res, const char *p, const char **pp)
  81. {
  82. char *dst = res->id;
  83. const char *s = p;
  84. if (*p == '#')
  85. *dst++ = *p++;
  86. while (isalnum(*p) || *p == '_' || *p == '.' || *p == ':' || *p == '@' || *p == '\\') {
  87. if (p - s >= MAXIDLEN)
  88. return -1;
  89. /*
  90. * Allow @ instead of / to be able to specify pmu/event/ without
  91. * conflicts with normal division.
  92. */
  93. if (*p == '@')
  94. *dst++ = '/';
  95. else if (*p == '\\')
  96. *dst++ = *++p;
  97. else
  98. *dst++ = *p;
  99. p++;
  100. }
  101. *dst = 0;
  102. *pp = p;
  103. dst = res->id;
  104. switch (dst[0]) {
  105. case 'm':
  106. if (!strcmp(dst, "min"))
  107. return MIN;
  108. if (!strcmp(dst, "max"))
  109. return MAX;
  110. break;
  111. case 'i':
  112. if (!strcmp(dst, "if"))
  113. return IF;
  114. break;
  115. case 'e':
  116. if (!strcmp(dst, "else"))
  117. return ELSE;
  118. break;
  119. case '#':
  120. if (!strcasecmp(dst, "#smt_on"))
  121. return SMT_ON;
  122. break;
  123. }
  124. return ID;
  125. }
  126. static int expr__lex(YYSTYPE *res, const char **pp)
  127. {
  128. int tok;
  129. const char *s;
  130. const char *p = *pp;
  131. while (isspace(*p))
  132. p++;
  133. s = p;
  134. switch (*p++) {
  135. case '#':
  136. case 'a' ... 'z':
  137. case 'A' ... 'Z':
  138. return expr__symbol(res, p - 1, pp);
  139. case '0' ... '9': case '.':
  140. res->num = strtod(s, (char **)&p);
  141. tok = NUMBER;
  142. break;
  143. default:
  144. tok = *s;
  145. break;
  146. }
  147. *pp = p;
  148. return tok;
  149. }
  150. /* Caller must make sure id is allocated */
  151. void expr__add_id(struct parse_ctx *ctx, const char *name, double val)
  152. {
  153. int idx;
  154. assert(ctx->num_ids < MAX_PARSE_ID);
  155. idx = ctx->num_ids++;
  156. ctx->ids[idx].name = name;
  157. ctx->ids[idx].val = val;
  158. }
  159. void expr__ctx_init(struct parse_ctx *ctx)
  160. {
  161. ctx->num_ids = 0;
  162. }
  163. static bool already_seen(const char *val, const char *one, const char **other,
  164. int num_other)
  165. {
  166. int i;
  167. if (one && !strcasecmp(one, val))
  168. return true;
  169. for (i = 0; i < num_other; i++)
  170. if (!strcasecmp(other[i], val))
  171. return true;
  172. return false;
  173. }
  174. int expr__find_other(const char *p, const char *one, const char ***other,
  175. int *num_otherp)
  176. {
  177. const char *orig = p;
  178. int err = -1;
  179. int num_other;
  180. *other = malloc((EXPR_MAX_OTHER + 1) * sizeof(char *));
  181. if (!*other)
  182. return -1;
  183. num_other = 0;
  184. for (;;) {
  185. YYSTYPE val;
  186. int tok = expr__lex(&val, &p);
  187. if (tok == 0) {
  188. err = 0;
  189. break;
  190. }
  191. if (tok == ID && !already_seen(val.id, one, *other, num_other)) {
  192. if (num_other >= EXPR_MAX_OTHER - 1) {
  193. pr_debug("Too many extra events in %s\n", orig);
  194. break;
  195. }
  196. (*other)[num_other] = strdup(val.id);
  197. if (!(*other)[num_other])
  198. return -1;
  199. num_other++;
  200. }
  201. }
  202. (*other)[num_other] = NULL;
  203. *num_otherp = num_other;
  204. if (err) {
  205. *num_otherp = 0;
  206. free(*other);
  207. *other = NULL;
  208. }
  209. return err;
  210. }