From a27e2c529ddb7dc1be0c6bcc44e3ab558126d15d Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Thu, 24 Jul 2014 10:06:27 +0200
Subject: arg
---
fast/grammar.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
fast/grammar.hh | 8 ++++++
fast/hypergraph.cc | 19 ++++++++------
fast/hypergraph.hh | 10 +++++---
fast/sparse_vector.hh | 3 ++-
fast/test_grammar.cc | 4 ++-
6 files changed, 100 insertions(+), 14 deletions(-)
(limited to 'fast')
diff --git a/fast/grammar.cc b/fast/grammar.cc
index 07d4732..7f2d506 100644
--- a/fast/grammar.cc
+++ b/fast/grammar.cc
@@ -1,6 +1,25 @@
#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 {
NT::NT(string& s)
@@ -84,6 +103,19 @@ Item::repr() const
os << t->repr();
else
os << nt->repr();
+
+ return os.str();
+}
+
+string
+Item::escaped() const
+{
+ ostringstream os;
+ if (type == TERMINAL)
+ os << t->escaped();
+ else
+ os << nt->escaped();
+
return os.str();
}
@@ -98,6 +130,19 @@ NT::repr() const
{
ostringstream os;
os << "NT<" << symbol << "," << index << ">";
+
+ return os.str();
+}
+
+string
+NT::escaped() const
+{
+ ostringstream os;
+ os << "[" << symbol;
+ if (index > 0)
+ os << "," << index;
+ os << "]";
+
return os.str();
}
@@ -112,6 +157,7 @@ T::repr() const
{
ostringstream os;
os << "T<" << word << ">";
+
return os.str();
}
@@ -141,6 +187,7 @@ Rule::repr() const
", arity=" << arity << \
", map:" << "TODO" << \
">";
+
return os.str();
}
@@ -150,11 +197,34 @@ operator<<(ostream& os, const Rule& r)
return os << r.repr();
}
+string
+Rule::escaped() const
+{
+ ostringstream os;
+ os << lhs->escaped() << " ||| ";
+ for (auto it = rhs.begin(); it != rhs.end(); it++) {
+ os << (**it).escaped();
+ if (next(it) != rhs.end()) os << " ";
+ }
+ os << " ||| ";
+ for (auto it = target.begin(); it != target.end(); it++) {
+ os << (**it).escaped();
+ if (next(it) != target.end()) os << " ";
+ }
+ os << " ||| ";
+ os << "TODO";
+ os << " ||| ";
+ os << "TODO";
+
+ return os.str();
+}
+
ostream&
operator<<(ostream& os, const Grammar& g)
{
for (auto it = g.rules.begin(); it != g.rules.end(); it++)
os << (**it).repr() << endl;
+
return os;
}
diff --git a/fast/grammar.hh b/fast/grammar.hh
index 76b96a6..51501cf 100644
--- a/fast/grammar.hh
+++ b/fast/grammar.hh
@@ -12,6 +12,8 @@
using namespace std;
+string esc_str(const string& s); // FIXME
+
namespace G {
struct NT {
@@ -21,6 +23,7 @@ struct NT {
NT() {};
NT(string& s);
string repr() const;
+ string escaped() const;
friend ostream& operator<<(ostream& os, const NT& t);
};
@@ -29,6 +32,7 @@ struct T {
T(string& s);
string repr() const;
+ string escaped() const { return esc_str(word); }
friend ostream& operator<<(ostream& os, const NT& nt);
};
@@ -44,6 +48,7 @@ struct Item {
Item(string& s);
string repr() const;
+ string escaped() const;
friend ostream& operator<<(ostream& os, const Item& i);
};
@@ -58,7 +63,10 @@ struct Rule {
Rule() {};
Rule(string& s);
string repr() const;
+ string escaped() const;
friend ostream& operator<<(ostream& os, const Rule& r);
+
+ MSGPACK_DEFINE();
};
struct Grammar {
diff --git a/fast/hypergraph.cc b/fast/hypergraph.cc
index 9101c92..6b7bd07 100644
--- a/fast/hypergraph.cc
+++ b/fast/hypergraph.cc
@@ -68,10 +68,10 @@ viterbi(Hypergraph& hg)
namespace io {
void
-read(Hypergraph& hg, string fn)
+read(Hypergraph& hg, vector