|
Boost Users : |
Subject: [Boost-users] Boost.Spirit's Greedy Fundamental Type Parsers
From: Daniel Hofmann (daniel_at_[hidden])
Date: 2016-03-31 17:02:05
Suppose I want to parse a list of ";"-separated floating point pairs
with "," being the pair separator as in "1,2;3,4". Following this list
comes a string literal representing a file extension, such as ".txt".
Therefore what I want to successfully parse input like the following:
1,2;3,4.txt
(For the record, the input could also be 1.1,2.2;3.3,4.4.txt)
The parser I came up with is the 1:1 translation of above's description
into the Spirit DSL and shows Spirit's expressive power:
((double_ % ",") % ";") >> ".txt"
Unfortunately, the parser fails on the input with the integral values
above. Why? Because the fundamental parser for double_ greedily matches
on the "4." in "4.txt". Changing the "4" to "4.0" as in
1,2;3,4.0.txt
parses successfully (but is not an option as it requires the user to
always add a trailing ".0" in case the last digit is integral.
I read about Spirit's DSL mapping to Parsing Expression Grammar (PEG)
with the choice operator | being evaluated in order. So the next logical
step for me was to try making use of it and adapting the parser:
(((int_ | double_) % ",") % ";") >> ".txt"
which works on
1,2;3,4.txt
but no longer on
1,2;3,4.0.txt
Is there a way to adapt the parser to handle both cases?
I asked this on IRC and got the answer to try a solution based on
((double_ >> ".") | (int_ >> ".")) >> "txt"
but when I use use this to parse "4.txt" into a std::vector<double> via
parse(first, last, ((double_ >> ".") | (int_ >> ".")) >> "txt", into);
the vector contains: {4, 4} and its size() is 2, which I can make no
sense of at all (but this may be a different problem).
Cheers,
Daniel J H
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net