option 1) All the words in the phrase resolve to something unique to that phrase. This option is essentially a form of obfuscation.
option 2) You use a different preprocessor (not the standard CPP)
This is your only real option if you intend to be able to handle stuff of any complexity. Natural Language programming is not as easy as you would think.
> Is there anyway to #define phrases to mean words?
> Example: #define A Very Long Integer __int64
No. Tokens can't contain white space.
> I am trying to make a library which turns C Syntax into English Syntax.
Use a lookup table.
1 2 3 4 5 6
const std::map< std::string, std::string > keyword_to_natural_language_phrase =
{
{ "void", "an incomplete type used as a placeholder" },
{ "int", "the natural integer type for the architecture" },
// ...
};
> Oh, So I could parse it with this map!!! Like ...
Not precisely like that; something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const std::map< std::string, std::string > = look_up
{
{ "void", "an incomplete type used as a placeholder" },
{ "int", "the natural integer type for the architecture" },
// ...
};
std::string translate_token( const std::string& token )
{
auto iter = look_up.find(token) ;
if( iter != look_up.end() ) // if the key was found in look_up
return iter->second ; // return the associated phrase
elsereturn token ; // else return the token untranslated
}
const std::map< std::string, std::string > = look_up
{
{ "void", "an incomplete type used as a placeholder" },
{ "int", "the natural integer type for the architecture" },
// ...
};
Line 1 [Error] expected unqualified-id before '=' token
#include <iostream>
#include <string>
#include <map>
typedef std::map<std::string, std::string> TSSM;
typedef std::pair<std::string, std::string> TSSP;
std::string translate_token(TSSM look_up, const std::string& token )
{
TSSM::iterator iter = look_up.find(token);
if(iter!=look_up.end()) // if the key was found in look_up
return iter->second; // return the associated phrase
elsereturn token; // else return the token untranslated
}
int main(void)
{
TSSM EnglishToC__;
std::string get,translated;
std::cout<<"Enter something to look for: ";
std::getline(std::cin,get);
EnglishToC__.insert(TSSP("void", "an incomplete type used as a placeholder"));
EnglishToC__.insert(TSSP("int", "the natural integer type for the architecture"));
translated=translate_token(EnglishToC__,get);
std::cout<<"\nTranslated Word: "<<translated;
}