$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Chris Cooney (ccooney_at_[hidden])
Date: 2003-08-28 15:07:51
Hello,
I've been fiddling around with boost::spirit in an
attempt to write a small and esoteric scripting language.
Here's some code snipits of what I'm trying to do.
example script (a one liner in this case):
DO_SOMETHING(param1,param2)
the grammer:
struct my_grammer : public grammar<my_grammer>
{
my_grammer(vector<string>& v_): v(v_) {}
template <typename ScannerT>
struct definition
{
definition(wb_grammer const& self)
{
//char token defs
COMMA = ch_p(',');
LPAREN = ch_p('(');
RPAREN = ch_p(')');
USCORE = ch_p('_');
param_alnum = lexeme_d[alpha_p >> *(alnum_p)];
do_something = str_p("DO_SOMETHING") >> LPAREN >>
/*param1*/param_alnum[append(self.v)] >> !COMMA >>
/*param2*/param_alnum[append(self.v)] >> RPAREN;
do_something_else = str_p("DO_SOMETHING_ELSE") >> LPAREN >>
/*param1*/param_mode[append(self.v)] >> !COMMA >>
/*param2*/param_type[append(self.v)] >> RPAREN;
functions = do_something | do_something_else;
}
rule<ScannerT>
COMMA,LPAREN,RPAREN,USCORE,param_alnum,do_something,do_something_else,functi
ons;
rule<ScannerT> const&
start() const { return functions; }
};
vector<string>& v;
};
int main()
{
vector<string> v;
my_grammer script_grammer(v);
string str;
while (getline(cin, str))
{
parse_info<> info = parse(str.c_str(), script_grammer, space_p);
if (info.full)
{
//determine which rule (script function) was called
//and do something with the parameters stored in the vector
}
else
{
//error out.
}
}
}
problem:
My problem is how to determine which rule was the one
that resulted in a successful parsing of the
line ("DO_SOMETHING(param1,param2)");
NOTE: I'm unfortunately stuck with the MSVC 7.0 compiler so
I can't use the Tree functionalities of spirit because of their
implementations use of partial template specialization.
any ideas?
Thank you very much,
Chris.