Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What your trying to achieve is parsing, not just raw splitting. Here is an example that will work for you using <a href="http://www.boost.org/doc/libs/1_55_0/libs/spirit/doc/html/index.html" rel="nofollow">boost::spirit</a>. I would strongly advice you using such tools because it allows you a bit of flexibility that raw litteral parsing with scanf won't provide you. This is not the only tool available, but it is worth the time you'll spent learning it.</p> <p>So here my small commented example, I hope it does help you:</p> <pre><code>#include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/spirit/include/phoenix_core.hpp&gt; #include &lt;boost/spirit/include/qi_as.hpp&gt; #include &lt;boost/variant.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;iterator&gt; // Parameters can be either string or int typedef boost::variant&lt;std::string, double&gt; Parameter; // Here is the function where we read the command and retrieve entity/function // and parameters bool read_command(const std::string&amp; str) { // Just to ease the parser writing namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; namespace phoenix = boost::phoenix; typedef qi::rule&lt;std::string::const_iterator, std::string()&gt; rule; // Variables where we will store the results std::string entity; std::string function; std::vector&lt;Parameter&gt; arguments; // An identifier rule identifier = qi::alpha &gt;&gt; *(qi::alnum | qi::char_("_")); // A parameter is either a number or an identifier, there is a issue if we // use "rule" type here, double are not read correctly anymore... auto parameter = qi::double_ | identifier; // The whole command bool r = qi::phrase_parse(str.begin(), str.end(), ( identifier &gt;&gt; identifier &gt;&gt; '(' &gt;&gt; (parameter % ',' ) &gt;&gt; ')' ), qi::space, entity, function, arguments); // Use qi "magic" to store result // Print everything std::cout &lt;&lt; "Parsing result:" &lt;&lt; std::endl &lt;&lt; " - Entity: " &lt;&lt; entity &lt;&lt; std::endl &lt;&lt; " - Function: " &lt;&lt; function &lt;&lt; std::endl &lt;&lt; " - " &lt;&lt; arguments.size() &lt;&lt; " parameter(s): "; // Use copy to print all the arguments std::copy(arguments.begin(), arguments.end(), std::ostream_iterator&lt;Parameter&gt;(std::cout, ", ")); std::cout &lt;&lt; std::endl; return r; } int main() { std::string str; // Read the line while (getline(std::cin, str)) { // Check that this is a line we do want to parse if (str.empty() || str[0] == 'q' || str[0] == 'Q') { break; } // Read the command and output the result if (read_command(str)) { std::cout &lt;&lt; "Parsing succeeded! " &lt;&lt; std::endl; } else { std::cout &lt;&lt; "Parsing failed." &lt;&lt; std::endl; } } return 0; } </code></pre> <p>Output example:</p> <pre><code>$ ./a.exe test f(1) Parsing result: - Entity: test - Function: f - 1 parameter(s): 1, Parsing succeeded! entity function(1, a, b) Parsing result: - Entity: entity - Function: function - 3 parameter(s): 1, a, b, Parsing succeeded! er1_ ft_(1, r_) Parsing result: - Entity: er1_ - Function: ft_ - 2 parameter(s): 1, r_, Parsing succeeded! </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.
    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