C ++ program whose output is two C ++ programs?

My current task right now is to write one program that as input introduces some textual specification for compressing 64-bit instructions into 32-bit instructions. From this specification I have to build two executable programs: encoder and decoder.

Currently, I am just writing a parser class to indicate the text specification that I have developed, but in the end I will have to turn the information that I receive into two new programs. The only way I know I can do this is to print in a new .cpp file using thenstream, then using system('g++ new_file.cpp -o new.x') to create the executable. Then perhaps system('rm new_file.cpp') for cleaning.

I went around all the other ways to do this, but found nothing. If you have any advice, I will be very grateful.

thanks

ps I have not included any of my codes because the code does not matter. For simplicity, my goal could be to write a program whose output is "Hello, World!" -esque executable file.

+4
source share
2 answers

Ask your program to do this:

 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { { ofstream out( "a.cpp" ); out << "#include <iostream>\nint main() { std::cout << \"hello world\\\n\"; } \n"; } system( "g++ a.cpp -o hello" ); } 

Just tested it - it creates a compiled welcome program.

+3
source

Hmm, that sounds like a good challenge for XML and XSLT. Use XML to determine what you want, and XSLT to create actual C ++ code.

EDIT: for those who are understated:

 <?xml version="1.0" encoding="UTF-8" ?> <proggy> <prompt input='uname' type='std::string'> <text>Name: </text> <output><text>Hello </text><field name="uname"/><text>!</text></output> </prompt> </proggy> 

XSL to convert this to C ++

 <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" indent="no"/> <xsl:variable name="co"><xsl:text>std::cout</xsl:text></xsl:variable> <xsl:variable name="ci"><xsl:text>std::cin</xsl:text></xsl:variable> <xsl:variable name="e"><xsl:text> &lt;&lt; std::endl;</xsl:text></xsl:variable> <xsl:variable name="so"><xsl:text> &lt;&lt; </xsl:text></xsl:variable> <xsl:variable name="si"><xsl:text> &gt;&gt; </xsl:text></xsl:variable> <xsl:variable name="nl"><xsl:text>&#xa;</xsl:text></xsl:variable> <!-- Root node --> <xsl:template match="/"> <xsl:text> #include &lt;iostream&gt; int main(void) { </xsl:text> <xsl:apply-templates select="//proggy"/> <xsl:text> return 0; } </xsl:text> </xsl:template> <xsl:template match="proggy"> <xsl:apply-templates select="prompt"/> </xsl:template> <xsl:template match="prompt"> <xsl:value-of select="$co"/><xsl:value-of select="$so"/> <xsl:text>"</xsl:text><xsl:value-of select="text"/><xsl:text>"</xsl:text> <xsl:value-of select="$e"/><xsl:value-of select="$nl"/> <xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@input"/><xsl:text>;</xsl:text> <xsl:value-of select="$nl"/> <xsl:text>if (</xsl:text> <xsl:value-of select="$ci"/><xsl:value-of select="$si"/><xsl:value-of select="@input"/> <xsl:text>){</xsl:text> <xsl:value-of select="$nl"/> <xsl:apply-templates select="output"/> <xsl:value-of select="$nl"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:template match="output"> <xsl:value-of select="$co"/> <xsl:apply-templates select="./*" mode="so"/> <xsl:value-of select="$e"/> </xsl:template> <xsl:template match="text" mode="so"> <xsl:value-of select="$so"/><xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text> </xsl:template> <xsl:template match="field" mode="so"> <xsl:value-of select="$so"/><xsl:value-of select="@name"/> </xsl:template> </xsl:stylesheet> 

I urge you to write a small C ++ program to create the same C ++ code! And this is a trivial example.

-1
source

All Articles