views:

216

answers:

2

If you go in my post history you'll see that i'm trying to develop an interpreter for a language that i'm working on. I want to use *size_t* using two different codes, but they all return nothing.

Here is the post of what i was trying: http://stackoverflow.com/questions/1215688/read-something-after-a-word-in-c

When i try to use the file that i'm testing it returns me nothing. Here is the sample file(only a print function that i'm trying to develop in my language):

print "This is a print function that i'm trying to develop in my language"

But remember that this is like print in Python, what the user type into the quotes(" ") is what have to be printed to all, remember that the user can choose what put into the quotes, then don't put something like a simple cout, post something that reads what is inside the quotes and print it to all. But here is the two test codes to do this, but all of they don't returns nothing to me:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
    // Error Messages
    string extension = argv[ 1 ];

    if(argc != 2)
    {
       cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
      return 0;
    }
    if(extension[extension.length()-3] != '.')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-2] != 't')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-1] != 'r')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    // End of the error messages

    ifstream file(argv[ 1 ]);
    if (!file.good()) {
       cout << "File " << argv[1] << " does not exist.\n";
      return 0;
    }
    string linha;
    while (!file.eof())
    {
    getline(file, linha);
    if (linha == "print")
       {
       size_t idx = linha.find("\""); //find the first quote on the line
       while ( idx != string::npos ) {
       size_t idx_end = linha.find("\"",idx+1); //end of quote
       string quotes;
       quotes.assign(linha,idx,idx_end-idx+1);
       // do not print the start and end " strings
       cout << "quotes:" << quotes.substr(1,quotes.length()-2) << endl;
       //check for another quote on the same line
       idx = linha.find("\"",idx_end+1); 
       } 
       }
    }
  return 0;
}

The second:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
    // Error Messages
    string extension = argv[ 1 ];

    if(argc != 2)
    {
       cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
      return 0;
    }
    if(extension[extension.length()-3] != '.')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-2] != 't')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-1] != 'r')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    // End of the error messages

    ifstream file(argv[ 1 ]);
    if (!file.good()) {
       cout << "File " << argv[1] << " does not exist.\n";
      return 0;
    }
    string linha;
    while (!file.eof())
    {
    getline(file, linha);
    if (linha == "print")
       {
       string code = " print \" hi \" ";
       size_t beg = code.find("\"");
       size_t end = code.find("\"", beg+1);
       // end-beg-1 = the length of the string between ""
       cout << code.substr(beg+1, end-beg-1);
       }
    }
  return 0;
}

And here is what is printed in the console:

ubuntu@ubuntu-laptop:~/Desktop/Tree$ ./tree test.tr
ubuntu@ubuntu-laptop:~/Desktop/Tree$

Like i said, it prints me nothing. See my post in D.I.C.: http://www.dreamincode.net/forums/showtopic118026.htm

Thanks, Nathan Paulino Campos

+2  A: 

getline(file, linha) will read an entire line from the file, so linha never be equal to print.

eduffy
+5  A: 

Your problem is the line

if (linha == "print")

which assumes the entire line just read in is "print", not that the line STARTS with print.

Also, why would you use 3 separate checks for a .tr extension, vs. just checking the end of the filename for ".tr"? (You should also be checking that argv[1] is long enough before checking substrings...)

Joe
But what i have to do in my code to correct this line and solve my problem?
Nathan Campos
If you are writing an interpreter, you are only setting yourself up for heartache by doing lots of string compares and not using a parser framework like lex/yacc or Boost.Spirit (both are advanced subjects)--an interpreter is a really hard early-on project if you're still just learning the language. That being said: to fix this particular problem however, you should be looking to see if the line STARTS with "print" using methods within the string class (like .substr).
Joe
Check if the first 4 characters are "print". e.g. if(strncmp(line.c_str(),"print",4) == 0) {..} in C. or perhaps if(line.find("print") == 0) , as some quick and dirty solutions
nos
Please, post an answer if the code, because i need to solve this and here in the comments is so much confusing. Thanks!
Nathan Campos
Please, buy a book on C++ and learn the basics of the language before attempting to build a difficult project. We are not here to do your work for you.
Joe
I have one book of Deitel: C++, How to program 5/e
Nathan Campos