Simple Parser Program

This is a discussion on Simple Parser Program within the C++ Programming forums, part of the General Programming Boards category; I have *just* started on working with C++ and i am interested in what the fellow forumers think of my ...

  1. #1
    Registered User ChJees's Avatar
    Join Date
    Jul 2007
    Location
    Sweden, Sundsvall
    Posts
    5

    Simple Parser Program

    I have *just* started on working with C++ and i am interested in what the fellow forumers think of my Test Parser program .

    (I have experience with PHP from before.)

    File: main.cpp, Executable Download
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    //Maybe not be needed
    #include <algorithm>
    #include <cctype>
    
    
    
    using namespace std;
    
    
    
    
    string StringToLower(string strToConvert)
    {//change each element of the string to lower case
       for(int i=0;i<strToConvert.length();i++)
       {
          strToConvert[i] = tolower(strToConvert[i]);
       }
       return strToConvert;//return the converted string
    }
    
    
    
    void BreakLine ( )
    {
      cout << "\n";
    }
    
    void PrintItOut(string eOutputText)
    {
      cout << "\"" << eOutputText << "\"" << endl;
    }
    
    void Parse()
    {
      //INIT
      string Line = "";
      string cParseLine = "";
      char cParseCharacter;
      char cParseCharacterPrev = '\0';
      char cParseCharacterNext = '\0';
      string FullString = "";
      bool ParseString = 0;
      bool NoEndLine = 0;
      bool CParserContinue = 1;
      
      int FunctionType = 0;
      
      int cPos = 0;
      int cLength = 0;
      
      ifstream fFile ("parse.txt");
      
      while (! fFile.eof() )
      {
      getline(fFile, Line);
      FullString.append(Line);
      }
      fFile.close();
      
      cLength = FullString.length();
      //PARSE!
      while (cPos < cLength)
      {
      //Get character; and look for escaping
      cParseCharacterPrev = cParseCharacter;
      cParseCharacter = FullString[cPos];
      cParseCharacterNext = FullString[cPos + 1];
      //cout << cParseCharacterPrev << cParseCharacter << cParseCharacterNext << " ";
      
      //Check if valid
          if(ParseString == 0 && cParseCharacter != '\t' && cParseCharacter != ' ' )
          {
           cParseLine += cParseCharacter;
            //////////////////////////////////////////////////
            //
            //  PRINT TEXT
            //
            //////////////////////////////////////////////////
            if ( StringToLower(cParseLine) == "print")
            {
              //Parse String now.
              ParseString = 1;
              //Empty the Line
              cParseLine.erase();
              cPos++;
            }
            //////////////////////////////////////////////////
            //
            //  LINE BREAK
            //
            //////////////////////////////////////////////////
            if ( StringToLower(cParseLine) == "break" && cParseCharacterNext == ';')
            {
              //Parse String now.
              //ParseString = 1;
              //Empty the Line
              BreakLine();
              cParseLine.erase();
              cPos++;
            }
            //////////////////////////////////////////////////
            //
            //  PAUSE
            //
            //////////////////////////////////////////////////
            if ( StringToLower(cParseLine) == "pause" && cParseCharacterNext == ';')
            {
              //Parse String now.
              //ParseString = 1;
              //Empty the Line
              BreakLine();
              cParseLine.erase();
              cPos++;
              system("pause");
              BreakLine();
            }
            //////////////////////////////////////////////////
            //
            //  CLEAR SCREEN
            //
            //////////////////////////////////////////////////
            if ( StringToLower(cParseLine) == "clear" && cParseCharacterNext == ';')
            {
              //Parse String now.
              //ParseString = 1;
              //Empty the Line
              system("cls");
              cParseLine.erase();
              cPos++;
            }
            //////////////////////////////////////////////////
            //
            //  EXIT
            //
            //////////////////////////////////////////////////
            if ( StringToLower(cParseLine) == "exit" && cParseCharacterNext == ';')
            {
              //Parse String now.
              //ParseString = 1;
              //Empty the Line
              BreakLine();
              cParseLine.erase();
              exit(1);
            }
          }else if (ParseString == 1){
            
            
            
            if (cParseCharacter == '\\')
            {
              //Nothing, if not next is it
               if (cParseCharacterNext == '\\')
               {
                 cParseLine += cParseCharacter;
                 CParserContinue = 1;
                 //cParseCharacter = '\\';
                 cPos ++;
                 cParseCharacter = cParseCharacterNext;
                 CParserContinue = 0;
               }
               if (cParseCharacterNext == ';')
               {
                 NoEndLine = 1;
                 CParserContinue = 1;
                 cParseCharacter = '\0';
                 cPos++;
                 cParseCharacter = cParseCharacterNext;
               }
               
            }
            
            
            if ( cParseCharacter == ';' && NoEndLine != 1 )
            {
              //Parse String now.
              //Do stuff
              if(FunctionType == 0)
              {
                PrintItOut(cParseLine);
                cParseLine.erase();
              }
              CParserContinue = 0;
              ParseString = 0;
              }
              
              if (CParserContinue == 1)
              {
                cParseLine += cParseCharacter;
              }
            //End String Parsing    
            }
           NoEndLine = 0;
           CParserContinue = 1;
           cPos++;
           }
      
    }
    
    int main(int argc, char *argv[])
    {
        system("PAUSE");
        cout << "\nParse!\n\n";
        Parse();
        cout << "\nEnd of Parse!\n\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    What could be optimised here? Or should i rewrite the parser from the beginning? (I am just making this parser to learn C++ and make use of it in games . )

    The compiler and program i use: Bloodshed Dev-C++ with mingw compiler.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Not bad as a first try

    I'd suggest changing this though
    Code:
      while (! fFile.eof() )
      {
      getline(fFile, Line);
      FullString.append(Line);
      }
      fFile.close();
    You may have been taught to use the idiom "Read until EOF", although this doesn't work in C++, because the EOF flag isn't set until after a failed read attempts to go past the end of the file (So your final call to getline(fFile, Line) will fail)

    The correct idiom in C++ is "Read while the file has content", which looks like this
    Code:
      while ( getline(fFile, Line) )
      {
         FullString.append(Line);
      }
      fFile.close();
    The final call to getline (The one which fails) will cause your while loop to stop there and then, rather than continuing to call FullString.append(Line);

  3. #3
    Registered User ChJees's Avatar
    Join Date
    Jul 2007
    Location
    Sweden, Sundsvall
    Posts
    5
    Thanks for the correction . Anything more i should think of when making parsers than just make them flexible?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You seem to be going cParseCharacterNext == ';' a lot. Perhaps you could wrap a larger if statement around those if statements that have this expression.

    Also, in at least one place you're comparing a bool against 1 or 0:
    Code:
    NoEndLine != 1
    It's standard practice to use true and false when referring to the values of bools, rather than 1 and 0. But 1 and 0 works, too, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User ChJees's Avatar
    Join Date
    Jul 2007
    Location
    Sweden, Sundsvall
    Posts
    5
    Just Programmer preference :d.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21