diff options
author | Martin Pieuchot <mpi@cvs.openbsd.org> | 2020-04-24 15:10:42 +0000 |
---|---|---|
committer | Martin Pieuchot <mpi@cvs.openbsd.org> | 2020-04-24 15:10:42 +0000 |
commit | b987f3beb1725c8a58993f4caf2fa17aa8e6e581 (patch) | |
tree | 17b76e8c0679c229655da8d37de15a1460a0706f | |
parent | 4ea2a05a5670f9f7c47afefc064d99c2404276f6 (diff) |
Simplify the grammar by letting the lexer handle builtin keywords.
Also clarify various constructs by using commonly understood names like
'expr' and 'vargs'.
-rw-r--r-- | usr.sbin/btrace/bt_parse.y | 181 |
1 files changed, 71 insertions, 110 deletions
diff --git a/usr.sbin/btrace/bt_parse.y b/usr.sbin/btrace/bt_parse.y index 44067875cba..1eb54353cd9 100644 --- a/usr.sbin/btrace/bt_parse.y +++ b/usr.sbin/btrace/bt_parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: bt_parse.y,v 1.12 2020/04/23 18:36:51 mpi Exp $ */ +/* $OpenBSD: bt_parse.y,v 1.13 2020/04/24 15:10:41 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot <mpi@openbsd.org> @@ -67,7 +67,7 @@ struct bt_stmt *bv_set(const char *, struct bt_arg *); struct bt_arg *bm_get(const char *, struct bt_arg *); struct bt_stmt *bm_set(const char *, struct bt_arg *, struct bt_arg *); -struct bt_stmt *bm_fn(enum bt_action, struct bt_arg *, struct bt_arg *); +struct bt_stmt *bm_op(enum bt_action, struct bt_arg *, struct bt_arg *); /* * Lexer @@ -100,20 +100,19 @@ static int yylex(void); %token ERROR OP_EQ OP_NEQ BEGIN END /* Builtins */ -%token ARG0 ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 -%token COMM CPU HZ KSTACK USTACK NSECS PID RETVAL TID -/* Functions */ -%token F_CLEAR F_DELETE F_EXIT F_PRINT F_PRINTF F_TIME F_ZERO -/* Map functions */ -%token M_COUNT M_MAX M_MIN M_SUM +%token BUILTIN PID TID +/* Functions and Map operators */ +%token F_DELETE FUNC0 FUNC1 FUNCN MOP0 MOP1 %token <v.string> STRING CSTRING %token <v.number> NUMBER + %type <v.string> gvar -%type <v.i> filterval oper builtin fn0 fn1 fnN mfn0 mfn1 mfnN +%type <v.i> filterval oper builtin +%type <v.i> BUILTIN F_DELETE FUNC0 FUNC1 FUNCN MOP0 MOP1 %type <v.probe> probe %type <v.filter> predicate %type <v.stmt> action stmt stmtlist -%type <v.arg> arg arglist map marg term +%type <v.arg> expr vargs map mexpr term %type <v.rtype> beginend %left '+' '-' @@ -154,45 +153,16 @@ predicate : /* empty */ { $$ = NULL; } builtin : PID { $$ = B_AT_BI_PID; } | TID { $$ = B_AT_BI_TID; } - | COMM { $$ = B_AT_BI_COMM; } - | CPU { $$ = B_AT_BI_CPU; } - | NSECS { $$ = B_AT_BI_NSECS; } - | KSTACK { $$ = B_AT_BI_KSTACK; } - | USTACK { $$ = B_AT_BI_USTACK; } - | ARG0 { $$ = B_AT_BI_ARG0; } - | ARG1 { $$ = B_AT_BI_ARG1; } - | ARG2 { $$ = B_AT_BI_ARG2; } - | ARG3 { $$ = B_AT_BI_ARG3; } - | ARG4 { $$ = B_AT_BI_ARG4; } - | ARG5 { $$ = B_AT_BI_ARG5; } - | ARG6 { $$ = B_AT_BI_ARG6; } - | ARG7 { $$ = B_AT_BI_ARG7; } - | ARG8 { $$ = B_AT_BI_ARG8; } - | ARG9 { $$ = B_AT_BI_ARG9; } - | RETVAL { $$ = B_AT_BI_RETVAL; } - ; - -fn0 : F_EXIT { $$ = B_AC_EXIT; } + | BUILTIN { $$ = $1; } ; -fn1 : F_CLEAR { $$ = B_AC_CLEAR; } - | F_TIME { $$ = B_AC_TIME; } - | F_ZERO { $$ = B_AC_ZERO; } +mexpr : MOP0 '(' ')' { $$ = ba_new(NULL, $1); } + | MOP1 '(' expr ')' { $$ = ba_new($3, $1); } + | expr { $$ = $1; } ; -fnN : F_PRINTF { $$ = B_AC_PRINTF; } - | F_PRINT { $$ = B_AC_PRINT; } - ; - -mfn0 : M_COUNT { $$ = B_AT_MF_COUNT; } - ; - -mfn1 : F_DELETE { $$ = B_AC_DELETE; } - ; - -mfnN : M_MAX { $$ = B_AT_MF_MAX; } - | M_MIN { $$ = B_AT_MF_MIN; } - | M_SUM { $$ = B_AT_MF_SUM; } +expr : CSTRING { $$ = ba_new($1, B_AT_STR); } + | term ; term : '(' term ')' { $$ = $2; } @@ -204,37 +174,28 @@ term : '(' term ')' { $$ = $2; } | builtin { $$ = ba_new(NULL, $1); } | gvar { $$ = bv_get($1); } | map { $$ = $1; } - ; + gvar : '@' STRING { $$ = $2; } | '@' { $$ = UNNAMED_MAP; } -map : gvar '[' arglist ']' { $$ = bm_get($1, $3); } - ; - -marg : arg { $$ = $1; } - | mfn0 '(' ')' { $$ = ba_new(NULL, $1); } - | mfnN '(' arg ')' { $$ = ba_new($3, $1); } - ; - -arg : CSTRING { $$ = ba_new($1, B_AT_STR); } - | term +map : gvar '[' vargs ']' { $$ = bm_get($1, $3); } ; -arglist : arg - | arglist ',' arg { $$ = ba_append($1, $3); } +vargs : expr + | vargs ',' expr { $$ = ba_append($1, $3); } ; NL : /* empty */ | '\n' ; stmt : ';' NL { $$ = NULL; } - | gvar '=' arg { $$ = bv_set($1, $3); } - | gvar '[' arglist ']' '=' marg { $$ = bm_set($1, $3, $6); } - | fnN '(' arglist ')' { $$ = bs_new($1, $3, NULL); } - | fn1 '(' arg ')' { $$ = bs_new($1, $3, NULL); } - | fn0 '(' ')' { $$ = bs_new($1, NULL, NULL); } - | mfn1 '(' map ')' { $$ = bm_fn($1, $3, NULL); } + | gvar '=' expr { $$ = bv_set($1, $3); } + | gvar '[' vargs ']' '=' mexpr { $$ = bm_set($1, $3, $6); } + | FUNCN '(' vargs ')' { $$ = bs_new($1, $3, NULL); } + | FUNC1 '(' expr ')' { $$ = bs_new($1, $3, NULL); } + | FUNC0 '(' ')' { $$ = bs_new($1, NULL, NULL); } + | F_DELETE '(' map ')' { $$ = bm_op($1, $3, NULL); } ; stmtlist : stmt @@ -474,7 +435,7 @@ bv_get(const char *vname) } struct bt_stmt * -bm_fn(enum bt_action mact, struct bt_arg *ba, struct bt_arg *mval) +bm_op(enum bt_action mact, struct bt_arg *ba, struct bt_arg *mval) { return bs_new(mact, ba, (struct bt_var *)mval); } @@ -509,6 +470,7 @@ bm_get(const char *mname, struct bt_arg *mkey) struct keyword { const char *word; int token; + int type; }; int @@ -517,53 +479,47 @@ kw_cmp(const void *str, const void *xkw) return (strcmp(str, ((const struct keyword *)xkw)->word)); } -int +struct keyword * lookup(char *s) { static const struct keyword kws[] = { - { "!=", OP_NEQ }, - { "==", OP_EQ }, - { "BEGIN", BEGIN }, - { "END", END }, - { "arg0", ARG0 }, - { "arg1", ARG1 }, - { "arg2", ARG2 }, - { "arg3", ARG3 }, - { "arg4", ARG4 }, - { "arg5", ARG5 }, - { "arg6", ARG6 }, - { "arg7", ARG7 }, - { "arg8", ARG8 }, - { "arg9", ARG9 }, - { "clear", F_CLEAR }, - { "comm", COMM }, - { "count", M_COUNT }, - { "cpu", CPU }, - { "delete", F_DELETE }, - { "exit", F_EXIT }, - { "hz", HZ }, - { "kstack", KSTACK }, - { "max", M_MAX }, - { "min", M_MIN }, - { "nsecs", NSECS }, - { "pid", PID }, - { "print", F_PRINT }, - { "printf", F_PRINTF }, - { "retval", RETVAL }, - { "sum", M_SUM }, - { "tid", TID }, - { "time", F_TIME }, - { "ustack", USTACK }, - { "zero", F_ZERO }, + { "!=", OP_NEQ, 0 }, + { "==", OP_EQ, 0 }, + { "BEGIN", BEGIN, 0 }, + { "END", END, 0 }, + { "arg0", BUILTIN, B_AT_BI_ARG0 }, + { "arg1", BUILTIN, B_AT_BI_ARG1 }, + { "arg2", BUILTIN, B_AT_BI_ARG2 }, + { "arg3", BUILTIN, B_AT_BI_ARG3 }, + { "arg4", BUILTIN, B_AT_BI_ARG4 }, + { "arg5", BUILTIN, B_AT_BI_ARG5 }, + { "arg6", BUILTIN, B_AT_BI_ARG6 }, + { "arg7", BUILTIN, B_AT_BI_ARG7 }, + { "arg8", BUILTIN, B_AT_BI_ARG8 }, + { "arg9", BUILTIN, B_AT_BI_ARG9 }, + { "clear", FUNC1, B_AC_CLEAR }, + { "comm", BUILTIN, B_AT_BI_COMM }, + { "count", MOP0, B_AT_MF_COUNT }, + { "cpu", BUILTIN, B_AT_BI_CPU }, + { "delete", F_DELETE, B_AC_DELETE }, + { "exit", FUNC0, B_AC_EXIT }, + { "hz", HZ, 0 }, + { "kstack", BUILTIN, B_AT_BI_KSTACK }, + { "max", MOP1, B_AT_MF_MAX }, + { "min", MOP1, B_AT_MF_MIN }, + { "nsecs", BUILTIN, B_AT_BI_NSECS }, + { "pid", PID, 0 /*B_AT_BI_PID*/ }, + { "print", FUNCN, B_AC_PRINT }, + { "printf", FUNCN, B_AC_PRINTF }, + { "retval", BUILTIN, B_AT_BI_RETVAL }, + { "sum", MOP1, B_AT_MF_SUM }, + { "tid", TID, 0 /*B_AT_BI_TID*/ }, + { "time", FUNC1, B_AC_TIME }, + { "ustack", BUILTIN, B_AT_BI_USTACK }, + { "zero", FUNC1, B_AC_ZERO }, }; - const struct keyword *kw; - kw = bsearch(s, kws, nitems(kws), sizeof(kws[0]), kw_cmp); - if (kw != NULL) { - return kw->token; - } else { - return STRING; - } + return bsearch(s, kws, nitems(kws), sizeof(kws[0]), kw_cmp); } int @@ -602,7 +558,7 @@ yylex(void) { unsigned char buf[1024]; unsigned char *ebuf, *p, *str; - int c, token; + int c; ebuf = buf + sizeof(buf); p = buf; @@ -734,6 +690,7 @@ again: /* parsing next word */ if (allowed_in_string(c)) { + struct keyword *kwp; do { *p++ = c; if (p == ebuf) { @@ -743,10 +700,14 @@ again: } while ((c = lgetc()) != EOF && (allowed_in_string(c))); lungetc(); *p = '\0'; - if ((token = lookup(buf)) == STRING) + kwp = lookup(buf); + if (kwp == NULL) { if ((yylval.v.string = strdup(buf)) == NULL) err(1, "%s", __func__); - return token; + return STRING; + } + yylval.v.i = kwp->type; + return kwp->token; } if (c == '\n') { |