The input for the parser is similar to this example:
struct Word{
Word(std::string txt, int val)
:text(txt)
,value(val)
{}
std::string text;
int value;
};
int main()
{
std::vector<Word> input;
input.push_back(Word("This", 10));
input.push_back(Word("is", 73));
input.push_back(Word("the", 5));
input.push_back(Word("input", 32));
}
The grammar for the parser is written for to the text
variable of the Words and can look like this:
qi::rule<Iterator, int()> word = qi::string("This") |
qi::string("is") |
qi::string("the") |
qi::string("input");
qi::rule<Iterator, std::vector<int>()> start = +word;
Parsing the std::vector<Word> input
should result in a vector containing the corresponding Integer values, for this example it would be
[10,73,5,32]
- Is this even possible with boost::spirit or should I take a different approach?
If this is could be a reasonable solution,
- How can one implement an
Iterator
for this, how does it look like? - What should the semantic actions look like to create the corresponding synthesized attribute or do I need some other spirit "magic"?
I hope I have provided enough information for this, let me know if not.
EDIT:
Looks like I asked not specific enough since I tried to keep this question as general as possible. Sehe's solution should work for what I described, but I have the following limitations:
- A Word can occur multiple times with different Integer values, there is no correlation between a Words text and its Integer value
- The "text" (in this example "This is the input") needs to be parsed anyway to complete another task. I have already written everything to do so and it would be really easy for me to add what I need, if only I could access the Integer value from inside the semantic actions somehow.