代码拉取完成,页面将自动刷新
#include "parser.h"
#include "calculator.h"
#include "gram.h"
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <unordered_map>
static std::unordered_map<std::string, int> keyworks = {
{"abs", NAME},
{"main", NAME},
{"max", NAME},
};
/*gram里生成的*/
extern int core_yylex(core_YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
core_yyscan_t yyscanner);
/*scan里生成的*/
extern core_yyscan_t scanner_init(const char *str, int len,
core_yy_extra_type *yyext);
int core_yylex_destroy(core_yyscan_t yyscanner);
static void scanner_finish(core_yyscan_t yyscanner);
Expr *parse_string(const char *str, int len) {
core_yyscan_t yyscanner;
base_yy_extra_type yyextra;
int yyresult;
yyextra.expr = NULL;
/* initialize the flex scanner */
yyscanner = scanner_init(str, len, &yyextra.core_yy_extra);
/* Parse! */
yyresult = base_yyparse(yyscanner);
/* Clean up (release memory) */
scanner_finish(yyscanner);
if (yyresult) {
return NULL; /* error */
}
return yyextra.expr;
}
extern void scanner_finish(core_yyscan_t yyscanner) {
base_yy_extra_type *yyextra = pg_yyget_extra(yyscanner);
if (yyextra->core_yy_extra.scanbuf) {
free(yyextra->core_yy_extra.scanbuf);
}
core_yylex_destroy(yyscanner);
}
/* from parser.cpp */
int base_yylex(YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner) {
base_yy_extra_type *yyextra = pg_yyget_extra(yyscanner);
int cur_token = core_yylex(&(lvalp->core_yystype), llocp, yyscanner);
switch (cur_token) {
case IDENT: {
auto it = keyworks.find(lvalp->str);
if (it != keyworks.end()) {
cur_token = it->second;
}
}
default:
break;
}
return cur_token;
}
char *strdup_lowder(const char *str) {
char *ret = strdup(str);
char *ptr = ret;
while (*ptr != 0) {
*ptr = tolower(*ptr);
ptr++;
}
return ret;
}
int64_t pg_strtoint64_safe(const char *s, std::string &error_msg) {
int64_t ret;
try {
ret = std::stoll(s);
} catch (std::invalid_argument const &ex) {
error_msg = std::string("invalid input syntax for type ") + s;
} catch (std::out_of_range const &ex) {
error_msg = std::string("out if range ") + s;
}
error_msg = "";
return ret;
}
double pg_strtodouble_safe(const char *s, std::string &error_msg) {
double ret;
try {
ret = std::stod(s);
} catch (std::invalid_argument const &ex) {
error_msg = std::string("invalid input syntax for type ") + s;
} catch (std::out_of_range const &ex) {
error_msg = std::string("out if range ") + s;
}
error_msg = "";
return ret;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。