mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-20 09:43:03 +00:00
examples: bistro: don't be lazy with switch
* examples/c/bistromathic/parse.y (yylex): Use the switch to discriminate all the cases.
This commit is contained in:
@@ -235,43 +235,49 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
|
|||||||
case '(': return TOK_LPAREN;
|
case '(': return TOK_LPAREN;
|
||||||
case ')': return TOK_RPAREN;
|
case ')': return TOK_RPAREN;
|
||||||
|
|
||||||
case 0: return TOK_YYEOF;
|
case '\0': return TOK_YYEOF;
|
||||||
|
|
||||||
default:
|
|
||||||
// Numbers.
|
// Numbers.
|
||||||
if (c == '.' || isdigit (c))
|
case '.':
|
||||||
{
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
int nchars = 0;
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars);
|
{
|
||||||
|
int nchars = 0;
|
||||||
|
sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars);
|
||||||
*line += nchars - 1;
|
*line += nchars - 1;
|
||||||
yylloc->last_column += nchars - 1;
|
yylloc->last_column += nchars - 1;
|
||||||
return TOK_NUM;
|
return TOK_NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identifiers.
|
// Identifiers.
|
||||||
else if (islower (c))
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
||||||
{
|
case 'f': case 'g': case 'h': case 'i': case 'j':
|
||||||
int nchars = 0;
|
case 'k': case 'l': case 'm': case 'n': case 'o':
|
||||||
char buf[100];
|
case 'p': case 'q': case 'r': case 's': case 't':
|
||||||
sscanf (*line - 1, "%99[a-z]%n", buf, &nchars);
|
case 'u': case 'v': case 'w': case 'x': case 'y':
|
||||||
*line += nchars - 1;
|
case 'z':
|
||||||
yylloc->last_column += nchars - 1;
|
{
|
||||||
if (strcmp (buf, "exit") == 0)
|
int nchars = 0;
|
||||||
return TOK_EXIT;
|
char buf[100];
|
||||||
else
|
sscanf (*line - 1, "%99[a-z]%n", buf, &nchars);
|
||||||
{
|
*line += nchars - 1;
|
||||||
symrec *s = getsym (buf);
|
yylloc->last_column += nchars - 1;
|
||||||
if (!s)
|
if (strcmp (buf, "exit") == 0)
|
||||||
s = putsym (buf, TOK_VAR);
|
return TOK_EXIT;
|
||||||
yylval->TOK_VAR = s;
|
else
|
||||||
return s->type;
|
{
|
||||||
}
|
symrec *s = getsym (buf);
|
||||||
}
|
if (!s)
|
||||||
|
s = putsym (buf, TOK_VAR);
|
||||||
|
yylval->TOK_VAR = s;
|
||||||
|
return s->type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Stray characters.
|
// Stray characters.
|
||||||
else
|
default:
|
||||||
{
|
yyerror (yylloc, "error: invalid character");
|
||||||
yyerror (yylloc, "error: invalid character");
|
return yylex (line, yylval, yylloc);
|
||||||
return yylex (line, yylval, yylloc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user