CS-285 Data Structures & Algorithms

Lab #7

Cross-Reference Generator (Maps & Multimaps)

 

 

Date:            Thursday, November 1, 2001

Due:    Friday, November 9, 2001

 

The purpose of this program is to use the STL multimap container to store identifiers and line numbers from a typical C++ program.  The program will need to scan an input file containing C++ source code, parse out the identifiers, and store the line number on which the identifier was found, in the map.  While the program is parsing the input file, it can write out a source file listing that simply “prepends” a line number to each source line encountered in the input file (see the example below).  When the end of the source file is found, the program will then generate an alphabetical cross-reference listing of all the identifiers found in the program.  After each identifier, a list of the source lines on which the identifier appears is written.  The following example shows sample output produced by the Cross Reference Generator.  In order to make the job of writing this program easier, I’ve placed a sample program on the course web page that demonstrates the use of maps in the STL.

 

Input:

 

#include <iostream>

using namespace std;

 

void main()

{

    cout << “hello world” << endl;

    cout << endl;

}

 

 

Output:

 

1              #include <iostream>

2              using namespace std;

3

4              void main()

5              {

6                  cout << “hello world” << endl;

7                  cout << endl;

8              }

 

Cross Reference:

cout                                        6,                7

endl                                        6,                7

include                                   1

iostream                                 1

main                                        4

namespace                            2

std                                          2

using                                      2

void                                        4