mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
examples: bistromathic: don't use Flex
This example will soon use GNU readline, so its scanner should be easy to use (concurrently) on strings, not streams. This is not a place where Flex shines, and anyway, these are examples of Bison, not Flex. There's already lexcalc and reccalc that demonstrate the use of Flex. * examples/c/bistromathic/scan.l: Remove. * examples/c/bistromathic/parse.y (yylex): New. Adjust dependencies.
This commit is contained in:
@@ -31,10 +31,7 @@
|
||||
}
|
||||
|
||||
%code provides {
|
||||
#define YY_DECL \
|
||||
int yylex (YYSTYPE *yylval, YYLTYPE *yylloc)
|
||||
YY_DECL;
|
||||
|
||||
int yylex (YYSTYPE *yylval, YYLTYPE *yylloc);
|
||||
void yyerror (YYLTYPE *yylloc, char const *);
|
||||
}
|
||||
|
||||
@@ -137,6 +134,10 @@ exp:
|
||||
// End of grammar.
|
||||
%%
|
||||
|
||||
/*------------.
|
||||
| Functions. |
|
||||
`------------*/
|
||||
|
||||
struct init
|
||||
{
|
||||
char const *name;
|
||||
@@ -189,6 +190,80 @@ getsym (char const *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*----------.
|
||||
| Scanner. |
|
||||
`----------*/
|
||||
|
||||
int
|
||||
yylex (YYSTYPE *yylval, YYLTYPE *yylloc)
|
||||
{
|
||||
int c;
|
||||
|
||||
// Ignore white space, get first nonwhite character.
|
||||
do {
|
||||
// Move the first position onto the last.
|
||||
yylloc->first_line = yylloc->last_line;
|
||||
yylloc->first_column = yylloc->last_column;
|
||||
|
||||
yylloc->last_column += 1;
|
||||
c = getchar ();
|
||||
} while (c == ' ' || c == '\t');
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '+': return TOK_PLUS;
|
||||
case '-': return TOK_MINUS;
|
||||
case '*': return TOK_STAR;
|
||||
case '/': return TOK_SLASH;
|
||||
case '^': return TOK_CARET;
|
||||
case '=': return TOK_EQUAL;
|
||||
case '(': return TOK_LPAREN;
|
||||
case ')': return TOK_RPAREN;
|
||||
|
||||
case '\n':
|
||||
yylloc->last_column = 1;
|
||||
yylloc->last_line += 1;
|
||||
return TOK_EOL;
|
||||
|
||||
case EOF: return TOK_EOF;
|
||||
|
||||
// Any other character is a token by itself.
|
||||
default:
|
||||
if (c == '.' || isdigit (c))
|
||||
{
|
||||
ungetc (c, stdin);
|
||||
int nchars = 0;
|
||||
scanf ("%lf%n", &yylval->TOK_NUM, &nchars);
|
||||
yylloc->last_column += nchars - 1;
|
||||
return TOK_NUM;
|
||||
}
|
||||
else if (islower (c))
|
||||
{
|
||||
ungetc (c, stdin);
|
||||
int nchars = 0;
|
||||
char buf[100];
|
||||
scanf ("%99[a-z]%n", buf, &nchars);
|
||||
symrec *s = getsym (buf);
|
||||
if (!s)
|
||||
s = putsym (buf, TOK_VAR);
|
||||
yylval->TOK_VAR = s;
|
||||
yylloc->last_column += nchars - 1;
|
||||
return s->type;
|
||||
}
|
||||
else
|
||||
{
|
||||
yyerror (yylloc, "error: invalid character");
|
||||
return yylex (yylval, yylloc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*---------.
|
||||
| Parser. |
|
||||
`---------*/
|
||||
|
||||
int
|
||||
yyreport_syntax_error (const yyparse_context_t *ctx)
|
||||
{
|
||||
@@ -215,6 +290,11 @@ void yyerror (YYLTYPE *loc, char const *msg)
|
||||
fprintf (stderr, ": %s\n", msg);
|
||||
}
|
||||
|
||||
|
||||
/*-------.
|
||||
| Main. |
|
||||
`-------*/
|
||||
|
||||
int main (int argc, char const* argv[])
|
||||
{
|
||||
// Enable parse traces on option -p.
|
||||
|
||||
Reference in New Issue
Block a user