hey, for a class project I have to write a parser for a grammar that my professor is giving us. I have broken it down into a very small part and want to get that working before parsing an entire string. In this bit of code, the string to be parsed is "a=" . Which is in the file read.txt

This is my code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool test(char *);

int main ()
{
    ifstream inFile;
    inFile.open("read.txt");
    
    string line;
    inFile >> line;

    
    char *c;
    *c = line [0];

    
    if (test(c) && *c == '\0')
       cout << "The string: " << line << "is in the language" << endl;
       else
       cout << "The string: " << line << "is not in the language" << endl;
    
    

    system("Pause");
    return 0;
}

bool test(char *c)
{
     if (*c == 'a' || *c == '-')
        ++*c;     
        if(*c == '=' || *c == '-')
        return true;
}

The problem I am having is in the test function. The string to be parsed is 'a=' . The function gets past the first if test because c is set to a, but then after '++*c' c is somehow incremented to b instead of the equal sign. I have no idea how to change this and any input/advice would be greatly appreciated!!!

There are several problems and it starts from main() only.

1)

char *c;
*c = line [0];

Assigning a character value to an address which doesn't point to any memory.
According to your code this line has to be:

char *c;
c = &line [0];
//or char *c = &line[0];

2) ++*c just fetches the value at which the pointer is pointing to (in this case 'a') and increments the value.But what you want is to move the pointer one step forward to point to the next input so just c++ or ++c is sufficient.

3)Your second if statement within the test function does check the second input character and verifies whether it is '=' or '-' and directly returns true.Problem here is that pointer c is still pointing to the second character only and not the third character.
Another major point is when the function test returns,it returns true but not false in any case and as the pointer c in function test and function main are different,after control returns from test function,pointer c is still at the first character.

Your help with the pointer and address of operator was very helpful thank you so much !! However, I am still having problems with what you mentioned in your third listed problem. I tried incrementing c after the second if test which did not help, the output was still not in the language. I dont understand why if the if test in main comes out to be true && true, why it doesnt output "in the language" ??

Ok so you incremented the pointer C after the second if statement...??? Good now it points to the '\0' if it is in the language.
Now in your code you are just returning the control and not the pointer position and as a result the c pointer in main is still at the first position which is the prime cause of your problem.
After calling the test function if you could some how return the pointer position and store it in pointer of main your work is done.

Okay so I solved that problem I believe but my work isnt done! Whenever I try to test a string that is not in the language, say "a=b**c", the program crashes. I'm pretty sure it happens in the term function when it reads the first '*' it does okay, but then when it reads the next one, I believe it reads it as null. Any advice? Here is the code with the grammar at the top commented out.

/*
A -> I = E
E -> T + E | T - E | T
T -> P * T | P / T | P
P -> I | L | (E)
I -> a | b | ... | y | z
L -> 0 | 1 | ... | 8 | 9
*/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


bool literal(char *);
bool integer(char *);
bool primary(char *);
bool term(char *);
bool expr(char *);
bool assign(char *);

int main ()
{
    ifstream inFile;
    inFile.open("experiment.txt");
    
    string line;
    inFile >> line;

    
    char *c;
    c = &line[0];
    
    cout << "String read from file: " << line << endl;

    
    if (assign(c))
       cout << "The string: '" << line << "' is in the language" << endl;
       else
       cout << "The string: '" << line << "' is not in the language" << endl;
    
    

    system("Pause");
    return 0;
}
bool assign (char *c)
{
    if (integer(c))
    {        
         ++c;       
         if (*c == '=')
         {             
             ++c;                   
             if (expr(c))
             {                
                ++c; 
                return true;       
              //  if (*c == '\0')
               // {                
                  //   return true;
               // }
             }
         }
     }
     else
         return false;
}

bool literal(char *c)
{
     if (*c == 0 || *c == 1 || *c == 2 || *c == 3 || *c == 4 || *c == 5 || *c == 6 || *c == 7 || *c == 8 || *c == 9)
     {   
        
        return true;
     }
     return false;
}

bool integer (char *c)
{
     if (*c == 'a' || *c == 'b' || *c == 'c' || *c == 'd' || *c == 'e' || *c == 'f' || *c == 'g' || *c == 'h' || *c == 'i' || *c == 'j' || *c == 'k' || *c == 'l' || *c == 'm' || *c == 'n' || *c == 'o' || *c == 'p' || *c == 'q' || *c == 'r' || *c == 's' || *c == 't' || *c == 'u' || *c == 'v' || *c == 'w' || *c == 'x' || *c == 'y' || *c == 'z')
     {
        return true;
     }
     return false;
}

bool primary (char *c)
{
     if(integer(c))
        return true;
     else if (literal(c))
          return true;
     else if (*c == '(')
          ++c;
          if (expr(c))
          {
             ++c;
             if (*c == ')')
             {
                ++c;
                return true;
             }
          }
          return false;
}

bool term (char *c)
{
     if (primary(c))
     {
          ++c;
          if (*c == '*' || *c == '/')
          {
             ++c;
             if (term(c))
             {
                return true;
             }
             else
                 return false;
          }
          return true;
     }
     return false;
}

bool expr (char *c)
{
     if (term(c))
     {
          ++c;
          if (*c == '+' || *c == '-')
          {
             ++c;
             if (expr(c))
             {                
                return true;
             }
          }
          return true;
     }
     return false;
}

PS thanks again : )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.