Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I have a text file which contains following text.
I need to write a c++ program which converts following text into c++ code which can be run. Can anyone tell me how I can achieve this?


Text is as follows:
Program aba13;
VAR
ab5,cb,be,eb:integer;
BEGIN
ab5=5;
cb=10;
Print("ab5=",ab5);
eb=cb+ab5;
PRINT(eb);
be=2*ab5+eb;
PRINT("be=",be);
END.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Dec-13 17:55pm    
Why?
—SA

This is a lot more difficult than you may think. If you want it to be a universal translator, you would need to first build a grammar and lexer. From the lexer output you can then translate the tree into equivalent C++ structure and run it back through the lexer to get the code.

You would use a tool like ANTLR [^]to generate this. If you want to look at what it takes to build one, then take a look at ANTLR's source code[^].
 
Share this answer
 
If it only needs to be able to compile this exact program, then the following will do:
C++
void main()
{
    cout << "ab5=5" << endl;
    cout << "15" << endl;
    cout << "eb=25" << endl;
}

It produces the same output as your source program. Unfortunately, it can't do anything else :-)
 
Share this answer
 
Comments
Aescleal 5-Dec-13 12:03pm    
Nothing like test driven development to get results quickly!
nv3 5-Dec-13 13:08pm    
Exactly! :-)
CPallini 5-Dec-13 15:56pm    
:-D
Albert Holguin 5-Dec-13 16:09pm    
Like our old boss used to say... "ship it!" (aka, once it works, ship it!). lol :D
nv3 5-Dec-13 16:18pm    
Sounds familiar :-)
You might embed a script language in order to accomplish (something similar to) that, i.e. evaluate user's expressions at runtime.
I suggest Lua[^] , is lightweight, fast and easy to embed in a C/C++ application. Even if you need to exactly interpret your Domain Specific Language, Lua might help you, have a look at the LPeg[^] library.
 
Share this answer
 
Comments
nv3 5-Dec-13 16:25pm    
Interesting! Nontheless it is not something you do in a rainy afternoon.
CPallini 6-Dec-13 3:14am    
I warmly suggest you to do that. :-)
By the way, thank you.
Albert Holguin 5-Dec-13 16:28pm    
+5, scripting is usually the way something like this is accomplished... and well, starting your own script interpreter can be a pain (for little benefit since there's plenty of scripting options).
CPallini 6-Dec-13 3:15am    
Thank you.
You text file looks like a kind of pseudo-code. Now you need to convert this pseudo code into C++ code, and you want to write a C++ program to do that, right ? Basically, you will be writing a kind of an interpreter. As Ron Bayer said, it is not as easy as it sounds.

Facebook wrote a software like this which converts PHP code into C++ code to save resources and speed up execution time and the program was a few hundred-thousand lines of code. But don't be encouraged, converting pseudo code (like in your text file) into C++ code probably would be easier than that.

I suggest you to read about Interpreters and writing them, regular expressions and text parsing, data-structures and algorithms...
 
Share this answer
 
I do not know the original language. It is a text file. It looks like fortran.
But I need to convert it to c++ code using a c++ program. Content of text file won't be changed.
Please see my code below. Finally I got some hope that I can make it working, Thanks a lot in advance.

C++
<pre><pre lang="xml">#include <iostream>
#include <fstream>
#include <sstream>
#include <Vector>
#include<algorithm>
using namespace std;

/*
Content of my final.txt
Program aba13;
VAR
ab5,cb,be,eb:integer;
BEGIN
ab5=5;
cb=10;
Print("ab5=",ab5);
eb=cb+ab5;
PRINT(eb);
be=2*ab5+eb;
PRINT("be=",be);
END.

// Output should be a c++ file which can be run. Output file name should be aba13.cpp

*/

void readfile(istream &input, vector<vector<string> > &output)
{
   string line;
    while( getline(input, line) )
    {
            istringstream Stream(line);
           vector<string> words;
            string Element;

            while( getline(Stream, Element, ';') )
            {
                    words.push_back(Element);
            }
            output.push_back(words);
    }
}

int main()
{
    ofstream myfile;
    string a;

    string str;


     fstream file("final.txt", ios::in);
    myfile.open ("output.txt"); // this is supposed to be aba13.cpp

    if(!file.is_open())
    {
           cout << "File not found!\n";
            return 1;
    }

    typedef vector< vector<string> > myV;
    myV data;

    readfile(file,data);

    for(myV::iterator i = data.begin(); i != data.end(); ++i)
    {
            for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
         {
                   a=*j;
                if (find(i->begin(),i->end(), "Program abc13") != i->end()) // I am trying to read each line and replace with proper c++ code using hand here..
                     myfile <<' '<<";";
{

}
                 /*  cout << a <<"New Line ";//testing
                 myfile <<a<<";";*/
            }

 }

 system("pause");
 file.close();
 myfile.close();

}
 
Share this answer
 
Comments
Richard MacCutchan 5-Dec-13 11:56am    
Why not just write the C++ code manually?
Member 2488839 5-Dec-13 11:58am    
I cannot do that. I have to read line by line and translate it to c++.
Richard MacCutchan 5-Dec-13 12:17pm    
Then you have effectively to write a pre-compiler, which is not a trivial task. If the above text is the only program you will ever have to process then it is a little easier, but still quite lengthy. You need to parse each statement or block and build a table from it, identifying the type of block and the individual statements. For example, the line beginning VAR is a variable declaration, and within that declaration there are a number of identifiers which will be typed as integers. Once you have built your table you can process it by creating a statement of the form int ab5,cb,be,eb;, or even mutliple lines each declaring a single variable ... and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900