Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recursive Descent Parsing Consider the following BNF grammar: Using the techniqu

ID: 663464 • Letter: R

Question

Recursive Descent Parsing

Consider the following BNF grammar:

Using the technique described in class implement a recursive descent parser that recognizes strings in this language. Input should be from a file called input.txt and output should be to the console. An example session might look like this (these strings are not necessarily in the language):

You must implement the project in BOTH Java and C++! Implementations that do not include a solution in both languages will, at best, receive half credit. To simplify things you will not have to handle whitespace when parsing the string, i.e. " " and similiar are illegal characters in this language.

Explanation / Answer

Answer:

#include <stdio.h>

#include <fstream.h>

#include <iostream.h>

#include <string.h>

using std::endl;

using std::ifstream;

using std::ios;

using std::ofstream;

using std::string;

using namespace std;

bool A(void);

bool E(void);

bool T(void);

bool F(void);

bool P(void);

bool U(void);

bool I(void);

bool C(void);

bool L(void);

bool D(void);

string s1;

bool A(void)

{

    if (I())

    {

        if (s1[] == '=')

        {

            s1 = s1.sub_str();

            if (E())

            {

                return true;

            }

        }

        else

        {     

        return true;

        }

    }

    return false;

}

bool E(void)

{

    if (T())

    {

        if (s1[] == '+' || s1[] == '-')

        {

            s1 = s1.sub_str();

            if (E())

            {

                return true;

            }

        }

        else

        {

            return true;

        }

    }

    return false;

}

bool T(void) {

    if (F())

    {

        if (s1[] == '*' || s1[] == '/')

        {

            S1 = s1.sub_str();

            if (T())

            {

                return true;

            }

        }

        else

        {

            return true;

        }

    }     

    return false;

}

bool F(void)

{

    if (P())

    {

        if (s1[] == '^')

        {

            s1 = s1.sub_str();

            if (T())

            {

                return true;

            }

        }

        else

        {

            return true;

        }

    }

    return false;

}

bool P(void)

{

    if (I())

    {

        return true;

    }

    else if (L())

    {

        return true;

    }

    else if(U())

    {

        if (I() || L())

        {

            return true;

        }

    }

        else if (I())

        {

            return true;

        }

        else if(L())

        {

            return true;

        }

        else if(s1[] == '(')

        {

            s1=s1.sub_str();

            if (A())

            {

                if(s1[] == ')')

                    s1=s1.sub_str();

                return true;

            }

        }

        return false;

}

bool U(void)

{

    if (s1[] == '+' || s1[] == '-' || s1[] == '!')

    {

        s1=s1.sub_str();

        return true;

    }

    return false;

}

bool I(void)

{

    if (C())

    {

        return true;

    }

    if (C())

    {

        if(I())

        {

            return true;

        }

    }

    return false;

}

bool C(void) {

    if ('a' <= s1[] && s1[] <= 'z')

    {

        s1 = s1.sub_str();

        return true;

    }

    return false;

}

bool L(void)

{

    if(D())

    {

        return true;

    }

    if(D())

    {

        if(L())

        {

            return true;

        }

    }

    return false;

}

bool D(void)

{

    if ('' <= s1[] && s1[] <= '')

    {

        s1 = s1.sub_str();

        return true;

    }

    return false;

}

int main(int argc, char *argv[])

{

    s1 = argc == ? argv[] : "";

    

    ifstream fin("inp.txt");

    string buffer;

        

    while (fin >> buffer)

    {

        

           cout << "THE STRING READ FROM FILE: ";

               cout << buffer << endl;

        for(int i=; i<argc;i++)

        {

            if (A() && s1[i] == s1.length())

            {

cout << "THE STRING "" << argv[i] << "" IS IN THE LANGUAGE" << endl;

            }

            else

            {

cout << "THE STRING "" << argv[i] << "" IS NOT IN THE LANGUAGE" << endl;

            }

        

        }

    }

    fin.close();

    getchar();

        return ;

}

Input.txt:

a=a+b-c*d

a=a**b++c

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote