473,513 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class to support keywords.

I want to write a class that supports operations on keywords belonging to
the C++ programming language. The following program repeatedly prompts the
user for a keyword until 'explicit' is finally entered:

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

What would a class that supports the functionality of KeyWord in the above
program look like?What are the details of the implementation of such a
class? How many changes to the implementation would be required if another
set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion. Thanks.
Jul 23 '05 #1
14 2326
Jason Heyes wrote:
I want to write a class that supports operations on keywords belonging to
the C++ programming language. The following program repeatedly prompts the
user for a keyword until 'explicit' is finally entered:

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

What would a class that supports the functionality of KeyWord in the above
program look like?What are the details of the implementation of such a
class? How many changes to the implementation would be required if another
set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion. Thanks.


// KeyWord.h

#include <iostream>
#include <string>

typedef std::string KeyWord;
Jul 23 '05 #2
* Jeff Schwab:

// KeyWord.h

#include <iostream>
#include <string>

typedef std::string KeyWord;


Well, you don't need <iostream> there.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
Jason Heyes wrote:

I would appreciate help in the form of code as well as discussion. Thanks.


Maybe its just me.
But from your description, it is not clear to me what you want to achieve.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4
Alf P. Steinbach wrote:
* Jeff Schwab:
// KeyWord.h

#include <iostream>
#include <string>

typedef std::string KeyWord;

Well, you don't need <iostream> there.


Right you are. I copied that from the top of my "make sure it works
before I post it" file.

// KeyWord.h -- Second Edition

#include <string>

typedef std::string KeyWord;
Jul 23 '05 #5
Karl Heinz Buchegger wrote:
Jason Heyes wrote:
I would appreciate help in the form of code as well as discussion. Thanks.

Maybe its just me.
But from your description, it is not clear to me what you want to achieve.


Looks like homework to me.
Jul 23 '05 #6
Jason Heyes wrote:
I want to write a class that supports operations on keywords belonging to the C++ programming language. The following program repeatedly prompts the user for a keyword until 'explicit' is finally entered:

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

What would a class that supports the functionality of KeyWord in the above program look like?What are the details of the implementation of such a class? How many changes to the implementation would be required if another set of keywords was to be used instead?


What you've shown so far doesn't seem to require anything that isn't
already present in std::string. You can look up details of its design
interface in the C++ standard (available from webstore.ansi.org). A
number of C++ standard libraries have source code available, making it
possible (if not necessarily easy) to study their implementations.

Changes to the class _should_ be required primarily if the definition
of the KIND of string that could constitute a key word changed, NOT
simply when the set of key words changed (e.g. keywords that can
included spaces would call for a different definition of operator>>
than std::string has). Of course, this is based on how you've used it
above -- different use might change the requirement(s) -- in
particular, it might be reasonable to have the Keyword class "know"
what's a keyword and what's not, in which case you'd have to do
_something_ to let it know -- but personally, I'd leave define the
class as an "engine" that used external data to define the set of key
words to be recognized.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #7
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

What you've shown so far doesn't seem to require anything that isn't
already present in std::string. You can look up details of its design
interface in the C++ standard (available from webstore.ansi.org). A
number of C++ standard libraries have source code available, making it
possible (if not necessarily easy) to study their implementations.

Changes to the class _should_ be required primarily if the definition
of the KIND of string that could constitute a key word changed, NOT
simply when the set of key words changed (e.g. keywords that can
included spaces would call for a different definition of operator>>
than std::string has). Of course, this is based on how you've used it
above -- different use might change the requirement(s) -- in
particular, it might be reasonable to have the Keyword class "know"
what's a keyword and what's not, in which case you'd have to do
_something_ to let it know -- but personally, I'd leave define the
class as an "engine" that used external data to define the set of key
words to be recognized.


I don't want to "tell" a KeyWord object the set of C++ keywords (e.g., for,
while, do, if, etc). It should already know. I would especially like to
extract a C++ keyword from a stream with complete ignorance (i.e., not
knowing the set of C++ keywords).
Jul 23 '05 #8

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

What you've shown so far doesn't seem to require anything that isn't
already present in std::string. You can look up details of its design
interface in the C++ standard (available from webstore.ansi.org). A
number of C++ standard libraries have source code available, making it
possible (if not necessarily easy) to study their implementations.

Changes to the class _should_ be required primarily if the definition
of the KIND of string that could constitute a key word changed, NOT
simply when the set of key words changed (e.g. keywords that can
included spaces would call for a different definition of operator>>
than std::string has). Of course, this is based on how you've used it
above -- different use might change the requirement(s) -- in
particular, it might be reasonable to have the Keyword class "know"
what's a keyword and what's not, in which case you'd have to do
_something_ to let it know -- but personally, I'd leave define the
class as an "engine" that used external data to define the set of key
words to be recognized.


