From 190f68c880eb27506669e95e2bc0493e2ec42c4c Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sun, 17 Aug 2014 07:51:16 +0100 Subject: functional again --- fast/grammar.cc | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'fast/grammar.cc') diff --git a/fast/grammar.cc b/fast/grammar.cc index 558f6e6..a003eb4 100644 --- a/fast/grammar.cc +++ b/fast/grammar.cc @@ -10,7 +10,18 @@ namespace G { NT::NT(string& s) { s.erase(0, 1); s.pop_back(); // remove '[' and ']' - stringstream ss(s); + istringstream ss(s); + if (ss >> index) { // [i] + symbol = ""; + index = stoi(s); + + return; + } else { // [X] + symbol = s; + index = 0; + + return; + } string buf; size_t j = 0; index = 0; // default @@ -135,28 +146,43 @@ operator<<(ostream& os, const Item& i) * */ Rule::Rule(const string& s) +{ + from_s(this, s); +} + +void +Rule::from_s(Rule* r, const string& s) { stringstream ss(s); size_t j = 0; string buf; - arity = 0; + r->arity = 0; size_t index = 1; + vector rhs_nt; + r->f = new Sv::SparseVector(); while (ss >> buf) { if (buf == "|||") { j++; continue; } if (j == 0) { // LHS - lhs = new NT(buf); + r->lhs = new NT(buf); } else if (j == 1) { // RHS - rhs.push_back(new Item(buf)); - if (rhs.back()->type == NON_TERMINAL) arity++; + r->rhs.push_back(new Item(buf)); + if (r->rhs.back()->type == NON_TERMINAL) { + rhs_nt.push_back(r->rhs.back()->nt); + r->arity++; + } } else if (j == 2) { // TARGET - target.push_back(new Item(buf)); - if (target.back()->type == NON_TERMINAL) { - order.insert(make_pair(index, target.back()->nt->index)); + r->target.push_back(new Item(buf)); + if (r->target.back()->type == NON_TERMINAL) { + r->order.insert(make_pair(index, r->target.back()->nt->index)); + if (r->target.back()->nt->symbol == "") + r->target.back()->nt->symbol = rhs_nt[r->target.back()->nt->index-1]->symbol; index++; } } else if (j == 3) { // F TODO + Sv::SparseVector::from_s(r->f, buf); // FIXME this is slow!!! } else if (j == 4) { // A TODO - } else { // ERROR + } else { + // ERROR } if (j == 4) break; } @@ -203,7 +229,7 @@ Rule::escaped() const os << " ||| "; os << f->escaped(); os << " ||| "; - os << "TODO"; + os << "TODO(alignment)"; return os.str(); } -- cgit v1.2.3