Calculator
Step 3
|
#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 (node &result) |
Private Member Functions | |
std::string | charify (char c) |
bool | get_number (std::string const &token, node &result) |
bool | get_add_expr (node &result) |
bool | get_mul_expr (node &result) |
bool | get_primary (node &result) |
bool | get_unary (node &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 36 of file parse.hpp.
parser::parser | ( | std::istream & | input | ) |
Constructor. Save the input
stream.
input | The input stream |
Definition at line 6 of file parse.cpp.
|
private |
Convert a characer to a readable form.
c | The character |
c
is readable. Definition at line 13 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 178 of file parse.cpp.
References get_mul_expr(), get_token(), and push_back().
Referenced by get_expr().
bool parser::get_expr | ( | node & | result | ) |
Read one expression and store the parse tree in result
.
result | Where to store the parse tree. |
parse_error | for various syntax and other errors |
Definition at line 143 of file parse.cpp.
References eof, get_add_expr(), get_token(), identifier, and push_back().
Referenced by get_primary().
|
private |
Parse an identifer.
identifier | Store the identifier here. |
Definition at line 37 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 200 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 129 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 244 of file parse.cpp.
References eof, get_expr(), get_number(), get_token(), 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 56 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 222 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 129 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 124 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 134 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 139 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 being pushed back |
Definition at line 118 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 141 of file parse.hpp.
Referenced by get_identifier(), and get_token().
|
private |
The kind of token that was pushed back.
Definition at line 144 of file parse.hpp.
Referenced by get_token(), and push_back().
|
private |
One token push-back.
Definition at line 143 of file parse.hpp.
Referenced by get_token(), and push_back().