(此版本为过程式,对应于《The C++ Programming Language 3rd》第六章)
表达式语法分析如下(递归定义):
*****************
* program:
* END //END is end-of-input
* expr_list END
* expr_list:
* expression PRINT // PRINT is semicolon
* expression PRINT expr_list
* expression:
* expression + term
* expression - term
* term
* term:
* term / primary
* term * primary
* primary
* primary:
* NUMBER
* NAME // this is variable
* NAME = expression
* - primary
* ( expression )
* *****************
- calc.h
- calc.cpp
- main.cpp
calc.h
/**
* calc.h
*****************
* program:
* END //END is end-of-input
* expr_list END
* expr_list:
* expression PRINT // PRINT is semicolon
* expression PRINT expr_list
* expression:
* expression + term
* expression - term
* term
* term:
* term / primary
* term * primary
* primary
* primary:
* NUMBER
* NAME // this is variable
* NAME = expression
* - primary
* ( expression )
*/
#ifndef CALC_H_
#define CALC_H_
#include <map>
#include <string>
using namespace std;
enum Token_value {
NAME, NUMBER, END,
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'
};
// for DRIVER PROGRAM
extern Token_value curr_tok;
extern map<string, double> table; // var table
extern int no_of_errors;
extern double expr(bool get);
extern Token_value get_token();
extern double error(const string& s);
// END for DRIVER PROGRAM
extern double term(bool get);
extern double prim(bool get);
#endif
calc.cpp
// calc.cpp
#include "calc.h"
#include <cctype>
#include <iostream>
#include <map>
#include <string>
using namespace std;
// error function
int no_of_errors;
double