-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello all:
I'm getting a segfault when I run this. It looks like the debug
prints, but when I debug it I just get an endless loop of backtrace.
If anyone can help point me in the right direction, I'd appreciate it.
I'd also appreciate, if possible any tips/tricks for cleaning up this
grammar.
Thanks!
//code here:
/***
*I-EBNF parser
*
*This defines a grammar for BNF.
*/
//Speeds up compilation times.
//This is a relatively small grammar, this is useful.
#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#define BOOST_SPIRIT_QI_DEBUG
#include
#include
#include
#include
#include
#include <vector>
#include <string>
#include <iostream>
namespace Parser
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
enum class RHSType
{
Terminal, Identifier
};
struct RHS
{
RHSType type;
std::string value;
};
struct Rule
{
std::string identifier; //lhs
std::vector<RHS> rhs;
};
}
//expose our structs to fusion:
BOOST_FUSION_ADAPT_STRUCT(
Parser::RHS,
(Parser::RHSType, type)
(std::string, value)
)
BOOST_FUSION_ADAPT_STRUCT(
Parser::Rule,
(std::string, identifier)
(std::vectorParser::RHS, rhs)
)
namespace Parser
{
typedef std::vector<Rule> RuleList;
//our grammar definition
template <typename Iterator>
struct Grammar: qi::grammar
{
Grammar(): Grammar::base_type(rules)
{
qi::char_type char_;
letter = char_("a-zA-Z");
digit = char_('0', '9');
symbol = char_('[') | ']' | '[' | ']' | '(' | ')' | '<' | '>'
| '\'' | '\"' | '=' | '|' | '.' | ',' | ';';
character = letter | digit | symbol | '_';
identifier = letter >> *(letter | digit | '_');
terminal = (char_('\'') >> character >> *character >>
char_('\'')) | (char_('\"') >> character >> *character >> char_('\"'));
lhs = identifier;
rhs = terminal | identifier | char_('[') >> rhs >> char_(']')
| char_('{') >> rhs >> char_('}') | char_('(') >> rhs >> char_(')') |
rhs >> char_('|') >> rhs | rhs >> char_(',') >> rhs;
rule = identifier >> char_('=') >> rhs;
rules = rule >> *rule;
}
private:
qi::rule letter, digit,
symbol, character;
qi::rule identifier,
lhs, terminal;
qi::rule rhs;
qi::rule rule;
qi::rule rules;
};
}
int main()
{
Parser::Grammarstd::string::const_iterator parser;
boost::spirit::ascii::space_type space;
std::string input;
std::vectorstd::string output;
bool result;
while (std::getline(std::cin, input))
{
if (input.empty())
{
break;
}
std::string::const_iterator it, itEnd;
it = input.begin();
itEnd = input.end();
result = phrase_parse(it, itEnd, parser, space, output);
if (result && it == itEnd)
{
std::cout << "success" << std::endl;
}
}
return 0;
}
- --
Take care,
Ty
twitter: @sorressean
web:http://tysdomain.com
pubkey: http://tysdomain.com/files/pubkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
iQEcBAEBAgAGBQJWBJSYAAoJEAdP60+BYxejpMwH/0EeVKIALYQOdl9esFIe9fIX
JSor6vD/xn3e+L93TcsfpCSgtJiITfGgCPIURVX4FAE1Tf7PZSsd5lvQS6MgjyhQ
q1EjDaAZNVt+cQhAftCoGcOPElx2aJXhNy/aTzcQqW1ui72t780GrF+MCo3tdq8p
5wIePhVbk0Gq8f3WBVGRXsKLi0hzwAPNXdHndNdYklNndKvClfHN8cwAZa5rFbMN
b9eULQNtw42oT9tABAtpTfCl+tvExL3DMftXoALP5wONSv3ewjX8vNVWb5iv0XeZ
yaYxLlnAGF314NgXOZKrtnXumDofnIzMDCZKxYFH+UfZfA7eFobGKOF3t/pN1JQ=
=gD3u
-----END PGP SIGNATURE-----