Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing file-path string to semantic action in Boost.Spirit
    primarykey
    data
    text
    <p>I am new to Boost.Spirit, and I have a question related to a mini-interpreter I am trying to implement using the library. As a sub-task of parsing my language, I need to extract a file-path from an input of the form:</p> <pre><code>"path = \"/path/to/file\"" </code></pre> <p>and pass it as a string (without quotes) to a semantic action.</p> <p>I wrote some code which can parse this type of input, but passing the parsed string does not work as expected, probably due to my lack of experience with Boost.Spirit.</p> <p>Can anyone help?</p> <p>In reality, my grammar is more complex, but I have isolated the problem to:</p> <pre><code>#include &lt;string&gt; #include "boost/spirit/include/qi.hpp" #include "boost/spirit/include/phoenix_core.hpp" #include "boost/spirit/include/phoenix_operator.hpp" namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; namespace phoenix = boost::phoenix; namespace parser { // Semantic action (note: in reality, this would use file_path_string in non-trivial way) void display_path(std::string file_path_string) { std::cout &lt;&lt; "Detected file-path: " &lt;&lt; file_path_string &lt;&lt; std::endl; } // Grammar template &lt;typename Iterator&gt; struct path_command : qi::grammar&lt;Iterator, ascii::space_type&gt; { path_command() : path_command::base_type(path_specifier) { using qi::string; using qi::lit; path = +(qi::char_("/") &gt;&gt; *qi::char_("a-zA-Z_0-9")); quoted_path_string = lit('"') &gt;&gt; (path- lit('"')) &gt;&gt; lit('"'); path_specifier = lit("path") &gt;&gt; qi::lit("=") &gt;&gt; quoted_path_string[&amp;display_path]; } qi::rule&lt;Iterator, ascii::space_type&gt; path_specifier; qi::rule&lt;Iterator, std::string()&gt; path, quoted_path_string; }; } int main() { using ascii::space; typedef std::string::const_iterator iterator_type; typedef parser::path_command&lt;iterator_type&gt; path_command; bool parse_res; path_command command_instance; // Instance of our Grammar iterator_type iter, end; std::string test_command1 = "path = \"/file1\""; std::string test_command2 = "path = \"/dirname1/dirname2/file2\""; // Testing example command 1 iter = test_command1.begin(); end = test_command1.end(); parse_res = phrase_parse(iter, end, command_instance, space); std::cout &lt;&lt; "Parse result for test 1: " &lt;&lt; parse_res &lt;&lt; std::endl; // Testing example command 2 iter = test_command2.begin(); end = test_command2.end(); parse_res = phrase_parse(iter, end, command_instance, space); std::cout &lt;&lt; "Parse result for test 2: " &lt;&lt; parse_res &lt;&lt; std::endl; return EXIT_SUCCESS; } </code></pre> <p>The output is:</p> <pre><code>Detected file-path: / Parse result for test 1: 1 Detected file-path: /// Parse result for test 2: 1 </code></pre> <p>but I would like to obtain:</p> <pre><code>Detected file-path: /file1 Parse result for test 1: 1 Detected file-path: /dirname1/dirname2/file2 Parse result for test 2: 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload