42 if (!textFile.is_open())
44 std::cerr <<
"Can't open CFL grammar file `" <<
fileName <<
"`" << std::endl;
53 while (getline(textFile, lineString))
61 symbolString = lineString.substr(lineString.find_first_not_of(WHITESPACE), lineString.find_last_not_of(WHITESPACE)+1);
64 lines.append(lineString.substr(lineString.find_first_not_of(WHITESPACE), lineString.find_last_not_of(WHITESPACE)+1));
68 std::regex reg(
"Start:([\\s\\S]*)Terminal:(.*)Productions:([\\s\\S]*)");
70 if (std::regex_search(lines, matches, reg))
72 lines = matches.str(3);
76 while ((pos = symbolString.find(
" ")) != std::string::npos)
78 sString =
stripSpace(symbolString.substr(0, pos));
79 symbolString.erase(0, pos + 1);
94 std::vector<std::string> wordProds;
96 while ((pos = lines.find(
";")) != std::string::npos)
98 word = lines.substr(0, pos);
99 wordProds.push_back(word);
100 lines.erase(0, pos + delimiter.length());
108 std::regex stripReg(
"\\s*(\\S*)\\s*");
109 std::regex_search(s, matches, stripReg);
110 return matches.str(1);
125 for (
auto wordProd : wordProdVec)
127 if ((pos = wordProd.find(delimiter1)) != std::string::npos)
130 std::string LHS = wordProd.substr(pos + delimiter1.size(), wordProd.size() - 1);
132 prod.push_back(RHSSymbol);
134 std::regex LHSRegEx(
"\\s*(.*)");
135 std::regex_search(LHS, matches, LHSRegEx);
136 LHS = matches.str(1);
137 while ((pos = LHS.find(delimiter)) != std::string::npos)
139 word = LHS.substr(0, pos);
140 LHS.erase(0, pos + delimiter.length());
void setStartKind(Kind startKind)
Symbol insertSymbol(std::string strLit)
SymbolMap< Symbol, Productions > & getRawProductions()
Kind insertTerminalKind(std::string strLit)
std::vector< Symbol > Production
GrammarBase * build() const
Build grammarBase from fileName.
const std::string parseProductionsString() const
Parse start symbol and production from file string.
const std::string stripSpace(std::string s) const
Strip front and tail space.
const std::vector< std::string > loadWordProductions() const
Parse whole production string to production vector.