|
Calculator
Step 2
|
#include <parse.hpp>
Public Types | |
| enum | kind : int { eof, identifier, number, plus ='+', minus ='-', times ='*', slash ='/', lparen = '(', rparen =')', equal ='=' } |
Public Member Functions | |
| parser (std::istream &input) | |
| bool | get_expr (double &result) |
Private Member Functions | |
| std::string | charify (char c) |
| bool | get_number (std::string const &token, double &result) |
| bool | get_add_expr (double &result) |
| bool | get_mul_expr (double &result) |
| bool | get_primary (double &result) |
| bool | get_unary (double &result) |
| kind | get_token (std::string &token) |
| void | get_identifier (std::string &identifier) |
| void | push_back (std::string const &token, kind k) |
| bool | isalpha (char c) const |
| bool | isalnum (char c) const |
| bool | isdigit (char c) const |
| bool | isprint (char c) const |
Private Attributes | |
| std::istream & | input_ |
| Share the input stream. More... | |
| std::ctype< char > const & | ctype_ |
| Cache the ctype facet for checking character categories. More... | |
| std::string | token_ |
| One token push-back. More... | |
| kind | kind_ |
| The kind of token that was pushed back. More... | |
Parser class template. The parser reads tokens from an input stream. A token can be a keyword, numeric literal, identifier, or symbol (operator or punctuator). Symbols can have multiple characters (e.g., :=).
Because the recursive-descent parser can examine too many tokens from the input stream, it keeps a push-back token. Once the parser knows it has gone too far, it pushes back the most recently read token. The next call to get_token() retrieves the pushed-back token.
Only one push-back is available, which limits the complexity of the syntax.
| enum parser::kind : int |
Token kind. Declare a name for each single-character token, to ensure the enumerated type can represent any operator or punctuator character.
| Enumerator | |
|---|---|
| eof | |
| identifier | |
| number | |
| plus | |
| minus | |
| times | |
| slash | |
| lparen | |
| rparen | |
| equal | |
Definition at line 37 of file parse.hpp.
| parser::parser | ( | std::istream & | input | ) |
Constructor. Save the input stream.
| input | The input stream |
Definition at line 5 of file parse.cpp.
|
private |
Convert a characer to a readable form.
| c | The character |
c is readable. Definition at line 12 of file parse.cpp.
References isprint().
Referenced by get_identifier(), and get_token().
|
private |
Parse an addition expression
ADD_EXPR ::= MUL_EXPR | ADD_EXPR + MUL_EXPR | ADD_EXPR - MUL_EXPR
| result | Store the result here |
Definition at line 174 of file parse.cpp.
References get_mul_expr(), get_token(), and push_back().
Referenced by get_expr().
| bool parser::get_expr | ( | double & | result | ) |
Read one expression and store the result in result.
| result | Where to store the result of the expression. |
| parse_error | for various syntax and other errors |
Definition at line 142 of file parse.cpp.
References eof, get_add_expr(), get_token(), identifier, push_back(), and set_variable().
Referenced by get_primary(), and parse_loop().
|
private |
Parse an identifer.
| identifier | Store the identifier here. |
Definition at line 36 of file parse.cpp.
References charify(), input_, isalnum(), and isalpha().
Referenced by get_token().
|
private |
Parse a multiplicative expression.
MUL_EXPR ::= UNARY | MUL_EXPR + UNARY | MUL_EXPR - UNARY
| result | Store the result here |
Definition at line 196 of file parse.cpp.
References get_token(), get_unary(), and push_back().
Referenced by get_add_expr().
|
private |
Parse a floating number.
| token | The token to parse |
| result | Store the number here |
token is a valid number or false for an error Definition at line 135 of file parse.cpp.
Referenced by get_primary().
|
private |
Parse a primary expression.
PRIMARY ::= NUMBER | IDENTIFIER | '(' EXPR ')'
| result | Store the result here |
Definition at line 239 of file parse.cpp.
References eof, get_expr(), get_number(), get_token(), get_variable(), identifier, and number.
Referenced by get_unary().
|
private |
Parse a token. A token can be a keyword, a literal or a symbol.
TOKEN ::= IDENTIFIER | NUMBER | SYMBOL
IDENTIIFER ::= ALPHA (ALPHA | DIGIT)*
NUMBER ::= DIGIT+ ('.' DIGITS+)? ('E' SIGN? DIGITS+)?
SYMBOL ::= '+' | '-' | '*' | '/' | '%' | '(' | ')' | '='
| token | Store the text of the token here. |
Definition at line 62 of file parse.cpp.
References charify(), eof, get_identifier(), identifier, input_, isalpha(), kind_, number, and token_.
Referenced by get_add_expr(), get_expr(), get_mul_expr(), get_primary(), and get_unary().
|
private |
Parse a unary expression.
UNARY ::= '-' PRIMARY | '+' PRIMARY | PRIMARY
| result | Store the result here |
Definition at line 220 of file parse.cpp.
References get_primary(), get_token(), and push_back().
Referenced by get_mul_expr().
|
inlineprivate |
Return true if c is alphanumeric. Use the locale of the input stream.
| c | The character to test. |
Definition at line 130 of file parse.hpp.
References ctype_.
Referenced by get_identifier().
|
inlineprivate |
Return true if c is alphabetic. Use the locale of the input stream.
| c | The character to test. |
Definition at line 125 of file parse.hpp.
References ctype_.
Referenced by get_identifier(), and get_token().
|
inlineprivate |
Return true if c is a digit. Use the locale of the input stream.
| c | The character to test. |
Definition at line 135 of file parse.hpp.
References ctype_.
|
inlineprivate |
Return true if c is printable. Use the locale of the input stream.
| c | The character to test. |
Definition at line 140 of file parse.hpp.
References ctype_.
Referenced by charify().
|
inlineprivate |
Push back a token. The next call to get_token() will return the pushed-back token.
| token | The token to push back. |
| k | The kind of token to push back |
Definition at line 119 of file parse.hpp.
Referenced by get_add_expr(), get_expr(), get_mul_expr(), and get_unary().
|
private |
|
private |
Share the input stream.
Definition at line 142 of file parse.hpp.
Referenced by get_identifier(), and get_token().
|
private |
The kind of token that was pushed back.
Definition at line 145 of file parse.hpp.
Referenced by get_token(), and push_back().
|
private |
One token push-back.
Definition at line 144 of file parse.hpp.
Referenced by get_token(), and push_back().
1.8.5