I don't want to "tell" a KeyWord object the set of C++ keywords (e.g.,
for, while, do, if, etc). It should already know. I would especially like
to extract a C++ keyword from a stream with complete ignorance (i.e., not
knowing the set of C++ keywords).


Umm, ok... but if you don't give your class the list of keywords that are
valid, then how is it supposed to know them??? There is no part of the
run-time library that includes that list for you, so it's impossible for
your class to know if a word is a "keyword" or not, unless you give it that
information... somehow!

The parsing engines in C++ compilers don't magically "know" what's a valid
C++ keyword; it's built into the parser somehow. One way to do that is to
hard-code all the possible keywords, and rules for when/how they can appear
and what they mean in that context. Another method is to use tools like lex
and yacc, which allow you to specify the keywords and grammar rules. But
that's a lot of work, and it takes a while to learn how to use such tools.

A simple, flexible model would be one where you call a member function of
your class to cause it to read a set of valid keywords from a file. Then,
to change what keywords are valid, all you'd need is a new file.

But you still haven't fully explained what you're trying to accomplish.
(Was this a homework problem, as suggested elsewhere?)

-Howard

Jul 23 '05 #9
Jason Heyes wrote:
I want to write a class that supports operations on keywords belonging to
the C++ programming language. The following program repeatedly prompts the
user for a keyword until 'explicit' is finally entered:

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

What would a class that supports the functionality of KeyWord in the above
program look like?What are the details of the implementation of such a
class? How many changes to the implementation would be required if another
set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion. Thanks.

using std::istream;
using std::ostream;

class KeyWord
{
public:

// Here is the declaration for the extraction operator.
friend istream& operator<<(istream&, Keyword& k);

// Might as well have the insertion operator too.
friend ostream& operator>>(ostream&, const Keyword&);

// The main() function requires the following operator:
bool operator!=(const char * text) const;

// If you have != you might as well have == too.
// (many implementations have != implemented in terms
// of ==.
bool operator==(const char * text) const;

// The main program indirectly states that there is a
// default (no parameter) constructor. The compiler
// can generate it for you, but you may want to be
// explicit and create one.
KeyWord(const char * new_text = 0);

// Don't forget the copy constructor too.
Keyword(const Keyword& kw);

// Yep, and the destructor:
virtual ~Keyword();

// If you declare the three above, you should also
// declare assignment:
Keyword& operator=(const Keyword& kw);
};

Well, this answers the first question, from observation
in the main() function.

Can you (the O.P.) post the implementation of these
functions?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #10
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:1z*******************@newssvr31.news.prodigy. com...
Jason Heyes wrote:
I want to write a class that supports operations on keywords belonging to
the C++ programming language. The following program repeatedly prompts
the user for a keyword until 'explicit' is finally entered:

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

What would a class that supports the functionality of KeyWord in the
above program look like?What are the details of the implementation of
such a class? How many changes to the implementation would be required if
another set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion.
Thanks.

using std::istream;
using std::ostream;

class KeyWord
{
public:

// Here is the declaration for the extraction operator.
friend istream& operator<<(istream&, Keyword& k);

// Might as well have the insertion operator too.
friend ostream& operator>>(ostream&, const Keyword&);

// The main() function requires the following operator:
bool operator!=(const char * text) const;

// If you have != you might as well have == too.
// (many implementations have != implemented in terms
// of ==.
bool operator==(const char * text) const;

// The main program indirectly states that there is a
// default (no parameter) constructor. The compiler
// can generate it for you, but you may want to be
// explicit and create one.
KeyWord(const char * new_text = 0);

// Don't forget the copy constructor too.
Keyword(const Keyword& kw);

// Yep, and the destructor:
virtual ~Keyword();

// If you declare the three above, you should also
// declare assignment:
Keyword& operator=(const Keyword& kw);
};

Well, this answers the first question, from observation
in the main() function.

Can you (the O.P.) post the implementation of these
functions?


Someone at University helped me write an extractor that does the job:

std::istream &KeyWord::operator>>(std::istream &is, KeyWord &keyword)
{
std::string s;
if (!(is >> s))
{
std::cout << "Failed to read string of KeyWord" << std::endl;
return is;
}

if (!(s == "do" || s == "while" || s == "explicit" /* || ... */))
{
std::cout << "String " << s << " does not describe a valid KeyWord"
<< std::endl;
is.setstate(is.failbit);
}

return is;
}

This isn't satisfactory due to the big if-statement. See, I would like to
write KeyWord classes for many other languages like Pascal, C, etc. I need
something more elegant than a giant if-statement.
Jul 23 '05 #11
Jason Heyes wrote:

Someone at University helped me write an extractor that does the job:

