Loading...
1%option prefix="expr_"
2%option reentrant
3%option bison-bridge
4
5%{
6#include <linux/compiler.h>
7#include "expr.h"
8#include "expr-bison.h"
9#include <math.h>
10
11char *expr_get_text(yyscan_t yyscanner);
12YYSTYPE *expr_get_lval(yyscan_t yyscanner);
13
14static double __value(YYSTYPE *yylval, char *str, int token)
15{
16 double num;
17
18 errno = 0;
19 num = strtod(str, NULL);
20 if (errno)
21 return EXPR_ERROR;
22
23 yylval->num = num;
24 return token;
25}
26
27static int value(yyscan_t scanner)
28{
29 YYSTYPE *yylval = expr_get_lval(scanner);
30 char *text = expr_get_text(scanner);
31
32 return __value(yylval, text, NUMBER);
33}
34
35/*
36 * Allow @ instead of / to be able to specify pmu/event/ without
37 * conflicts with normal division.
38 */
39static char *normalize(char *str, int runtime)
40{
41 char *ret = str;
42 char *dst = str;
43
44 while (*str) {
45 if (*str == '\\') {
46 *dst++ = *++str;
47 if (!*str)
48 break;
49 }
50 else if (*str == '?') {
51 char *paramval;
52 int i = 0;
53 int size = asprintf(¶mval, "%d", runtime);
54
55 if (size < 0)
56 *dst++ = '0';
57 else {
58 while (i < size)
59 *dst++ = paramval[i++];
60 free(paramval);
61 }
62 }
63 else
64 *dst++ = *str;
65 str++;
66 }
67
68 *dst = 0x0;
69 return ret;
70}
71
72static int str(yyscan_t scanner, int token, int runtime)
73{
74 YYSTYPE *yylval = expr_get_lval(scanner);
75 char *text = expr_get_text(scanner);
76
77 yylval->str = normalize(strdup(text), runtime);
78 if (!yylval->str)
79 return EXPR_ERROR;
80
81 yylval->str = normalize(yylval->str, runtime);
82 return token;
83}
84
85static int literal(yyscan_t scanner, const struct expr_scanner_ctx *sctx)
86{
87 YYSTYPE *yylval = expr_get_lval(scanner);
88
89 yylval->num = expr__get_literal(expr_get_text(scanner), sctx);
90 if (isnan(yylval->num))
91 return EXPR_ERROR;
92
93 return LITERAL;
94}
95%}
96
97number ([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)(e-?[0-9]+)?
98
99sch [-,=]
100spec \\{sch}
101sym [0-9a-zA-Z_\.:@?]+
102symbol ({spec}|{sym})+
103literal #[0-9a-zA-Z_\.\-]+
104
105%%
106 struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
107
108d_ratio { return D_RATIO; }
109max { return MAX; }
110min { return MIN; }
111if { return IF; }
112else { return ELSE; }
113source_count { return SOURCE_COUNT; }
114{literal} { return literal(yyscanner, sctx); }
115{number} { return value(yyscanner); }
116{symbol} { return str(yyscanner, ID, sctx->runtime); }
117"|" { return '|'; }
118"^" { return '^'; }
119"&" { return '&'; }
120"<" { return '<'; }
121">" { return '>'; }
122"-" { return '-'; }
123"+" { return '+'; }
124"*" { return '*'; }
125"/" { return '/'; }
126"%" { return '%'; }
127"(" { return '('; }
128")" { return ')'; }
129"," { return ','; }
130. { }
131%%
132
133int expr_wrap(void *scanner __maybe_unused)
134{
135 return 1;
136}
1%option prefix="expr_"
2%option reentrant
3%option bison-bridge
4
5%{
6#include <linux/compiler.h>
7#include "expr.h"
8#include "expr-bison.h"
9
10char *expr_get_text(yyscan_t yyscanner);
11YYSTYPE *expr_get_lval(yyscan_t yyscanner);
12
13static double __value(YYSTYPE *yylval, char *str, int token)
14{
15 double num;
16
17 errno = 0;
18 num = strtod(str, NULL);
19 if (errno)
20 return EXPR_ERROR;
21
22 yylval->num = num;
23 return token;
24}
25
26static int value(yyscan_t scanner)
27{
28 YYSTYPE *yylval = expr_get_lval(scanner);
29 char *text = expr_get_text(scanner);
30
31 return __value(yylval, text, NUMBER);
32}
33
34/*
35 * Allow @ instead of / to be able to specify pmu/event/ without
36 * conflicts with normal division.
37 */
38static char *normalize(char *str, int runtime)
39{
40 char *ret = str;
41 char *dst = str;
42
43 while (*str) {
44 if (*str == '@')
45 *dst++ = '/';
46 else if (*str == '\\')
47 *dst++ = *++str;
48 else if (*str == '?') {
49 char *paramval;
50 int i = 0;
51 int size = asprintf(¶mval, "%d", runtime);
52
53 if (size < 0)
54 *dst++ = '0';
55 else {
56 while (i < size)
57 *dst++ = paramval[i++];
58 free(paramval);
59 }
60 }
61 else
62 *dst++ = *str;
63 str++;
64 }
65
66 *dst = 0x0;
67 return ret;
68}
69
70static int str(yyscan_t scanner, int token, int runtime)
71{
72 YYSTYPE *yylval = expr_get_lval(scanner);
73 char *text = expr_get_text(scanner);
74
75 yylval->str = normalize(strdup(text), runtime);
76 if (!yylval->str)
77 return EXPR_ERROR;
78
79 yylval->str = normalize(yylval->str, runtime);
80 return token;
81}
82%}
83
84number ([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)
85
86sch [-,=]
87spec \\{sch}
88sym [0-9a-zA-Z_\.:@?]+
89symbol ({spec}|{sym})+
90
91%%
92 struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
93
94 {
95 int start_token = sctx->start_token;
96
97 if (sctx->start_token) {
98 sctx->start_token = 0;
99 return start_token;
100 }
101 }
102
103d_ratio { return D_RATIO; }
104max { return MAX; }
105min { return MIN; }
106if { return IF; }
107else { return ELSE; }
108#smt_on { return SMT_ON; }
109{number} { return value(yyscanner); }
110{symbol} { return str(yyscanner, ID, sctx->runtime); }
111"|" { return '|'; }
112"^" { return '^'; }
113"&" { return '&'; }
114"<" { return '<'; }
115">" { return '>'; }
116"-" { return '-'; }
117"+" { return '+'; }
118"*" { return '*'; }
119"/" { return '/'; }
120"%" { return '%'; }
121"(" { return '('; }
122")" { return ')'; }
123"," { return ','; }
124. { }
125%%
126
127int expr_wrap(void *scanner __maybe_unused)
128{
129 return 1;
130}