Next: The mfcalc
Main, Previous: The mfcalc
Symbol Table, Up: Multi-Function Calculator: mfcalc
[Contents][Index]
mfcalc
LexerThe function yylex
must now recognize variables, numeric values, and
the single-character arithmetic operators. Strings of alphanumeric
characters with a leading letter are recognized as either variables or
functions depending on what the symbol table says about them.
The string is passed to getsym
for look up in the symbol table. If
the name appears in the table, a pointer to its location and its type
(VAR
or FUN
) is returned to yyparse
. If it is not
already in the table, then it is installed as a VAR
using
putsym
. Again, a pointer and its type (which must be VAR
) is
returned to yyparse
.
No change is needed in the handling of numeric values and arithmetic
operators in yylex
.
#include <ctype.h> #include <stddef.h>
int yylex (void) { int c = getchar (); /* Ignore white space, get first nonwhite character. */ while (c == ' ' || c == '\t') c = getchar (); if (c == EOF) return YYEOF;
/* Char starts a number => parse the number. */ if (c == '.' || isdigit (c)) { ungetc (c, stdin); if (scanf ("%lf", &yylval.NUM) != 1) abort (); return NUM; }
Bison generated a definition of YYSTYPE
with a member named
NUM
to store value of NUM
symbols.
/* Char starts an identifier => read the name. */ if (isalpha (c)) { static ptrdiff_t bufsize = 0; static char *symbuf = 0;
ptrdiff_t i = 0; do
{ /* If buffer is full, make it bigger. */ if (bufsize <= i) { bufsize = 2 * bufsize + 40; symbuf = realloc (symbuf, (size_t) bufsize); } /* Add this character to the buffer. */ symbuf[i++] = (char) c; /* Get another character. */ c = getchar (); }
while (isalnum (c)); ungetc (c, stdin); symbuf[i] = '\0';
symrec *s = getsym (symbuf); if (!s) s = putsym (symbuf, VAR); yylval.VAR = s; /* or yylval.FUN = s. */ return s->type; } /* Any other character is a token by itself. */ return c; }
Next: The mfcalc
Main, Previous: The mfcalc
Symbol Table, Up: Multi-Function Calculator: mfcalc
[Contents][Index]