5

LaTeX数学を解析したい(最初の例では、記号のみを認識して保持する)。現在、上付き文字と下付き文字、中括弧との組み合わせに問題があります (たとえばa^{bc}、それらの組み合わせで、基本的なa^b動作は問題ありません)。最小限の例 (可読性を維持しながら人間的に可能な限り短く):

#include <iostream>
  using std::cout;
#include <string>
  using std::string;

#include <boost/spirit/home/x3.hpp>
  namespace x3 = boost::spirit::x3;
  using x3::space;
  using x3::char_;
  using x3::lit;
  using x3::repeat;

x3::rule<struct scripts, string> scripts = "super- and subscripts";
x3::rule<struct braced_thing, string> braced_thing = "thing optionaly surrounded by curly braces";
x3::rule<struct superscript, string> superscript = "superscript";
x3::rule<struct subscript, string> subscript = "subscript";

// main rule: any number of items with or without braces
auto const scripts_def = *braced_thing;
// second level main rule: optional braces, and any number of characters or sub/superscripts
auto const braced_thing_def = -lit('{') >> *(subscript | superscript | repeat(1)[(char_ - "_^{}")]) >> -lit('}');
// superscript: things of the form a^b where a and b can be surrounded by curly braces
auto const superscript_def = braced_thing >> '^' >> braced_thing;
// subscript: things of the form a_b where a and b can be surrounded by curly braces
auto const subscript_def = braced_thing >> '_' >> braced_thing;

BOOST_SPIRIT_DEFINE(scripts)
BOOST_SPIRIT_DEFINE(braced_thing)
BOOST_SPIRIT_DEFINE(superscript)
BOOST_SPIRIT_DEFINE(subscript)

int main()
{
  const string input = "a^{b_x y}_z {v_x}^{{x^z}_y}";
  string output; // will only contain the characters as the grammar is defined above
  auto first = input.begin();
  auto last = input.end();
  const bool result = x3::phrase_parse(first, last,
                                       scripts,
                                       space,
                                       output);
  if(first != last)
    std::cout << "partial match only:\n" << output << '\n';
  else if(!result)
    std::cout << "parse failed!\n";
  else
    std::cout << "parsing succeeded:\n" << output << '\n';
}

それもAvailable on Coliruです。

問題は、この segfaults (明らかな理由で確信しています) であり、これを表現文法で表現する方法が他にありません。

4

1 に答える 1