I need help developing a program that acts as a command line parser. Essentially, my program needs to ask the user to input a command line: eg - A+B=C. It will then output the following:
A
+
B
=
C.

Although this seems basic, my program needs to recognize different groups of characters and output them correctly. The following are examples my program needs to take into consideration:

1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343

Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.

2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C

3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]

4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6

4a)
D_1
=
Need ']' to close matrix

5) A regular sentence: Hello How are you
5a)
Hello
How
are
you

6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;

I have worked on a pseudocode for the main part of my program thus far, and I think I have all the functions named within it that I would need. I was wondering if someone could help me develop the functions correctly for my program, thanks!

Below is my pseudocode:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
    cout << "        Welcome to My Command Line Parser!" << endl;
    cout << "        XXXXXXXXXX, XXXXXXXXXXXXXXXXXXXXXX" << endl;
    cout << "                   XXXXXXXX, 2007\n" << endl;
    cout << "\n" << endl;
    cout << "Input a Command Line\n";
    cin.getline(buffer, 500);
        
    char piece[500];
    int st = 0, ed = 0, err_code = 1;

        while( buffer[st]!=0 )
        {
            st = find_first_element( );
            if (buffer[st] == 0) err_code = 0;

            ed = find_last_element( st ); // err_code can be set inside

            if (err_code == 0) break;
            else 
            {
                _copy_segment(piece, st, ed);
                _display_segment(piece);
                st = ed + 1;
            }
        }
        return err_code;

}

//Functions

Recommended Answers

All 5 Replies

1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343

Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.

Look at isalpha(), isalnum(), and isdigit() functions

2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C

isspace() for this.

3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]

Work on this last....

4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6

4a)
D_1
=
Need ']' to close matrix

Keep a counter for each pair of [, {, ( seen. Inc at open, dec at close. If by the end of the input any are not 0 you have a mismatch.

5) A regular sentence: Hello How are you
5a)
Hello
How
are
you

What about them? Would aple banena charry be acceptable as a sentence? I think this would be beyond the scope of your program.

6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;

Make your own isXXX() function, like isoperator() and you will have to define a set of rules and program for them.

commented: Good info; thanks WaltP :) - joeprogrammer +6

What about them? Would aple banena charry be acceptable as a sentence? I think this would be beyond the scope of your program.

Yes, aple benena charry would be acceptable as a sentence. my program doesn't have to recognize real words.

I am having trouble implementing all this with the current arrangement I have. I looked up the functions you suggested to me and understand what they do. But I am unclear as to how to implement them into my main function. am I going to have to do some sort of 'if' statement?

if isalpha() = 0
                  { cout << endl;
                }

I'm just really confused as to where I need to begin with this current program that I have and am not sure where to start. thanks!

Basically, yes. But use the proper syntax for your if

help! this is due in a couple of hours and I just have a problem with the matrix. even when the matrix is valid my code says it is invalid, can anyone help? thanks!

#include <iostream>


using std::cout;
using std::cin;
using std::endl;


bool Word_Tracker(char a);
bool Symbol_Digit_Word_Tracker(char a);
bool Operator_Checker(char a);
bool Number_Checker(char a);
bool Is_Matrix(char a);
bool Characters_Allowed_Matrix(char a);
bool Comma_Semicolon(char a);
void Clears(char a[], int size);
void First_Whitespace(char a[], int& pos);
void Display(char a[]);

int main()
{
    char string[500];
    cout << "          Welcome to My Command Line Parser!" << endl;
    cout << "        Raymond Guido, Northwestern University" << endl;
    cout << "                   Copyright, 2007\n" << endl;
    cout << "\n" << endl;
    cout << "Input a Command Line\n";
    cin.getline(string, 500);
        
    int x = 0;
    char word[500] = {0};
    char oper_ator[500] = {0};
    char number[500] = {0};
    char matrix[500] = {0};
    char comma_semicolon[500] = {0};
    

        while( string[x]!=0 )
        {
                First_Whitespace(string,x);

                if (Operator_Checker(string[x]) == true)
                {
                    while (Operator_Checker(string[x]) == true)
                    {
                    oper_ator[x]= string[x];
                    x ++;
                    }
                Display(oper_ator);
                Clears(oper_ator,500);
                }

                else if (Word_Tracker(string[x]) == true)
                {
                    while ((Word_Tracker(string[x]) == true || Symbol_Digit_Word_Tracker(string[x]) == true))
                    {
                    word[x]= string[x];
                    x ++;
                    }
                Display(word);
                Clears(word,500);
                }

                else if (Number_Checker(string[x]) == true)
                {
                    while (Number_Checker(string[x]) == true)
                    {
                        number[x]= string[x];
                        x ++;
                    }
                Display(number);
                Clears(number,500);
                }

                else if (Is_Matrix(string[x]) == true)
                {
                    while (Is_Matrix(string[x]) == true || Characters_Allowed_Matrix(string[x]) == true)
                    {
                    matrix[x]= string[x];
                    x ++;
                    }
                    if ( string[x] == ']')
                    {
                    matrix[x]= string[x];
                    x ++;
                    Display(matrix);
                    Clears(matrix,500);
                    }
                    else
                    {
                    cout << "Invalid Matrix" << endl;
                    break;
                    }
                }

                else if (Comma_Semicolon(string[x]) == true)
                {
                    while (Comma_Semicolon(string[x]) == true)
                    {
                    comma_semicolon[x]= string[x];
                    x ++;
                    }
                Display(comma_semicolon);
                Clears(comma_semicolon,500);
                }

                else
                {
                cout  << "\n" <<"You have entered invalid operators";
                break;
                }
        }


    cout  << "\n" << "Command Line Separated" << endl;
}
//Functions

void First_Whitespace(char a[], int& pos)
{
    while(a[pos]==' ')
    {
        pos ++;
    }
}
bool Comma_Semicolon(char a)
{
    bool b;
    if ((a == ';') || (a == ','))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Number_Checker(char a)
{
    bool b;
    if ((a == '.') || (a >= '0' && a <= '9'))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Operator_Checker(char a)
{
    bool b;
    if ((a == '+') || (a == '-') || (a == '*')
        || (a == '/') || (a == '='))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Word_Tracker(char a)
{
    bool b;
    if ((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z'))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Symbol_Digit_Word_Tracker(char a)
{
    bool b;
    if ((a == '.') || (a == ',') || (a== '_') || (a >= '0' && a <= '9'))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Is_Matrix(char a)
{
    bool b;
    if (a == '[')
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
bool Characters_Allowed_Matrix(char a)
{
    bool b;
    if ((a == ' ') || (a >= 0 && a <= 9) || (a == ';'))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}

void Clears(char a[], int c)
{
    for(int b=0; b<c; b++)
        a[b] = 0;
}

void Display(char a[])
{
    char *b = a;
    while( *b == 0)
    {
        b ++;
    }
    cout << b << endl;
}

You need to put single quotes around 0 and 9 for them to be treated as characters...

bool Characters_Allowed_Matrix(char a)
{
    bool b;
    if ((a == ' ') || (a >= '0' && a <= '9') || (a == ';'))
    {
        b = true;
        return b;
    }
    else
    {
        b = false;
        return b;
    }
}
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.