From 0b3cdb4ae2fa176ba74a48ff7a1616395079c151 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Tue, 5 Aug 2014 22:46:43 +0200
Subject: too much to tell
---
fast/grammar.cc | 243 ++++++++++++++++++++++++++++++--------------------------
1 file changed, 129 insertions(+), 114 deletions(-)
(limited to 'fast/grammar.cc')
diff --git a/fast/grammar.cc b/fast/grammar.cc
index 7f2d506..558f6e6 100644
--- a/fast/grammar.cc
+++ b/fast/grammar.cc
@@ -1,170 +1,165 @@
#include "grammar.hh"
-string
-esc_str(const string& s) { // FIXME
- ostringstream os;
- for (auto it = s.cbegin(); it != s.cend(); it++) {
- switch (*it) {
- case '"': os << "\\\""; break;
- case '\\': os << "\\\\"; break;
- case '\b': os << "\\b"; break;
- case '\f': os << "\\f"; break;
- case '\n': os << "\\n"; break;
- case '\r': os << "\\r"; break;
- case '\t': os << "\\t"; break;
- default: os << *it; break;
- }
- }
-
- return os.str();
-}
-
namespace G {
+/*
+ * G::NT
+ *
+ */
NT::NT(string& s)
{
- s.erase(0, 1);
- s.pop_back();
+ s.erase(0, 1); s.pop_back(); // remove '[' and ']'
stringstream ss(s);
string buf;
- size_t c = 0;
- index = 0;
+ size_t j = 0;
+ index = 0; // default
while (ss.good() && getline(ss, buf, ',')) {
- if (c == 0) {
+ if (j == 0) {
symbol = buf;
} else {
index = stoi(buf);
}
- c++;
+ j++;
}
}
-T::T(string& s)
+string
+NT::repr() const
{
- word = s;
+ ostringstream os;
+ os << "NT<" << symbol << "," << index << ">";
+
+ return os.str();
}
-Item::Item(string& s)
+string
+NT::escaped() const
{
- if (s.front() == '[' && s.back() == ']') {
- type = NON_TERMINAL;
- nt = new NT(s);
- } else {
- type = TERMINAL;
- t = new T(s);
- }
+ ostringstream os;
+ os << "[" << symbol;
+ if (index > 0)
+ os << "," << index;
+ os << "]";
+
+ return os.str();
}
-Rule::Rule(string& s)
+ostream&
+operator<<(ostream& os, const NT& nt)
{
- stringstream ss(s);
- size_t c = 0;
- string buf;
- while (ss >> buf) {
- if (buf == "|||") { c++; continue; }
- if (c == 0) { // LHS
- lhs = new NT(buf);
- } else if (c == 1) { // RHS
- rhs.push_back(new Item(buf));
- if (rhs.back()->type == NON_TERMINAL) arity++;
- } else if (c == 2) { // TARGET
- target.push_back(new Item(buf));
- } else if (c == 3) { // F TODO
- } else if (c == 4) { // A TODO
- } else { // ERROR FIXME
- }
- if (c == 4) break;
- }
- arity = 0;
+ return os << nt.repr();
}
-Grammar::Grammar(string fn)
+/*
+ * G::T
+ *
+ */
+T::T(const string& s)
{
- ifstream ifs(fn);
- string line;
- while (getline(ifs, line)) {
- G::Rule* r = new G::Rule(line);
- rules.push_back(r);
- if (r->arity == 0)
- flat.push_back(r);
- else if (r->rhs.front()->type == NON_TERMINAL)
- start_nt.push_back(r);
- else
- start_t.push_back(r);
- }
+ word = s;
}
string
-Item::repr() const
+T::repr() const
{
ostringstream os;
- if (type == TERMINAL)
- os << t->repr();
- else
- os << nt->repr();
+ os << "T<" << word << ">";
return os.str();
}
string
-Item::escaped() const
+T::escaped() const
{
- ostringstream os;
- if (type == TERMINAL)
- os << t->escaped();
- else
- os << nt->escaped();
-
- return os.str();
+ return util::json_escape(word);
}
ostream&
-operator<<(ostream& os, const Item& i)
+operator<<(ostream& os, const T& t)
{
- return os << i.repr();
+ return os << t.repr();
}
-string
-NT::repr() const
-{
- ostringstream os;
- os << "NT<" << symbol << "," << index << ">";
- return os.str();
+/*
+ * G::Item
+ *
+ * Better solve this by inheritance
+ * -> rhs, target as vector