cpp курсач2

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "stdafx.h"
#include <map>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <regex>
#include "parser_edsl.h"
enum TokType {
END_OF_TEXT = 0,
PRINT, READ, VARNAME, NUMBER, STRING,
PLUS = '+', MINUS = '-', MUL = '*', DIV = '/', SET = '=',
SEMICOLON = ';', COMMA = ',', LP = '(', RP = ')'
};
//double add_func1(double x, double y) {
// return x + y;
//}
//NTerm<TokType, double> E =
//Rule() << E << PLUS << Term<double>(NUMBER) << add_func1
//| Rule() << Term<double>(NUMBER);
//
//void print_val_func(double value) {
// printf("%f", value);
//}
//
//NTerm<TokType, void> P =
//Rule() << PRINT << E << print_val_func;
// <тип токена, возвращаемое значение функции (void, если функции нет)>
// Program
extern NTerm<TokType, void> Operator;
NTerm<TokType, void> Program =
Rule() << Operator
| Rule() << Program << ';' << Operator;
// Operator
extern NTerm<TokType, void> InputOperator, PrintOperator, AssignOperator;
NTerm<TokType, void> Operator =
Rule() << InputOperator
| Rule() << PrintOperator
| Rule() << AssignOperator;
// InputOperator
extern NTerm<TokType, double*> Variable;
void input_func(double *variable) {
scanf("%lf", variable);
}
NTerm<TokType, void> InputOperator =
Rule() << READ << Variable << input_func
| Rule() << InputOperator << ',' << Variable << input_func;
// PrintOperator
extern NTerm<TokType, double> Expression;
void print_val_func(double value) {
printf("%f", value);
}
void print_str_func(std::string str) {
printf("%s", str.c_str());
}
NTerm<TokType, void> PrintOperator =
Rule() << PRINT << Expression << print_val_func
| Rule() << PRINT << Term<std::string>(STRING) << print_str_func
| Rule() << PrintOperator << ',' << Expression << print_val_func
| Rule() << PrintOperator << ',' << Term<std::string>(STRING) << print_str_func;
// AssignOperator
void assign_func(double *variable, double value) {
*variable = value;
}
NTerm<TokType, void> AssignOperator =
Rule() << Variable << '=' << Expression << assign_func;
// Variable
std::map<std::string, double> variables;
double *variable_func(std::string name) {
return &variables[name];
}
NTerm<TokType, double*> Variable =
Rule() << Term<std::string>(VARNAME) << variable_func;
// Expression
extern NTerm<TokType, double> ExprTerm; // Слагаемое
double add_func(double x, double y) {
return x + y;
}
double sub_func(double x, double y) {
return x - y;
}
NTerm<TokType, double> Expression =
Rule() << ExprTerm
| Rule() << Expression << '+' << ExprTerm << add_func
| Rule() << Expression << '-' << ExprTerm << sub_func;
// ExprTerm
extern NTerm<TokType, double> Factor;
double mul_func(double x, double y) {
return x * y;
}
double div_func(double x, double y) {
return x / y;
}
NTerm<TokType, double> ExprTerm =
Rule() << Factor
| Rule() << ExprTerm << '*' << Factor << mul_func
| Rule() << ExprTerm << '/' << Factor << div_func;
// Factor
double deref_var_func(double *variable) {
return *variable;
}
NTerm<TokType, double> Factor =
Rule() << Term<double>(NUMBER)
| Rule() << Variable << deref_var_func
| Rule() << LP << Expression << ')';
class MyLexer : public Lexer<TokType> {
int i = 0;
char* buffer;
public:
MyLexer(char *input_file)
{
buffer = input_file;
}
Token<TokType> *next_token() {
Coord coord;
coord.filename = "in.txt";
coord.line = 1;
coord.col = 1;
i++;
switch (i)
{
case 1:
return new Token<TokType>(PRINT, coord);
case 2:
return new AttrToken<TokType, double>(NUMBER, 8, coord);
case 3:
return new Token<TokType>(PLUS, coord);
case 4:
return new AttrToken<TokType, double>(NUMBER, 10, coord);
case 5:
return new Token<TokType>(PLUS, coord);
case 6:
// return new AttrToken<TokType, double>(NUMBER, 10, coord);
//case 7:
return new Token<TokType>(END_OF_TEXT, coord);
}
}
};
// Основная программа
int main(int argc, char *argv[]) {
setlocale(LC_ALL, "RUS");
if (argc > 1) {
//MyLexer lexer(argv[1]);
//Program.parse(lexer);
}
else {
std::ifstream file;
file.open("C:\\Users\\Lenovo\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\in.txt", std::ios::in);
if (file) {
std::filebuf* pbuf = file.rdbuf();
std::size_t size = pbuf->pubseekoff(0, file.end, file.in);
pbuf->pubseekpos(0, file.in);
char* buffer = new char(size);
int p = sizeof(char);
pbuf->sgetn(buffer, size);
//std::cout.write(buffer, size);
MyLexer lexer(buffer);
PrintOperator.parse(lexer);
//delete[] buffer;
}
else
{
std::cout << "Где файл? Нет файла." << '\n';
}
}
return 0;
}