Hi there,
First of all thanks a lot for this amazing tool ! I have a lot of fun using it !
Though I think that have more or less the same problem than German in that I have solved it but in a not completely satisfactory way. Indeed the solution I have found lead to some slightly complicated type definitions
Here is an excerpt of my main grammar:
// Litteral grammars
typedef LiteralGrammar<bool> bool_literal_t;
bool_literal_t boolLiteral;
typedef LiteralGrammar<double> double_literal_t;
double_literal_t
doubleLiteral;
typedef LiteralGrammar<int> int_literal_t;
int_literal_t intLiteral;
typedef LiteralGrammar<std::string> string_literal_t;
string_literal_t stringLiteral;
// wrapping input nodes tables in grammars
typedef GrammarWrapper<input_node_table<double>::type> input_double_t;
input_double_t doubleInputTable;
typedef GrammarWrapper<input_node_table<std::vector<double> >::type> input_node_double_vector_t;
input_node_double_vector_t doubleVectorInputTable;
typedef GrammarWrapper<input_node_table<int>::type> input_int_t;
input_int_t intInputTable;
typedef GrammarWrapper<input_node_table<bool>::type> input_bool_t;
input_bool_t boolInputTable;
// wrapping output nodes tables in grammars
typedef GrammarWrapper<output_node_table<double>::type> output_double_t;
output_double_t doubleOutputTable;
typedef GrammarWrapper<output_node_table<std::vector<double> >::type> output_node_double_vector_t;
output_node_double_vector_t
doubleVectorOutputTable;
typedef GrammarWrapper<output_node_table<int>::type> output_int_t;
output_int_t intOutputTable;
typedef GrammarWrapper<output_node_table<bool>::type> output_bool_t;
output_bool_t boolOutputTable;
// all integer output nodes
typedef JointGrammar2<int_literal_t, output_int_t> all_int_output_t;
all_int_output_t allIntOutputs;
// integer expressions
typedef ExpressionGrammar<all_int_output_t> int_expr_t;
int_expr_t
intExpression;
// index array grammars
typedef IndexArrayGrammar<output_node_double_vector_t, int_expr_t,
OutPutNode<double> > output_double_node_index_array_t;
output_double_node_index_array_t doubleOutputArrayElement;
typedef IndexArrayGrammar<input_node_double_vector_t, int_expr_t,
InPutNode<double>
> double_input_index_array_t;
double_input_index_array_t doubleInputArrayElement;
// all double outputs
typedef JointGrammar3<double_literal_t,
output_double_node_index_array_t,
output_double_t> all_double_output_t;
all_double_output_t allDoubleOutputs;
typedef JointGrammar2<double_literal_t,
output_double_node_index_array_t> all_output_double_t;
all_output_double_t allDoubleOutput;
//double expression grammar
typedef ExpressionGrammar<all_double_output_t> double_expr_t;
double_expr_t doubleExpression;
// comparisons grammars
typedef ComparisonGrammar<double_expr_t> double_comp_t;
double_comp_t doubleComparison;
typedef ComparisonGrammar<int_expr_t> int_comp_t;
int_comp_t intComparison;
// all bool usable by bool expression grammar
typedef JointGrammar4<bool_literal_t, output_bool_t,
double_comp_t, int_comp_t> all_output_bool1_t;
all_output_bool1_t allBoolOutputs;
typedef JointGrammar3<bool_literal_t, double_comp_t, int_comp_t> all_output_bool_t;
all_output_bool_t allBoolOutput;
//bool expression grammar
typedef ExpressionGrammar<all_output_bool1_t> bool_expr_t;
bool_expr_t boolExpression;
// to string grammars
typedef ToStringGrammar<double_expr_t> to_string_double_t;
to_string_double_t toStringDouble;
typedef ToStringGrammar<int_expr_t> to_string_int_t;
to_string_int_t toStringInt;
typedef ToStringGrammar<bool_expr_t> to_string_bool_t;
to_string_bool_t toStringBool;
// all strings grammars
typedef JointGrammar4<string_literal_t, to_string_double_t, to_string_int_t, to_string_bool_t> all_strings_t;
all_strings_t
allStrings;
//string expression grammar
typedef ExpressionGrammar<all_strings_t> string_expr_t;
string_expr_t stringExpression;
// debug grammar
typedef DebugGrammar<string_expr_t, bool_expr_t> debug_t;
debug_t debug;
// all assignable doubles
typedef JointGrammar2<double_input_index_array_t, input_double_t> all_double_inputs_t;
all_double_inputs_t all_double_input_t;
// assignment grammars
typedef
AssignmentGrammar<all_double_inputs_t, double_expr_t> double_assgt_t;
double_assgt_t doubleAssignments;
typedef AssignmentGrammar<input_int_t, int_expr_t> int_assgt_t;
int_assgt_t intAssignments;
typedef AssignmentGrammar<input_bool_t, bool_expr_t> bool_assgt_t;
bool_assgt_t boolAssignments;
// all assignments
typedef JointGrammar4<debug_t, double_assgt_t, int_assgt_t, bool_assgt_t> all_assgt_t;
all_assgt_t assignments;
// all control flows
typedef
ControlFlowsGrammar<all_assgt_t, bool_expr_t> control_flows_t;
control_flows_t controlFlows;
As you may notice I have to precise quite precisely the type of my subgrammars. I would be happy if I could use some plain old polymorphism so as to be a bit more general.
best regards,
François
----- Original Message ----
From: Joel de Guzman <joel <at> boost-consulting.com>
To: spirit-general <at> lists.sourceforge.net
Sent: Friday, 2 November, 2007 12:38:49 AM
Subject: Re: [Spirit-general] Writing a grammar with "subgrammars"
Germán Diago wrote:
> Hello. I'm trying to write a parser for a language and I have
something
> like this:
>
>
> class MyPieceOfGrammar : public Grammar<MyPieceOfGrammar>
> {
>
> template <class ScannerT>
> struct definition {
> rule<parserT> rule1, rule2, rule3;
>
> definition(const MyPieceOfGrammar & self)
> {
> rule1 = ...;
> rule2 = ...;
> }
>
> const rule<ScannerT> & start() const
> {
> return rule1;
> }
> };
>
> };
>
> So now, I have a little parser for one
piece of my grammar. But the
> problem is that I want to write many little grammars
> and I want to reuse some rules for different grammars, rules that I
want
> to be stored in a "main" grammar object or so
> so that those rules are visible from my little grammars. After that I
> want to be able to integrate all those little grammars in
> the big grammar, which is the whole language. . I want something like
this:
>
> class AllTheGrammar : public grammar<AllTheGrammar> {
>
> ///Uses all my little grammars and my little grammars should be
able
> to reuse rules from this one, because if I make
> a change in a rule of this one, the change will be propagated
to
> all the other grammars.
> };
>
> And I want to write little pieces of grammar in different files to
build
> a modular parser. If I don't do this, I get a big piece of monolithic
> software, and it's too big to do it in a monolithic way.
>
> I don't know if I have expressed myself well enough, I expect so. Can
> anyone help on this, please?
Spirit is designed to be modular. You can have as many fine-grained
grammars
as you wish. In fact, that's the recommended strategy. You simply use
your small grammars inside your bigger grammars, that's it.
Regards,
--
Joel de Guzman
http://www.boost-consulting.comhttp://spirit.sf.net-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>
http://get.splunk.com/_______________________________________________
Spirit-general mailing list
Spirit-general <at> lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/spirit-general