expr.y 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <string.h>
  8. #define MAXIDLEN 256
  9. %}
  10. %pure-parser
  11. %parse-param { double *final_val }
  12. %parse-param { struct parse_ctx *ctx }
  13. %parse-param { const char **pp }
  14. %lex-param { const char **pp }
  15. %union {
  16. double num;
  17. char id[MAXIDLEN+1];
  18. }
  19. %token <num> NUMBER
  20. %token <id> ID
  21. %left '|'
  22. %left '^'
  23. %left '&'
  24. %left '-' '+'
  25. %left '*' '/' '%'
  26. %left NEG NOT
  27. %type <num> expr
  28. %{
  29. static int expr__lex(YYSTYPE *res, const char **pp);
  30. static void expr__error(double *final_val __maybe_unused,
  31. struct parse_ctx *ctx __maybe_unused,
  32. const char **pp __maybe_unused,
  33. const char *s)
  34. {
  35. pr_debug("%s\n", s);
  36. }
  37. static int lookup_id(struct parse_ctx *ctx, char *id, double *val)
  38. {
  39. int i;
  40. for (i = 0; i < ctx->num_ids; i++) {
  41. if (!strcasecmp(ctx->ids[i].name, id)) {
  42. *val = ctx->ids[i].val;
  43. return 0;
  44. }
  45. }
  46. return -1;
  47. }
  48. %}
  49. %%
  50. all_expr: expr { *final_val = $1; }
  51. ;
  52. expr: NUMBER
  53. | ID { if (lookup_id(ctx, $1, &$$) < 0) {
  54. pr_debug("%s not found", $1);
  55. YYABORT;
  56. }
  57. }
  58. | expr '+' expr { $$ = $1 + $3; }
  59. | expr '-' expr { $$ = $1 - $3; }
  60. | expr '*' expr { $$ = $1 * $3; }
  61. | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; }
  62. | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; }
  63. | '-' expr %prec NEG { $$ = -$2; }
  64. | '(' expr ')' { $$ = $2; }
  65. ;
  66. %%
  67. static int expr__symbol(YYSTYPE *res, const char *p, const char **pp)
  68. {
  69. char *dst = res->id;
  70. const char *s = p;
  71. while (isalnum(*p) || *p == '_' || *p == '.') {
  72. if (p - s >= MAXIDLEN)
  73. return -1;
  74. *dst++ = *p++;
  75. }
  76. *dst = 0;
  77. *pp = p;
  78. return ID;
  79. }
  80. static int expr__lex(YYSTYPE *res, const char **pp)
  81. {
  82. int tok;
  83. const char *s;
  84. const char *p = *pp;
  85. while (isspace(*p))
  86. p++;
  87. s = p;
  88. switch (*p++) {
  89. case 'a' ... 'z':
  90. case 'A' ... 'Z':
  91. return expr__symbol(res, p - 1, pp);
  92. case '0' ... '9': case '.':
  93. res->num = strtod(s, (char **)&p);
  94. tok = NUMBER;
  95. break;
  96. default:
  97. tok = *s;
  98. break;
  99. }
  100. *pp = p;
  101. return tok;
  102. }
  103. /* Caller must make sure id is allocated */
  104. void expr__add_id(struct parse_ctx *ctx, const char *name, double val)
  105. {
  106. int idx;
  107. assert(ctx->num_ids < MAX_PARSE_ID);
  108. idx = ctx->num_ids++;
  109. ctx->ids[idx].name = name;
  110. ctx->ids[idx].val = val;
  111. }
  112. void expr__ctx_init(struct parse_ctx *ctx)
  113. {
  114. ctx->num_ids = 0;
  115. }
  116. int expr__find_other(const char *p, const char *one, const char ***other,
  117. int *num_otherp)
  118. {
  119. const char *orig = p;
  120. int err = -1;
  121. int num_other;
  122. *other = malloc((EXPR_MAX_OTHER + 1) * sizeof(char *));
  123. if (!*other)
  124. return -1;
  125. num_other = 0;
  126. for (;;) {
  127. YYSTYPE val;
  128. int tok = expr__lex(&val, &p);
  129. if (tok == 0) {
  130. err = 0;
  131. break;
  132. }
  133. if (tok == ID && strcasecmp(one, val.id)) {
  134. if (num_other >= EXPR_MAX_OTHER - 1) {
  135. pr_debug("Too many extra events in %s\n", orig);
  136. break;
  137. }
  138. (*other)[num_other] = strdup(val.id);
  139. if (!(*other)[num_other])
  140. return -1;
  141. num_other++;
  142. }
  143. }
  144. (*other)[num_other] = NULL;
  145. *num_otherp = num_other;
  146. if (err) {
  147. *num_otherp = 0;
  148. free(*other);
  149. *other = NULL;
  150. }
  151. return err;
  152. }