Data Structures in C++: Parsing page ranges Hello there, I have an assignment du
ID: 3835787 • Letter: D
Question
Data Structures in C++: Parsing page ranges
Hello there, I have an assignment due for my C++ data structures class and I am struggling to code this problem.
Although it may not be an easy assignment, I'll really apprciate it if you could implement this code successfully.
Assignment 6, parsing page ranges Home Due: 2017-05-01" 6 OO PM You know how when you go to print a document theres an option to print "pages In some applications its just two boxes, "from" and "to, but other programs are more flexible, allowing you to write things like 1,2,4,5 1 to 10 1,3 5,8 1 10 odd and so forth. In this assignment, you're going to build an expression tree type and a parser for these "page set'' expressions. The grammar page sets of Looking at the above, we can begin to form an idea of what a valid "page set expression" looks like: A comma-separated list with at least one item where each item is either a single page number or a range of pages where a range of pages is two page numbers, separated by either a dash or the word "to, and optionally followed by the words "odd" or "even (Printing only odd/even pages is useful for duplexing.)Explanation / Answer
I have developed the program which will accepts a pair of strings as an input and it directly converts them into tokens. I have included the comments for each part of the program.
Program:-
// This is the header files which are used to assign properties and methods to each respective classes
#include <iostream>
namespace val = stringVal::val;
namespace parse {
using grammerExpr = parserStdy::vector<int>;
struct displayTokens {
parserStdy::ostream& parseFinalView;
auto operator()(grammerExpr const& e) const
{
parserStdy::copy(parserStdy::begin(e), parserStdy::end(e), parserStdy::ostream_iterator<grammerExpr::value_type>(parseFinalView, ", "));
;
}
};
}
// Uses the parserLexical function for calculating on a range and individual basis
namespace parserLexical {
auto const expand = [](auto& par) {
using stringVal::fusion::parseL;
for (auto i = parseL<0>(_attr(par)); i <= parseL<1>(_attr(par)); ++i)
val::_val(par).push_back(i);
};
auto const pValues = val::uint_;
auto const pRanges = val::rule<struct _r, parse::grammerExpr>{} = (pValues >> '-' >> pValues)[expand];
auto const grammerExpr = val::rule<struct _e, parse::grammerExpr>{} = -(pRanges | val::repeat(1)[pValues]) % ',';
}
template <class pSentences, class pSkips, class pGrammerSent, class viewToken, class pAstVal>
auto parseTesting(pSentences const& pSentPhrase, pGrammerSent const& pGramr, pSkips const& pSkipSent, pAstVal& parseInfo, viewToken const& viewParse)
{
// The All variables are declared over here.
auto parseFirstList = pSentPhrase.begin();
auto parseLastList = pSentPhrase.end();
auto& parseFinalView = viewParse.parseFinalView;
auto const parseSuccess = phrase_parse(parseFirstList, parseLastList, pGramr, pSkipSent, parseInfo);
// using do statements to check all the conditions which satisfies the requirement
if (parseSuccess) {
parseFinalView << "The provided data is sucessfully Parsed: ";
viewParse(parseInfo);
parseFinalView << " ";
}
else {
parseFinalView << " The provided data is failed. Please try again : ";
parseFinalView << " The input of the parse items are: " << pSentPhrase << " ";
}
if (parseFirstList != parseLastList)
parseFinalView << " The provided data which is remaining UnParsed: '" << parserStdy::string(parseFirstList, parseLastList) << ' ';
}
// The main method is the starting point of the program
int main()
{
parserStdy::string parseTestingPhase[] = {
// This is for the individual data.
"1,2,3,4,6,7,9,10,11,12",
// This is to specify using the range values from 1-10
"1-4,6-7,9-12",
};
for (auto const& p : parseTestingPhase) {
parse::grammerExpr numeric_data;
parseTesting(p, parserLexical::grammerExpr, val::space, numeric_data, parse::displayTokens{ parserStdy::cout });
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.