|
Calculator
Step 4
|
#include <parse.hpp>
Public Types | |
| enum | kind : int { eof, identifier, number, plus ='+', minus ='-', times ='*', slash ='/', lparen = '(', rparen =')', equal ='=', comma =',' } |
Public Member Functions | |
| parser (std::istream &input) | |
| bool | get_statement (std::ostream &output) |
Private Member Functions | |
| std::string | charify (char c) |
| bool | get_number (std::string const &token, node &result) |
| bool | get_expr (node &result) |
| bool | get_add_expr (node &result) |
| bool | get_mul_expr (node &result) |
| bool | get_primary (node &result) |
| bool | get_unary (node &result) |
| void | get_definition (std::string &name, identifier_list ¶meters, node &definition) |
| kind | get_token (std::string &token) |
| void | get_identifier (std::string &identifier) |
| void | get_expr_list (node_list &result) |
| template<class OutputIterator > | |
| OutputIterator | get_namelist (OutputIterator output) |
| 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 | |
| comma | |
Definition at line 31 of file parse.hpp.
| parser::parser | ( | std::istream & | input | ) |
Constructor. Save the input stream.
| input | The input stream |
Definition at line 10 of file parse.cpp.
|
private |
Convert a characer to a readable form.
| c | The character |
c is readable. Definition at line 17 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 213 of file parse.cpp.
References get_mul_expr(), get_token(), and push_back().
Referenced by get_expr().
|
private |
Parse a function or variable definition A variable is just like a function that takes no parameters.
DEFINITION ::= DEF IDENTIFIER OPT_PARAMETERS '=' EXPR
OPT_PARAMETERS ::= emtpy | '(' OPT_IDENTIFIER_LIST ')'
OPT_IDENTIFIER_LIST ::= empty | IDENTIFIER_LIST
IDENTIFIER_LIST ::= IDENTIFIER | IDENTIFIER_LIST ',' IDENTIFIER
| [out] | name | Store the variable or function name here |
| [out] | parameters | Store the list of parameter names here |
| [out] | definition | Store the definition expression here |
Definition at line 157 of file parse.cpp.
References get_expr(), get_namelist(), get_token(), and identifier.
Referenced by get_statement().
|
private |
Parse an expression
| result | Store the result here |
Definition at line 208 of file parse.cpp.
References get_add_expr().
Referenced by get_definition(), get_expr_list(), get_primary(), and get_statement().
|
private |
Parse a comma-separated expression list.
| [out] | result | Store the result here |
Definition at line 272 of file parse.cpp.
References get_expr(), get_token(), and push_back().
Referenced by get_primary().
|
private |
Parse an identifer.
| identifier | Store the identifier here. |
Definition at line 41 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 232 of file parse.cpp.
References get_token(), get_unary(), and push_back().
Referenced by get_add_expr().
|
private |
Parse a list of parameter names. Names are identifiers, separated by commas. The list can be empty. This is a template so the container type is unimportant. Any output iterator will do.
| [out] | output | Store the identifiers here |
output after storing all the identifiers Definition at line 193 of file parse.hpp.
References get_token(), and identifier.
Referenced by get_definition().
|
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 146 of file parse.cpp.
Referenced by get_primary().
|
private |
Parse a primary expression.
PRIMARY ::= NUMBER | IDENTIFIER | '(' EXPR ')' | FUNCTION_CALL
FUNCTION_CALL ::= IDENTIFIER '(' OPT_EXPR_LIST ')'
OPT_EXPR_LIST ::= empty | EXPR_LIST
EXPR_LIST ::= EXPR | EXPR_LIST ',' EXPR
| result | Store the result here |
Definition at line 293 of file parse.cpp.
References eof, get_expr(), get_expr_list(), get_number(), get_token(), identifier, number, and push_back().
Referenced by get_unary().
| bool parser::get_statement | ( | std::ostream & | output | ) |
Read one statement and store the parse tree in result. If the statement is an assignment or function definition, store the variable or function. If the statement is an expression, print the result to output.
STATEMENT ::= DEFINITION | QUIT | EXPR
| output | The output stream. |
| parse_error | for various syntax and other errors |
Definition at line 178 of file parse.cpp.
References eof, node::evaluate(), get_definition(), get_expr(), get_token(), identifier, push_back(), and set_function().
|
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 69 of file parse.cpp.
References charify(), eof, get_identifier(), identifier, input_, isalpha(), kind_, number, and token_.
Referenced by get_add_expr(), get_definition(), get_expr_list(), get_mul_expr(), get_namelist(), get_primary(), get_statement(), and get_unary().
|
private |
Parse a unary expression.
UNARY ::= '-' PRIMARY | '+' PRIMARY | PRIMARY
| result | Store the result here |
Definition at line 251 of file parse.cpp.
References eof, 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 167 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 162 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 172 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 177 of file parse.hpp.
References ctype_.
Referenced by charify().
|
private |
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 60 of file parse.cpp.
References eof, kind_, and token_.
Referenced by get_add_expr(), get_expr_list(), get_mul_expr(), get_primary(), get_statement(), and get_unary().
|
private |
|
private |
Share the input stream.
Definition at line 179 of file parse.hpp.
Referenced by get_identifier(), and get_token().
|
private |
The kind of token that was pushed back.
Definition at line 182 of file parse.hpp.
Referenced by get_token(), and push_back().
|
private |
One token push-back.
Definition at line 181 of file parse.hpp.
Referenced by get_token(), and push_back().
1.8.5