#define phrases and words

Is there anyway to #define phrases to mean words?

Example:
#define A Very Long Integer __int64

The preprocessor thinks that I am defining "A" to mean "Very Long Integer __int64". I want it to be that "A Very Long Integer" is __int64.

Thanks.

BTW: I am trying to make a library which turns C Syntax into English Syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef ENGLISHDICT_H_
#define ENGLISHDICT_H_

#include <cstdio>

// types
	typedef int Integer;
	typedef void None;
	typedef bool Boolean;
	typedef char Character;
	typedef char* Characters;
	typedef short Short;
	typedef __int8 Integer_8;
	typedef __int16 Integer_16;
	typedef __int32 Integer_32;
	typedef __int64 Integer_64;
	
	#define Constant const
	#define New new

// throw-aways
	#define Is
	#define To
	#define By
	#define Of
	#define An
	#define A

// operators:
	// ;
		#define _ ;
	// ==
		#define Equal ==
	// +
		#define Plus +
	// -
		#define Minus -
	// /
		#define Divided /
	// %
		#define Remainder %
	// ~
		#define Destruct
 
#endif // ENGLISHDICT_H_ 
Last edited on
No, there is no way unless:

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.

Good luck!
> 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" },
     // ... 
};
JLBorges wrote:
Use a lookup table.

Oh, So I could parse it with this map!!! Like

1
2
3
4
5
6
7
8
9
10
11
while(readatokenfromafile(FILE)!=EOF)
{
       for(int x=0;x<keyword_to_natural_language_phrase.size();x++)
       {
              if(token==keyword_to_natural_language_phrase[x][1])
              {
                       writetofile(keyword_to_natural_language_phrase[x][0]);
              }
        }
}
fclose(FILE);


I do not know a lot about vectors, but I guess I can use arrays.
Last edited on
> 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

       else return token ; // else return the token untranslated
}


And then:
1
2
3
4
5
6
7
// ...
std::string token ;
std::ifstream file_in( "cfile.c" ) ; // input stream
std::ofstream file_out( "translated.txt" ) ; // output stream

while( readatoken( file_in, token ) ) 
     file_out << translate_token(token) ;



Read up on std::map<> http://www.cprogramming.com/tutorial/stl/stlmap.html
I'm getting an error message in your code:

1
2
3
4
5
6
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 


But thanks for the lead!
Last edited on
Thanks so much!
Here is my code if anyone else wants to see.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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

    else return 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;
}
Topic archived. No new replies allowed.