Question
Write code in c++ A compiler begins the compilation process by dividing a program into a set of delimited strings called tokens . This task
Write code in c++
A compiler begins the compilation process by dividing a program into a set of delimited strings
called tokens . This task is referred to as lexical analysis . For instance, given the C++ statement,
if ( j =>
lexical analysis by a C++ compiler produces the following ten tokens:
if ( j =>
Before you can perform lexical analysis, you need operations that support the input and output of
delimited strings. A pair of String ADT input/output operations are described below.
friend istream & operator >> ( istream &input,
String &inputString )
Requirements:
The specified input stream must not be in an error state.
Returns:
Extracts (inputs) a string from the specified input stream, returns it in inputString , and returns
the resulting state of the input stream. Begins the input process by reading whitespace (blanks,
newlines, and tabs) until a non-whitespace character is encountered. This character is returned as
the first character in the string. Continues reading the string character by character until another
whitespace character is encountered.
friend ostream & operator
const String &outputString )
Requirements:
The specified output stream must not be in an error state.
Returns:
Inserts (outputs) outputString in the specified output stream and returns the resulting state of
the output stream.
Note that these operations are not part of the String class. However, they do need to have access to
the data members of this class. Thus, they are named as friends of the String class.
Step 1: The file strio.cpp contains implementations of these string input/output operations. Add
these operations to your implementation of the String ADT in the file stradt.cpp .
Prototypes for these operations are included in the declaration of String class in the file
stradt.h .
Step 2: Create a program (stored in the file lexical.cpp ) that uses the operations in the
String ADT to perform lexical analysis on a text file containing a C++
program. Your program should read the tokens in this file and output each
token to the screen using the following format:
1 : [1stToken]
2 : [2ndToken]
...
This format requires that your program maintain a running count of the number of
tokens that have been read from the text file. Assume that the tokens in the text file
are delimited by whitespacean assumption that is not true for C++ programs in
general.
Step 3: Test your lexical analysis program using the C++ program in the file
progsamp.dat . The contents of this file are shown below.
void main ( )
{
int j ,
total = 0 ;
for ( j = 1 ; j =>
total += j ;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started