boost::spirit
-
hi,
will nen parser mit der spirit lib schreiben. Die Grammatik steht jetzt halbwegs udn er ließt das ganze wunderbar ein nur wie krieg ich die daten jetzt in nen vector gepackt?
die Grammatik
template <typename ScannerT> struct definition { public: definition( Redcode const &self ) { assembly_file = list ; list = +boost::spirit::root_node_d[instruction] ; instruction = opcode >> '.' >> modifier >> mode >> boost::spirit::token_node_d[number] >> ',' >> mode >> boost::spirit::token_node_d[number] ; opcode = boost::spirit::str_p("DAT") ... ; modifier = boost::spirit::str_p("BA") | ... ; mode = boost::spirit::ch_p('#') | ... ; number = boost::spirit::int_p | boost::spirit::uint_p ; alphanumeral = boost::spirit::alnum_p | boost::spirit::space_p ; ...und so versuch ich herauszubekommen, ob was im parsetree steht
long evaluate(tree_parse_info<> info) { return eval_list(info.trees.begin()); } ... long eval_list(iter_t const& i) { cout << "i->value = " << std::string(i->value.begin(), i->value.end()) << " i->children.size() = " << i->children.size() << endl; int p=1; // first child points to a term, so call eval_term on it iter_t chi = i->children.begin(); //long lhs = eval_term(chi); for (++chi; chi != i->children.end(); ++chi) { cout << "loop" << p++ << endl; if (i->value.id() == Redcode::instructionID) .... } }und hier soll das ganze rein;-)
typedef struct Instruction{ enum Opcode Opcode; enum Modifier Modifier; enum Mode AMode; Address ANumber; enum Mode BMode; Address BNumber; }Instruction;nur krieg ich zurzeit die anzahl der instructions
-
1.ohne den test-input oder die komplette grammatik kann man nichtmal sehen, ob die daten richtig verarbeitet werden(siehe die "...")
2. ohne die parse funktion ist es im zusammenhang mit der grammatik auch ein ratespiel, welche form der parsebaum hat
-
ja, ich benutze den ast_parse
info = ast_parse( Textbuffer.c_str(), Grammatik, (space_p | comment_p( ";" )));testinput:
DAT.A #12,#12 ;test DAT.A #12,#12 ;test ;testset DAT.A #12,#12 ;testdas war eigentlich die grammatik danach kommen nurnoch die rule deklaration:
boost::spirit::rule< ScannerT, boost::spirit::parser_context<>, boost::spirit::parser_tag< alphanumeralID > > alphanumeral; boost::spirit::rule<ScannerT, boost::spirit::parser_context<>, boost::spirit::parser_tag<assembly_fileID> > const& start() const { return assembly_file; }
-
und hier?
modifier = boost::spirit::str_p("BA") | ...
-
modifier = boost::spirit::str_p("BA") | boost::spirit::str_p("AB") | boost::spirit::ch_p('A') | boost::spirit::ch_p('B') | boost::spirit::ch_p('F') | boost::spirit::ch_p('X') | boost::spirit::ch_p('I')
-
so wie der baum jetzt ist, kann man damit keine ast erzeugen. du musst erst noch parent nodes bestimmen.
-
ja, aber wie setzt ich die richtig. So das ich aus
DAT.A #12,#12
eine Instruction füllen kanntypedef struct Instruction{ enum Opcode Opcode; enum Modifier Modifier; enum Mode AMode; Address ANumber; enum Mode BMode; Address BNumber; }Instruction;