From fc18b4313a21b1f3f6b07398b091314ffbdd68c5 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 14 Apr 2020 07:59:51 +0200 Subject: [PATCH] examples: bistro: don't be lazy with switch * examples/c/bistromathic/parse.y (yylex): Use the switch to discriminate all the cases. --- examples/c/bistromathic/parse.y | 66 ++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y index 54002d29..a3b34c38 100644 --- a/examples/c/bistromathic/parse.y +++ b/examples/c/bistromathic/parse.y @@ -235,43 +235,49 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc) case '(': return TOK_LPAREN; case ')': return TOK_RPAREN; - case 0: return TOK_YYEOF; + case '\0': return TOK_YYEOF; - default: // Numbers. - if (c == '.' || isdigit (c)) - { - int nchars = 0; - sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars); + case '.': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + { + int nchars = 0; + sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars); *line += nchars - 1; yylloc->last_column += nchars - 1; return TOK_NUM; - } + } + // Identifiers. - else if (islower (c)) - { - int nchars = 0; - char buf[100]; - sscanf (*line - 1, "%99[a-z]%n", buf, &nchars); - *line += nchars - 1; - yylloc->last_column += nchars - 1; - if (strcmp (buf, "exit") == 0) - return TOK_EXIT; - else - { - symrec *s = getsym (buf); - if (!s) - s = putsym (buf, TOK_VAR); - yylval->TOK_VAR = s; - return s->type; - } - } + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'y': + case 'z': + { + int nchars = 0; + char buf[100]; + sscanf (*line - 1, "%99[a-z]%n", buf, &nchars); + *line += nchars - 1; + yylloc->last_column += nchars - 1; + if (strcmp (buf, "exit") == 0) + return TOK_EXIT; + else + { + symrec *s = getsym (buf); + if (!s) + s = putsym (buf, TOK_VAR); + yylval->TOK_VAR = s; + return s->type; + } + } + // Stray characters. - else - { - yyerror (yylloc, "error: invalid character"); - return yylex (line, yylval, yylloc); - } + default: + yyerror (yylloc, "error: invalid character"); + return yylex (line, yylval, yylloc); } }