std::istream &KeyWord::operator>>(std::istream &is, KeyWord &keyword)
{
std::string s;
if (!(is >> s))
{
std::cout << "Failed to read string of KeyWord" << std::endl;
return is;
}

if (!(s == "do" || s == "while" || s == "explicit" /* || ... */))
{
std::cout << "String " << s << " does not describe a valid KeyWord"
<< std::endl;
is.setstate(is.failbit);
}

return is;
}

This isn't satisfactory due to the big if-statement. See, I would like to
write KeyWord classes for many other languages like Pascal, C, etc. I need
something more elegant than a giant if-statement.


Well, obviously the list of valid keywords must then come from elsewhere.
Eg. you can setup a std::vector or a std::map in the KeyWord class, which
contains all the valid keywords. The operator then checks if the just read
string is contained in that vector or map. If yes -> string is a valid
keyword.

How this vector is set up is completely left to your imagination. You can
eg. put them in in the constructor and have them hard coded in the source
file, or you can read them from a file or ...

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #12
Jason Heyes wrote:

Someone at University helped me write an extractor that does the job:

std::istream &KeyWord::operator>>(std::istream &is, KeyWord &keyword)
{
std::string s;
if (!(is >> s))
{
std::cout << "Failed to read string of KeyWord" << std::endl;
return is;
}

if (!(s == "do" || s == "while" || s == "explicit" /* || ... */))
{
std::cout << "String " << s << " does not describe a valid KeyWord"
<< std::endl;
is.setstate(is.failbit);
}

return is;
}

This isn't satisfactory due to the big if-statement. See, I would like to
write KeyWord classes for many other languages like Pascal, C, etc. I need
something more elegant than a giant if-statement.


Vectors and arrays.

/* in source file */
#include <string>
using std::string;

static const string keyword_list[] =
{
string("static"), string("const"),
string("using"), string("if"),
string("while"), string("do"),
string("goto"), string("for")
}
static const unsigned int Words_In_List =
sizeof(keyword_list) / sizeof(keyword_list[0]);

bool is_keyword(const string& word)
{
bool found = false;
for (unsigned int i = 0;
!found && (i < Words_In_List); ++i)
{
found = word == keyword_list[i];
}
return found;
}

If the keywords are sorted, you can use a
faster search, such as std::upper_bound,
std::lower_bound, or std::binary_search.

You may also want to use a vector, since
vectors can grow dynamically at run-time.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #13
In message <42***********************@news.optusnet.com.au> , Jason Heyes
<ja********@optusnet.com.au> writes
[...]
if (!(s == "do" || s == "while" || s == "explicit" /* || ... */))
{
std::cout << "String " << s << " does not describe a valid KeyWord"
<< std::endl;
is.setstate(is.failbit);
}

return is;
}

This isn't satisfactory due to the big if-statement. See, I would like to
write KeyWord classes for many other languages like Pascal, C, etc. I need
something more elegant than a giant if-statement.


Look up std::set, particularly its count() function.

If you want to associate behaviour with the keys, try std::map.

--
Richard Herring
Jul 23 '05 #14
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...

Well, obviously the list of valid keywords must then come from elsewhere.
Eg. you can setup a std::vector or a std::map in the KeyWord class, which
contains all the valid keywords. The operator then checks if the just read
string is contained in that vector or map. If yes -> string is a valid
keyword.

How this vector is set up is completely left to your imagination. You can
eg. put them in in the constructor and have them hard coded in the source
file, or you can read them from a file or ...


Ok I am satisfied. Thanks for your help.
Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1760
by: Jason Heyes | last post by:
This is a revised version of a post entitled "Class to support keywords". Please reply to this post instead of the old one. The following program repeatedly prompts the user for C++ keywords...
2
2267
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any...
1
1457
by: Julia | last post by:
Hi I can see that .NET have System.Diagnostics.StackTrace class and I wonder can it be used to log the stack including values which was passed to the function? can i use reflection(some...
10
3258
by: Jon | last post by:
I'm investiging multi-threaded GUI applications; specifically the technique used to ensure that controls are only modified under the correct thread. The standard technique is to use ...
7
13338
by: semedao | last post by:
Hi, I am using cryptostream on both sides on tcp connection that pass data. I am also use asyc socket , so , the data that recieved in the callback method not always have the length of the buffer...
3
2932
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I'm well into a project at the moment which uses a MasterPage. I'm also using seperate code-behind files for each page (C#). I just realised that the MasterPage doesn't give you a...
7
3377
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
5
2648
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
How can I get the list of connection string's keywords available in sqlclient programmatically? I have found the list in here...
4
2655
by: Looch | last post by:
All, I created a class in a windows application and I'd like to be able to pass an instance of that class as a method parameter to a web service. The class contains 100 variables. I tried adding...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.