मैं पार्स करना चाहता हूं (पहले उदाहरण में, केवल पहचानें, प्रतीकों को रखते हुए) लाटेक्स गणित। अभी, मुझे घुंघराले ब्रेसिज़ (उदा। 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 (मुझे स्पष्ट कारणों के लिए निश्चित है) और मेरे पास इसका कोई अन्य तरीका नहीं है, ठीक है, इसे अभिव्यक्त करना ... अभिव्यक्ति व्याकरण।
आपकी समस्या [इस के समान है (लेकिन कहीं अधिक जटिल है)एक] (http://stackoverflow.com/questions/18611990/flipping-the-order-of-subrules-inside-a-rule-in-a-boostspirit-grammar-results)। मुझे विश्वास से बहुत दूर है कि [यह] (http://coliru.stacked-crooked.com/a/79e2edf0a6ff86d1) सही है, लेकिन देखें कि यह मदद करता है या नहीं। यदि भविष्य में आपको एएसटी बनाने की जरूरत है ... यह सुंदर नहीं होगा (अर्थपूर्ण कार्रवाई नरक)। उम्मीद है कि आपको एक बेहतर जवाब मिलेगा। पीएस: आपका 'char _- "_^{}" 'सही नहीं है' char_-lit (" _^{} ") के बराबर है, लेकिन' lit ("abc") 'बिल्कुल ठीक "abc" से मेल नहीं खाता "ए" "या" बी "या" सी "। – llonesmiz
@cv_and_he वास्तव में आपका नमूना बाएं-रिकर्सन को हटा देता है और '{}' की मैला हैंडलिंग को ठीक करता है। यहां [एक अद्यतन जो दिखाता है] (http://coliru.stacked-crooked.com/a/30b2ee7981c52bab) यह कम से कम _matching_ समान परीक्षण मामलों (मुझे पूरा यकीन है कि एएसटी में कुछ अंतर है "लेकिन हम कर सकते हैं ओपी की जरूरत को बेहतर बनाने के लिए क्या कहना है, मुझे लगता है)। – sehe