#include "grammar.h" #include #include #include #include "tdl-lexer.hpp" using namespace std; namespace delphin { Grammar::Grammar(const string &fname) { _gfilename = fname; _gfile = boost::filesystem::path(fname); _gpath = boost::filesystem::path(_gfile.parent_path()); ifstream gf(_gfilename.c_str()); if (gf.is_open()) { TdlLexer scanner(&gf); _lexer = &scanner; TdlParser parser(*this); if (parser.parse()) {//parsing failed gf.close(); cerr << "Parsing grammar failed. Terminating." << endl; exit(1); } gf.close(); } else { cerr << "Couldn't open grammar file " << _gfilename << endl; exit(1); } } Grammar::~Grammar() { } string Grammar::letype(string &le) { if (_letypes.count(le)) { return _letypes[le]; } else { return string(); } } string Grammar::lexeme(string &le) { if (_lexemes.count(le)) { return _lexemes[le]; } else { return string(); } } bool Grammar::is_lexrule(string rule) { if (_lex_rules.count(rule)) return true; else return false; } bool Grammar::is_letype(string letype) { if (_letype_list.count(letype)) return true; else return false; } std::string Grammar::grammardir() { return _gpath.string(); } void Grammar::set_letype(std::string le, std::string letype) { _letypes.insert(tSSValue(le, letype)); } void Grammar::add_lexrule(std::string rule) { _lex_rules.insert(set::value_type(rule)); } void Grammar::add_letype(std::string type) { _letype_list.insert(set::value_type(type)); } /* void Grammar::readLexicon(const string &fn) { while (line.find("ORTH") == string::npos && !line.empty()) getline(lexiconf, line); if (!line.empty()) { //found ORTH in lex entry int q1 = line.find_first_of('"'); if (q1 != string::npos) {//find lexeme int q2 = line.find('"', q1+1); if (q2 != string::npos) { string orth = line.substr(q1+1, q2-(q1+1)); q1 = line.find('"', q2+1); while (q1 != string::npos) {//MWE q2 = line.find('"', q1+1); if (q2 != string::npos) { orth = orth+"_"+line.substr(q1+1, q2-(q1+1)); q1 = line.find('"', q2+1); } else { cerr << "Unmatched quotes at " << line << endl; break; } } lexemes.insert(tSSValue(lename, orth)); */ void Grammar::error(const class location &l, const std::string &m) { cerr << "Error parsing grammar: " << l << " :" << m << endl; } void Grammar::error(const std::string &m) { cerr << "Error parsing grammar: " << m << endl; } }//namespace