Table of symbols

Hey,

I am trying to figure out how to print out a table of symbols into the console.

I have an input file that has a predefined form such as this

1
2
3
4
5
6
7
8
int main ( )
{
 int var1 =  10 ;
 long var2 = 5 ;
 double var6 ;
 var1 ++ ;
 return 0 ;
}


where everything is delimited by space.

1. I had to count the number of occurrences of each
- variables - ( int,char,char* etc.) + those declared as parameters in func
- functions - only their declaration not callings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 if (file.is_open())
    {
        while (getline(file,line))
        {         
            std::stringstream  linestream(line);    
            // if token == keyword .... var ++      
            while (linestream >> token){  
                if (token == "int" || token == "byte" || token == "short" || token == "char" || token == "float" || 
                token == "long" || token == "struct" || token == "int*" || token == "byte*" || token == "short*" || token == "char*" ||
                token == "float*" || token == "long*" || token == "void" ) {  
                    varCount++;
                }
            }   
            // if line cointains keyword ... check if line contains ( but not ;  ... to be a function 
            if (line.find("void") != string::npos || line.find("int") != string::npos || line.find("double") != string::npos ||
            line.find("short") != string::npos || line.find("long") != string::npos || line.find("float")  != string::npos || 
            line.find("char") != string::npos || line.find("struct") != string::npos || line.find("int*") != string::npos || 
            line.find("byte*") != string::npos || line.find("short*") != string::npos || line.find("char*") != string::npos ||
             line.find("float*") != string::npos || line.find("long*") != string::npos)
            {   
                if (line.find("(") != string::npos && line.find(";") == string::npos) 
                { 
                    funcCount++; 
                    varCount--; 
                } 
            } 
        }



so I would use struct and in there I would store information about that one variable/function:
1
2
3
4
5
6
struct table {
    string name; // name of func / var
    int lineNumber;   // where it was encountered 
    string identifier; // is it function or variable
    string type; // type of variable or function 
}


then probably put it into a vector of structs and later call it from there to display similar output:
1
2
3
4
 main, line 1, function, int
 var1, line 3, variable, int
 var2, line 4, variable, long
 var6, line 5, variable, double


The thing is I cannot figure out how to get those details about variables/functions into the struct.


I would appreciate any advice you could give
Last edited on
Hello Lexigon,

It would be helpful if you post a more complete program that can be compiled and run. Also post the actual input file, so everyone will be using the same information.

Right now it is mostly guess work of how all the code fits together and what you are reading.

Andy
L8 - 10 and L15 - 19. It would be far easier to store the required strings to search for in say a std::set. This then makes the test a 1 liner and it's much easier to add new search strings.

Perhaps something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <set>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

int main()
{
	const std::set<std::string> token {"int", "byte", "short", "char", "float", "long", "struct", "int*", "byte*", "short*", "char*",
				"float*", "long*", "void", "double"};

	std::ifstream ifs("prog.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	size_t varCount {};
	size_t funcCount {};

	for (std::string line; std::getline(ifs, line); ) {
		std::istringstream iss(line);

		for (std::string word; iss >> word; )
			if (token.find(word) != token.end())
				if (line.find("(") != std::string::npos && line.find(";") == std::string::npos)
					++funcCount;
				else
					++varCount;
	}

	std::cout << "\nVariables: " << varCount;
	std::cout << "\nFunctions: " << funcCount;
}


which for the given program in the first post, displays:


Variables: 3
Functions: 1

Last edited on
Topic archived. No new replies allowed.