C++ BNF语法解析

https://github.com/r35382/bnflite

#include "bnflite/bnflite.h"
#include <iostream>

typedef bnf::Interface<double> Calc;
/* pass number result */
Calc DoNumber(std::vector<Calc>& res)
{
	char* lst;
	double value = strtod(res[0].text, &lst); // formaly it is not correct for not 0-terminated strings
	Calc ret(value, res);
	if (lst - res[0].text - ret.length != 0) {
		std::cout << "number parse error:"; std::cout.write(ret.text, ret.length);
	}
	return ret;
}

int main_bnflite(int argc, char* argv[]) {
	//int argc1 = 3;
	//char* argv1[3] = { "sjijasd", "测试", "。,.,,.." };
	//main_prettyprint(argc1, argv1);

	bnf::Token token('0', '9');
	bnf::Lexem number = 6 * token;
	bnf::Rule positiveNum = !bnf::Token('-') | number;
	bnf::Bind(positiveNum, DoNumber);
	const char* tail = 0;

	typedef bnf::Interface<double> Calc;
	Calc result;
	std::string clc("123456");
	int tst = bnf::Analyze(positiveNum, clc.c_str(), &tail, result);
	if (tst > 0)
		std::cout << "Result of " << clc << " = " << result.data << std::endl;
	else
		std::cout << "Parsing errors detected, status = " << std::hex << tst << std::endl
		<< "stopped at: " << tail << std::endl;
	return 0;
}


创作不易,小小的支持一下吧!

相关推荐

  1. Yaml语法

    2024-06-17 21:50:05       60 阅读
  2. k8s Yaml语法

    2024-06-17 21:50:05       18 阅读
  3. python的基本语法

    2024-06-17 21:50:05       17 阅读
  4. helm中的_helpers.tpl文件语法

    2024-06-17 21:50:05       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 21:50:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 21:50:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 21:50:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 21:50:05       20 阅读

热门阅读

  1. ZC2205-24V500mAUltralow-Quiescent-Current LDO

    2024-06-17 21:50:05       6 阅读
  2. MySQL触发器基本结构

    2024-06-17 21:50:05       8 阅读
  3. python作业

    2024-06-17 21:50:05       6 阅读
  4. SAP PI系统关于接口清单和接口通量的自定义视图

    2024-06-17 21:50:05       9 阅读
  5. MYSQL 数字(Aggregate)函数

    2024-06-17 21:50:05       10 阅读
  6. 掌握.gitignore与标签(Tag)的高效使用

    2024-06-17 21:50:05       6 阅读
  7. 读《任正非文集》

    2024-06-17 21:50:05       7 阅读
  8. Python 学习 第二册 第14章 网络编程

    2024-06-17 21:50:05       6 阅读
  9. c++深拷贝、浅拷贝

    2024-06-17 21:50:05       9 阅读
  10. 视图和子查询

    2024-06-17 21:50:05       6 阅读
  11. 47-5 内网渗透 - 提权环境搭建

    2024-06-17 21:50:05       7 阅读