Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

fain_mytoken.txt that has: abbb a bb abb to see what it does. This program accepts a b^+ as mytoken. Change this program with new FA

fain_mytoken.txt that has:

abbb

a

bb

abb to see what it does.

This program accepts a b^+ as mytoken.

Change this program with new FA functions (follow ** comments)

to accept the following:

function ident_token l ( l | d | _ )^*

function real_token d^* . d^+

function int_token d^+

Note: l can be either a or b

d can be either 2 or 3

You should not handle other digits and characters.

Main calls Scanner repeatedly.

Scanner grabs another word from the input file.

Scanner calls ident_token, real_token and int_token in this order until

one of them returns TRUE.

Scanner gives back the token type and the word to Main.

Main has to display the word and the token type.

Requirements: Must add REs as comments

Do not remove tracing messages

Must test the program again with the input file fain.txt containing the following:

ab_2a - ident

a_b_2_a - ident

.23 - real

23.3 - real

23 - int

ab&c - bad

23.6 - bad

2a3 - bad

22..2 - bad

23. bad

========================================================================================================================================

#include

#include

#include

using namespace std;

// ** Change this to fit the HW specification - look for **

// ** Must have the same types of tracing couts as mytoken.

// ** Must complete the ** comments with RE

//-----------------------------------------------------------------

// MYTOKEN DFA done and has a sample

// This FA is for a b^+

bool mytoken(string s)

{

int state = 0;

int charpos = 0;

cout << "Trying the mytoken machine..." << endl

while (s[charpos] != '\0')

{

cout << "current state: " << state << endl;

cout << "character: " << s[charpos] << endl;

if (state == 0 && s[charpos] == 'a')

state = 1;

else

if (state == 1 && s[charpos] == 'b')

state = 2;

else

if (state == 2 && s[charpos] == 'b')

state = 2;

else

{

cout << "I am stuck in state " << state << endl;

return(false);

}

charpos++;

}//end of while

// where did I end up????

if (state == 2) return(true); // end in a final state

else return(false);

}

// IDENT DFA

// This FA is for **

bool ident_token(string s)

{

// ** complete this based on mytoken

}

// REAL DFA

// This FA is for **

bool real_token(string s)

{

// ** complete this based on mytoken

}

//INT DFA

// This FA is for **

bool integer_token(string s)

{

// ** complete this based on mytoken

}

// -----------------------------------------------------

enum tokentype {ERROR, MYTOKEN, IDENT, REAL, INT};

int scanner(tokentype& , string& ); // to be called by main

fstream fin; // file stream to use

// The test-driver

int main()

{ string fname;

cout << "Enter the input file name:";

cin >> fname;

fin.open( fname.c_str(), fstream::in);

string tokens[5] = {"ERROR", "MYTOKEN", "IDENT", "REAL", "INT"};

tokentype thetype;

string theword;

while (true) // keep on going

{

scanner(thetype, theword); // the paramers will be set by Scanner

if (theword == "EOF") break;

cout << "Type is:" << tokens[thetype] << endl;

}

fin.close();

}// end of main

// sets the_type and w

int scanner(tokentype& the_type, string& w)

{

// This goes through all machines one by one on the input string w

cout << ".....Scanner was called..." << endl;

fin >> w; // grab next word from fain.txt

cout << "Word is:" << w << endl;

if (mytoken(w))

{ the_type = MYTOKEN; }

// ** add other if-then's here in the right order to go through

// ** all FAs one by one and set the_type to be IDENT, REAL or INT.

else //none of the FAs returned TRUE

{ cout << "Lexical Error: The string is not in my language" << endl;

the_type = ERROR; }

}//the very end of scanner

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

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions