C++ data structure Parsing Page Ranges You know how when you go to print a docum
ID: 3835662 • Letter: C
Question
C++ data structure
Parsing Page Ranges
You know how when you go to print a document there’s 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
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 of page sets
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.)
A grammar for this looks like this:
The start symbol, as is our custom, is the first symbol defined: pset.
Program structure
To complete this assignment you’ll need to write a program which accepts page set expressions as strings, lexically converts them into tokens, parses the token sequence into a suitable expression tree structure, and then uses that structure to print out a list of all the individual pages that are in the set. For example, for all of the above page sets we have
Lexical analysis
You can assume that all tokens are separated by spaces or commas. Thus, 1,2 is two tokens, not one.
Semantic analysis
Remember that to write a top-down parser, it’s usually best to implement each symbol as its own function, which can call on the other functions as needed. (This grammar is only slightly recursive, in the pset rule, so that’s the only function that will be recursive.)
The return value of these functions should be a pointer to your expression tree type (or nullptr if the token sequence could not be parsed).
------------------------------------------------------------------------------------------------------------------------------------------------------
How can I construct
by using struct pset_range : public pset{ ...} ;
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.