15 #ifndef DATA_GRAMMAR_UTILITIES_HPP 16 #define DATA_GRAMMAR_UTILITIES_HPP 33 inline const uint
getSize (
const std::string& rhs ) {
34 if ( rhs ==
"" )
return 0;
45 if ( word[0] <
'A' || word[0] >
'Z' )
return true;
58 if ( word[word.size() - 1] >=
'0' && word[word.size() - 1] <=
'9' )
59 word.resize ( word.size() - 1 );
74 const std::vector<std::string>& translation ,
75 unordered_map<uint, uint> *mappings ) {
76 unordered_map<std::string, uint> partial_mappings;
78 for ( uint k = 0; k < source.size(); ++k ) {
80 partial_mappings[source[k]] = nt++;
83 for ( uint k = 0; k < translation.size(); ++k ) {
85 USER_CHECK ( partial_mappings.find ( translation[k] ) != partial_mappings.end(),
86 "RHS source and RHS target do not match!" );
87 ( *mappings ) [partial_mappings[translation[k]]] = nt++;
bool isTerminal(const std::string &word)
Determine if the element is a terminal (i.e. a word, represented by a number) or a non-terminal (i...
uint count_needles(const std::string &haystack, const char needle, std::size_t start, std::size_t end)
Convenience function that counts the number of times a needle appears.
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
const uint getSize(const std::string &rhs)
A generic element counter that can be used to any string. It is intended to use with either source or...
void getRuleMappings(const std::vector< std::string > &source, const std::vector< std::string > &translation, unordered_map< uint, uint > *mappings)
Given a source and translation of the same rule, sharing the same non-terminals in RHS...
void getFilteredNonTerminal(std::string &word)
Return the filtered non-terminal name. For example, for the rule Z 3_XT2_5 XT2, getFilteredNonTermina...