I need a program that opens a sample program and counts the number of reserved words, operands and operators. Operands and operators are easy to distinguish. My problem is how do I determine the reserved words there?

One solution I thought of is to create a separate text file that has all the reserved words in them to be used as a cross reference. Do you think this will work and if you have a better idea, I'd be glad to listen. Thanks.

P.S. I also would like to know what it means when you add the word unique as I'm tasked to find reserved words along with unique reserved words, operators along with unique operators and operands along with unique operands. Thanks again.

6 Years
Discussion Span
Last Post by firstPerson

Just create a std::map with all of the reserved words, and check for them using that.
And for your post script, unique just means there is only one of.


Map info: http://www.cplusplus.com/reference/stl/map/

It'll work but another (perhaps less effective solution) is to use string.compare() to compare words from your file against reserved words. However this will take a lot more ressources and is far from effective. Just pointing out another way to do it.


Hi, I don't understand really how to use maps. I've already finished this by using a separate txt file and the line.find function in fstream. If you don't mind though, can you maybe explain maps a bit more or provide a simple example so I can use it in the future.


An old snippet -from here(excuse my amateur style, back then)-:

map<string, Element> elements;
    elements["H"]._Element(true, false, false, 1, 1, 1);
    elements["H"].name   = "Hydrogen";
    elements["H"].symbol = "H";
    elements["H"].amu    = 1.00794;

Do you know how to use any other STL containers or pointers/arrays?


Pointers and arrays, yes. Just the simple data structures. As for STL containers, well you lost me there. I'm a computer engineer so I deal mostly with hardware and ASM. Just doing this for a friend though.

Thanks for the snippet. Will go over it in a while.


Actually forget the map approach. For small set of data it might be better to use just a sorted array. For example :

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

class KeywordDictionary{		
	std::vector<string> keyWords;
	KeywordDictionary(){ _init();}
	bool isKeyword(const std::string& word)const{ return _isKeyword(word); }
	void _init(){
		//and more
	//check if the passed word is a keyword
	//The function does not care about lower/upper case
	bool _isKeyword(const std::string& word)const{		
		struct NoCaseCompare{
			bool operator()(const std::string& lhs, const std::string& rhs){
				string lhsCpy(lhs), rhsCpy(rhs);
				return lhsCpy < rhsCpy;
		return std::binary_search(keyWords.begin(),keyWords.end(),word,NoCaseCompare());

int main() {  	
	KeywordDictionary keywords;
	cout << keywords.isKeyword("IF") << " " << keywords.isKeyword("For") << " " << keywords.isKeyword("null") << endl;

and later on, you can change the underlying implementation without breaking the clients code, if you choose to.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.