|
// CmdLineParser.cpp : d�finit le point d'entr�e pour l'application console.
//
#include "stdafx.h"
#include <iostream>
#include "CmdLine.h"
using namespace std;
using namespace boost;
using namespace spirit;
////////////////////////////////////////////////////////////////////////////
//
// Main program
//
////////////////////////////////////////////////////////////////////////////
int
main()
{
using namespace boost::spirit;
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "\t\tCommand line parser \n\n";
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "Type a command line parameter like nested tag input...or [q or Q] to quit\n\n";
cout << "Example: cmd -key=value -key2=\"value2\" -key3='value3'\n\n";
command_line_parser cmdline;
string str;
while (getline(cin, str))
{
if (str[0] == 'q' || str[0] == 'Q')
break;
if (cmdline.parse(str))
{
cout << "-------------------------\n";
cout << "Parsing succeeded\nCommand: "<<cmdline.get_command()<<"\n";
command_line_parser::keyvalue_container::const_iterator it = cmdline.get_keyvalues().begin();
while (it != cmdline.get_keyvalues().end())
{
cout<<it->first<<" -> "<<it->second<<"\n";
++it;
}
cout << "-------------------------\n";
}
else
{
cout << "-------------------------\n";
cout << "Parsing failed\n";
cout << "stopped at: \": " << cmdline.stop() << "\"\n";
cout << "-------------------------\n";
}
}
cout << "Bye... :-) \n\n";
return 0;
}
/*
////////////////////////////////////////////////////////////////////////////
//
// Plain calculator example.
//
// [ JDG 5/10/2002 ]
//
////////////////////////////////////////////////////////////////////////////
#include "boost/spirit/core.hpp"
#include <iostream>
#include <string>
////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace boost::spirit;
////////////////////////////////////////////////////////////////////////////
//
// Semantic actions
//
////////////////////////////////////////////////////////////////////////////
namespace {
void do_int(char const* str, char const* end)
{
string s(str, end);
cout << "PUSH(" << s << ')' << endl;
}
void do_add(char const*, char const*) { cout << "ADD\n"; }
void do_subt(char const*, char const*) { cout << "SUBTRACT\n"; }
void do_mult(char const*, char const*) { cout << "MULTIPLY\n"; }
void do_div(char const*, char const*) { cout << "DIVIDE\n"; }
void do_neg(char const*, char const*) { cout << "NEGATE\n"; }
}
////////////////////////////////////////////////////////////////////////////
//
// Our calculator grammar
//
////////////////////////////////////////////////////////////////////////////
struct calculator : public grammar<calculator>
{
template <typename ScannerT>
struct definition
{
definition(calculator const& self)
{
expression
= term
>> *( ('+' >> term)[&do_add]
| ('-' >> term)[&do_subt]
)
;
term
= factor
>> *( ('*' >> factor)[&do_mult]
| ('/' >> factor)[&do_div]
)
;
factor
= lexeme_d[(+digit_p)[&do_int]]
| '(' >> expression >> ')'
| ('-' >> factor)[&do_neg]
| ('+' >> factor)
;
}
rule<ScannerT> expression, term, factor;
rule<ScannerT> const&
start() const { return expression; }
};
};
////////////////////////////////////////////////////////////////////////////
//
// Main program
//
////////////////////////////////////////////////////////////////////////////
int
main()
{
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "\t\tExpression parser...\n\n";
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "Type an expression...or [q or Q] to quit\n\n";
calculator calc; // Our parser
for (;;)
{
char str[256];
cin.getline(str, 256);
if (str[0] == 'q' || str[0] == 'Q')
break;
parse_info<> info = parse(str, calc, space_p);
if (info.full)
{
cout << "-------------------------\n";
cout << "Parsing succeeded\n";
cout << "-------------------------\n";
}
else
{
cout << "-------------------------\n";
cout << "Parsing failed\n";
cout << "stopped at: \": " << info.stop << "\"\n";
cout << "-------------------------\n";
}
}
cout << "Bye... :-) \n\n";
return 0;
}
*/
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).