[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
examples: bistro: don't be lazy with switch
From: |
Akim Demaille |
Subject: |
examples: bistro: don't be lazy with switch |
Date: |
Tue, 14 Apr 2020 08:20:00 +0200 |
commit 6fe8b5f7d1388e02a052f34962da6999cca0999b
Author: Akim Demaille <address@hidden>
Date: Tue Apr 14 07:59:51 2020 +0200
examples: bistro: don't be lazy with switch
* examples/c/bistromathic/parse.y (yylex): Use the switch to
discriminate all the cases.
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);
}
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- examples: bistro: don't be lazy with switch,
Akim Demaille <=