Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DFA that reads in two text files that are labeled dfas.txt and trs.txt dfas.txt has this in it : token1 0 1 token2 2 3

DFA that reads in two text files that are labeled "dfas.txt" and "trs.txt"

dfas.txt has this in it :

token1 0 1 token2 2 3

and trs.txt has this in it :

0 1 -1 -1 -1 -1 -1 -1 -1 -1 2 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

DFA will read both files and exclude all the -1's and the output will look like this:

"States are 0 to 9 and chars are a to d a b c d State 0:0 1 State 1: State 2: 2 3 State 3: State 4: State 5: State 6: State 7: State 8: State 9: token1: 0 is start and ends in 1 token2: 2 is start and ends in 3

@@Enter a string: ab Trying dfa 0-------- state: 0 char: a new state: 0 state: 0 char: b new state: 1 Found token token1 do control-C to quit @@Enter a string: abb Trying dfa 0-------- state: 0 char: a new state: 0 state: 0 char: b new state: 1 state: 1 char: b new state: -1 Trying dfa 1-------- state: 2 char: a new state: -1 Lexical error! do control-C to quit"

---------------------------------------------------------------------

Here is the code below

// fill in the functions with " ** "

#include #include #include #include using namespace std;

// info on each DFA struct info { string name; // token name int startstate; int finalstate; };

info DFAs[4]; // store up to 4 dfas' start and final int TRS[10][4]; // store all trs's - states 0-9 and chars a b c d -- all dfas transitions are in here

// ----- utility functions -----------------------

int readTables() {

ifstream fin ("trs.txt", ios::in); ifstream fin2 ("dfas.txt", ios::in); // ** Read in the files into TRS and DFAs // ** Return how many DFAs were read }

void displayTables() { // ** display DFAs nicely labeled // ** display TRS nicely labeled }

bool accept(info dfa, string word) { // ** Does the dfa accept the word? // Start with the start state of the DFA and // look up the next state in TRS for each char in word. // At the end of the word, make sure you are in the // final state of the DFA. // Use a formula to convert chars to TRS col numbers. }

int main() { cout << "This is a table driven scanner. Needs trs.txt and dfas.txt." << endl; cout << "States are 0 to 9 and chars are a to d" << endl;

int numDFA = readTables(); // how many DFAs were read displayTables(); cout << ".....done reading tables...." << endl;

string word; while(true) { cout << "@@Enter a string: " ; cin >> word; // ** try the DFAs one by one and see // if the word is accepted // if so, display the word and the token name // ** if no DFA does, generate a lexical error message. cout << "do control-C to quit" << endl; }

}//the end

Any help would be appreciated!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions