| 1 |
In tt(mfcalc), the parser's member function tt(lex()) must now recognize |
| 2 |
variables, function names, numeric values, and the single-character arithmetic |
| 3 |
operators. Strings of alphanumeric characters not starting with a digit are |
| 4 |
recognized as either variables or functions depending on the table in which |
| 5 |
they are found. By arranging tt(lex)'s logic such that the function table is |
| 6 |
searched first it is simple to ensure that no variable can ever have the name |
| 7 |
of a predefined function. The implementation used here, in which two |
| 8 |
different tables are used for the arithmetic functions and the variable |
| 9 |
symbols is appealing because it's simple to implement. However, it also has |
| 10 |
the drawback of being difficult to scale to more generic calculators, using, |
| 11 |
e.g., different data types and different types of functions. In such |
| 12 |
situations a single symbol table is more preferable, where the keys are the |
| 13 |
identifiers (variables, function names, predefined constants, etc.) while the |
| 14 |
values are objects describing their characteristics. A re-implementation of |
| 15 |
tt(mfcalc) using an integrated symbol table is suggested in one of the |
| 16 |
exercises of the next section ref(EXERCISES). |
| 17 |
|
| 18 |
The parser's tt(lex) member has these characteristics: |
| 19 |
itemization( |
| 20 |
it() All leading blanks and tabs are skipped |
| 21 |
it() If no (other) character could be obtained 0 is returned, indicating |
| 22 |
End-Of-File. |
| 23 |
it() If the first non-blank character is a dot or number, a number is |
| 24 |
extracted from the standard input. Since the semantic value data |
| 25 |
member of tt(mfcalc)'s parser (tt(d_val)) is itself also a tt(union), |
| 26 |
the numerical value can be extracted into tt(d_val_.u_val), and a |
| 27 |
tt(NUM) token can be returned. |
| 28 |
it() If the first non-blank character is not a letter, then a |
| 29 |
single-character token was received and the character's value is |
| 30 |
returned as the next token. |
| 31 |
it() Otherwise the read character is a letter. This character and all |
| 32 |
subsequent alpha-numeric characters are extracted to construct the |
| 33 |
name of an identifier. Then this identifier is searched for in the |
| 34 |
tt(s_functions) map. If found, tt(d_val_.u_fun) is given the function's |
| 35 |
address, found as the value of the tt(s_functions) map element |
| 36 |
corresponding to the read identifier, and token tt(FNCT) is returned. |
| 37 |
If the symbol is not found in tt(s_functions) the address of the |
| 38 |
value ofn tt(d_symbols) associated with the received identifier is |
| 39 |
assigned to tt(d_val_.u_symbol) and token tt(VAR) is returned. Note |
| 40 |
that this automatically defines newly used variables, since |
| 41 |
tt(d_symbols[name]) automatically inserts a new element in a map if |
| 42 |
tt(d_symbol[name]) wasn't already there. |
| 43 |
) |
| 44 |
Here is the parser's tt(lex) member function: |
| 45 |
verbinclude(mfcalc/parser/lex.cc) |