I try to write a grammar to parse the following syntax:
// - command
// - command value0 ... valueN
// - command -arg0 ... -argN
// - command -arg0 value0 ... valueN ... -argN value0 ... valueN
- Each element shall be interpreted as a string
- Within a string all symbols are allowed
- Between command, argument and value multiple blanks shall be allowed
- An argument starts always with '-'
The results shall be stored in a struct:
struct Data { std::string m_command; std::map< std::string, std::vector< std::string > m_arg; }
- m_command shall store the parsed command
- m_arg shall store the parsed argument and the corresponding values within a vector
I added my current grammar within a short example here
My problem:
The vector contains more entries than available values because blanks are also interpreted as values
It's not exactly clear how you want the grammer to function¹, but from the target data structure I get the impression things could be simplified vastly by
using automatic attribute propagation instead of phoenix (see also Boost Spirit: "Semantic actions are evil"?).
In the sample below I've used Fusion adaptation to enable automatic attribute propagation (which, at once, enables debug output with
Live On Coliru
Prints
(with debug output disabled)
¹ (e.g. is
-23.0
expressly an option or not)