The Parser Class. McGraw-Hill The Complete Reference Fourth Edition
Publicité
Publicité
968
C + + : T h e C o m p l e t e R e f e r e n c e
The Parser Class
The expression parser is built upon the parser class. The first version of parser is shown here. Subsequent versions of the parser build upon it.
class parser { char *exp_ptr; // points to the expression char token[80]; // holds current token char tok_type; // holds token's type void eval_exp2(double &result); void eval_exp3(double &result); void eval_exp4(double &result); void eval_exp5(double &result); void eval_exp6(double &result); void atom(double &result); void get_token(); void serror(int error); int isdelim(char c); public:
}; parser(); double eval_exp(char *exp);
The parser class contains three private member variables. The expression to be evaluated is contained in a null-terminated string pointed to by exp_ptr. Thus, the parser evaluates expressions that are contained in standard ASCII strings. For example, the following strings contain expressions that the parser can evaluate:
"10
− 5"
"2 * 3.3 / (3.1416 * 3.3)"
When the parser begins execution, exp_ptr must point to the first character in the expression string. As the parser executes, it works its way through the string until the null-terminator is encountered.
The meaning of the other two member variables, token and tok_type, are described in the next section.
The entry point to the parser is through eval_exp( ), which must be called with a pointer to the expression to be analyzed. The functions eval_exp2( ) through eval_exp6( ) along with atom( ) form the recursive-descent parser. They implement an enhanced set of the expression production rules discussed earlier. In subsequent versions of the parser, a function called eval_exp1( ) will also be added.
Télécharger
Publicité