Question

C++ preffered please, thank you.

Image for Write a code in a high-level language (C++, C# or Java or ??) for a Deterministic Pushdown automaton which wil

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

#include <iostream>

#include <cstdlib>

#include <fstream>

#include <list>

#include <vector>

#include <map>

#include <sstream>

class PDA1

{

            public:

                        PDA1(string filename);

                        PDA1 ();

                        void isAccepted(string str);

            private:

                        void populateTransitionTable1(ifstream *p);

                        void initialOuput1(string s);

                        void output1(string s, string k, unsigned int i);

                        string showCurrentStack1();

                        void notAccepted1(string str);

                        int numStates1;

                        string endStates1;

                        string alphabet1;

                        list <string> s;

                        map<string, string> m;

                        map<string, int> moveNum1;

                        string currentState1;

};

int main ()

{

           

            ifstream inStream1;

            inStream1.open("grammer.txt");

            int nDPDA1;

            inStream1 >> nDPDA1;

            list<PDA1> generic1;

            for (int i = 0; i < nDPDA1; i++)

            {

                        string temp;

                        inStream1 >> temp;

                        generic1.push_back(PDA1(temp));

            }

           

            inStream1.close();

            return 0;

}

PDA1::PDA1(string filename1)

{

            ifstream file1;

            file1.open(filename1.c_str());

            if (file1.fail())

            {

                        cout << "Trouble Opening File\n";

                        exit(1);

            }

           

            file1 >> numStates1;

            file1 >> endStates1;

           

           

            file1 >> alphabet1;

           

            ifstream *pointer = &file1;

            populateTransitionTable1 (pointer);

            currentState1 = "0";

            s.push_back("Z0");

           

           

            char buff[256];

            file1.getline(buff, 256);

            string temp(buff);       

            while (! file1.eof())

            {

                        isAccepted(temp);

                        file1 >> temp;

            }

            file1.close();

}

void PDA1::populateTransitionTable1 (ifstream *p1)

{

            int n;

            *p1 >> n;

            string temp1;

            string key1= "";

            string value1 = "";

            for (int k = 0; k < n; k++)

            {

                       

                        for (int i = 0; i < 3; i++)

                        {

                                    *p1 >> temp1;

                                    key.append (temp1);

                        }

                        for (int i = 0; i < 2; i++)

                        {

                                    *p1 >> temp1;

                                    value1.append(temp1);

                        }

                        moveNum1.insert(pair<string, int>(key, k+1));

                        m.insert(pair<string, string>(key, value1));

                        key = "";

                        value1 = "";

            }

}

void PDA1::isAccepted(string str1)

{

           

            string c = str.substr(0);

            str.append("#");

            alphabet1.append("#");

            string temp1Key = "";

            string operation = "";

            initialOuput1(c);

            for (unsigned int i = 0; i < str.size(); i++)

            {

                       

                        unsigned int alpha = alphabet1.find((str.c_str())[i]);

                        if (alpha == string::npos)

                        {

                                    cout<<"(Crash)\n";

                                    break;

                        }

                       

                        temp1Key.append("(");

                        temp1Key.append(currentState);

                        temp1Key += (str.c_str())[i];

                        temp1Key.append(s.back());

                        temp1Key.append(")");

                       

                       

                        map<string, string>::iterator it = m.find(temp1Key);

                       

                        if (it != m.end())

                        {

                        stringstream out;

                        out << m.find(temp1Key)->second;

                        operation = out.str();

                        out.str("");

                        }

                        else

                        {         

                                    break;

                        }

                       

                        currentState1 = operation.c_str()[1];

                       

                       

                       

                        string decision = operation.substr(2);

                        unsigned int found = decision.find("^", 0);

                        if (found != string::npos)

                        {

                                    s.pop_back();

                        }

                        else

                        {

                                    string nextFind = "";

                                    nextFind+=(str.c_str())[i];

                                    nextFind+="Z0";

                                    found = decision.find(nextFind, 0);

                                    if (found != string::npos)

                                    {

                                                nextFind=str.c_str()[i];

                                                s.push_back(nextFind);

                                    }

                                    else

                                    {

                                                nextFind="";

                                                nextFind+=(str.c_str())[i];

                                                found = decision.find(nextFind, 0);

                                                if (found != string::npos)

                                                {

                                                            s.push_back(nextFind);

                                                }

                                    }

                        }

                        output1(c, temp1Key, i+1);

                       

                        temp1Key = "";

                        operation = "";

            }

            unsigned int isEnd = endStates1.find(currentState1,0);

            if (isEnd != string::npos)

            {

                        cout << "(Accepted)\n\n\n";

            }

            else

            {

                        cout << "(Not Accepted)\n\n\n";

            }

           

           

            currentState1 = "0";

            s.clear();

            s.push_back("Z0");

}

void PDA1::initialOuput1 (string c)

{

            cout << "(initially)\t"<<currentState<<"\t\t"<<c;

           

            if (c.size() < 8) cout<<"\t\t";

            else cout<< "\t";

           

            cout<<showCurrentStack1()<<"\n\n";

}

void PDA1::output1 (string c, string key,unsigned int iteration)

{

                        cout<< moveNum1.find(key)->second << "\t\t" << currentState1

                                    << "\t\t";

                        if (c.size() > iteration)

                        {

                                    cout << c.substr(iteration);

                                    if ((c.substr(iteration)).size() < 8)

                                                cout << "\t\t";

                                    else

                                                cout << "\t";

                        }

                        else { cout << "\t\t"; }

                        cout << showCurrentStack1() <<"\n";

}

string PDA1::showCurrentStack1 ()

{

            string stackState = "";

            list<string> temp1 (s);

            while (temp1.size() > 0)

            {

                        stackState.append(temp1.back());

                        temp1.pop_back();

            }

            return stackState;

}

Add a comment
Know the answer?
Add Answer to:
C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to...

    In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

  • NEED THIS SOON. Recursive Descent Parsing Consider the following BNF grammar: A -> I = E...

    NEED THIS SOON. Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P O P | P O -> + | - | * | / | ** P -> I | L | UI | UL | (E) U -> + | - | ! I -> C | CI C -> a | b | ... | y | z L -> D | DL D -> 0 | 1 | ......

  • In Java As in previous labs, you can enter all the necessary Java code fragments into...

    In Java As in previous labs, you can enter all the necessary Java code fragments into the DrJava Interactions pane. This will introduce you to reading input from a file and writing data to a file. As a warmup, we are going to read input from a String. Look up the StringReader class in the API. Create a StringReader object using the string "EECS 132". Then, use the read method to read each character, one at a time, from the...

  • Differentiate between machine code, low level language (assembly), and high level language (c)

    Differentiate between machine code, low level language (assembly), and high level language (c)

  • Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like...

    Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like C or Java. It does not need to run for me, but the code should be included in a text document called FirstnameLastnameHLA3.txt along with your assignment submission. Implement the program in MIPSzy Assembly language. Use the high level code as comments to the right of the Assembly code as the textbook does. If you write that MIPSzy...

  • . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın gua...

    . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın guage of all palindromes over alphabet {a, b} . Please give both the high-level description (text description or pseudo-code algorithm) and the low-level state transition diagram. Please analyze how many steps (one transition is considered as a step) it takes to accept an input palindrome with n symbols . Please design a deterministic Turing machine with two semi-infinite tapes for the same language. Please give...

  • This is a JAVA language The files provided in the code editor to the right contain...

    This is a JAVA language The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Note: DebugData2.txt does not need to be edited. It is used by the program. DebugData2.txt 435-9845 239-9845 981-9883 384-5656 875-3784 874-8120 DebugThirteen2.java // Program reads in a file of phone numbers without area codes // inserts "(312)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT