473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Design pattern question

Hello,

I'm trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I'd like to achieve is a "factory" which would return the proper
class depending on the content type received, so I'd call this factory
like factory.create("html");, etc

The problem I'm facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn't have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.

Jun 5 '07 #1
5 2419
al******@gmail.com wrote:
I'm trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I'd like to achieve is a "factory" which would return the proper
class depending on the content type received, so I'd call this factory
like factory.create("html");, etc

The problem I'm facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn't have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.
Write the class that uses the parser first. Write it in such a way that
it will work no matter what format the text is in. Then your problem
will be solved.

Think more abstractly.
Jun 5 '07 #2
al******@gmail.com wrote:
Hello,

I'm trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I'd like to achieve is a "factory" which would return the proper
class depending on the content type received, so I'd call this factory
like factory.create("html");, etc

The problem I'm facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn't have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.
Ok there are a couple issues revealed here:
1) From your description, you are writing a lexical analyzer (scanner
and tokenizer), do not confuse a parser with a lexical analyzer. Start
from here:
http://en.wikipedia.org/wiki/Semanti...ter_science%29
Know exactly what you want and define your problem. Do you want a lexer
or a parser?

2) A factor method works best when parallel hierarchies exists, you can
formulate your hierarchies into a lexer hierarchy and a token hierarchy.
Then a lexer class can create a token class through covariance and
virtual methods:
class lexer{
public:
virtual token * create_token(const attr & at){
return new token(at);
}
virtual ~lexer(){};
};
class html : virtual public lexer {
public:
html_token * create_token(const attr & at){
return new html_token(at);
}
}
};

class token {
public:
token(const attr & at){ // construct a token
}
};
class html_token : virtual public token{
pubilc:
html_token(const attr & at){ // construct a html token
}
};
3) There is nothing wrong with if/else/switch constructs. In fact IMO
that's only way to can initialize objects dynamically within the realm
of C++. You can use platform specific feature, i.e. dynamic library and
name resolution, to facilitate a more generic solution but that does not
have anything to do with C++.
Jun 5 '07 #3
Hi, thanks a lot for your answer..
Ok there are a couple issues revealed here:
1) From your description, you are writing a lexical analyzer (scanner
and tokenizer), do not confuse a parser with a lexical analyzer. Start
from here:http://en.wikipedia.org/wiki/Semanti...ter_science%29
Know exactly what you want and define your problem. Do you want a lexer
or a parser?
Yes, thanks for letting me know. I didn't have enough knowledge on the
separation of these processes.
Actually, it seems I need both of them..

The idea is basically being able to interpret a http header, and also
other protocols, say rtsp to put an example.
Both have similarities, but there are some things specific to each
protocol.
Am I wrong if I say that in most cases the analyzing and parsing is
done in the same place?

My idea is simply being able to get a stream of data and then build a
data structure holding the different headers,
with its fields and attributes.. From what I think, I don't think I
need to make a lex analyzer and then a parser.. I could do
obth things at the time?
2) A factor method works best when parallel hierarchies exists, you can
formulate your hierarchies into a lexer hierarchy and a token hierarchy.
Then a lexer class can create a token class through covariance and
virtual methods:
class lexer{
public:
virtual token * create_token(const attr & at){
return new token(at);
}
virtual ~lexer(){};};

class html : virtual public lexer {
public:
html_token * create_token(const attr & at){
return new html_token(at);
}
}

};

class token {
public:
token(const attr & at){ // construct a token
}};

class html_token : virtual public token{
pubilc:
html_token(const attr & at){ // construct a html token
}};

3) There is nothing wrong with if/else/switch constructs. In fact IMO
that's only way to can initialize objects dynamically within the realm
of C++. You can use platform specific feature, i.e. dynamic library and
name resolution, to facilitate a more generic solution but that does not
have anything to do with C++.
Thanks. I definetly need to give this more thought, or learn existing
examples to see how they do this.

Jun 5 '07 #4
al******@gmail.com wrote:
Hi, thanks a lot for your answer..
>Ok there are a couple issues revealed here:
1) From your description, you are writing a lexical analyzer (scanner
and tokenizer), do not confuse a parser with a lexical analyzer. Start
from here:http://en.wikipedia.org/wiki/Semanti...ter_science%29
Know exactly what you want and define your problem. Do you want a lexer
or a parser?

Yes, thanks for letting me know. I didn't have enough knowledge on the
separation of these processes.
Actually, it seems I need both of them..

The idea is basically being able to interpret a http header, and also
other protocols, say rtsp to put an example.
Both have similarities, but there are some things specific to each
protocol.
Am I wrong if I say that in most cases the analyzing and parsing is
done in the same place?
They work this way: a parser is fed by a lexer. Usually a token
signifies a start of a syntactic block that can be analyzed by a parser.
They are not done in the same place (I assume you meant lexical and
syntactic analysis)

Check EBNF and Boost::spirit. In your case I don't think you need a
recursive descent parser. A simple EBNF parser is most likely adequate.
>
My idea is simply being able to get a stream of data and then build a
data structure holding the different headers,
with its fields and attributes.. From what I think, I don't think I
need to make a lex analyzer and then a parser.. I could do
obth things at the time?
>2) A factor method works best when parallel hierarchies exists, you can
formulate your hierarchies into a lexer hierarchy and a token hierarchy.
Then a lexer class can create a token class through covariance and
virtual methods:
class lexer{
public:
virtual token * create_token(const attr & at){
return new token(at);
}
virtual ~lexer(){};};

class html : virtual public lexer {
public:
html_token * create_token(const attr & at){
return new html_token(at);
}
}

};

class token {
public:
token(const attr & at){ // construct a token
}};

class html_token : virtual public token{
pubilc:
html_token(const attr & at){ // construct a html token
}};

3) There is nothing wrong with if/else/switch constructs. In fact IMO
that's only way to can initialize objects dynamically within the realm
of C++. You can use platform specific feature, i.e. dynamic library and
name resolution, to facilitate a more generic solution but that does not
have anything to do with C++.

Thanks. I definetly need to give this more thought, or learn existing
examples to see how they do this.
Jun 5 '07 #5
In article <f4**********@aioe.org>, fe****@aepnetworks.com says...

[ ... ]
Check EBNF and Boost::spirit. In your case I don't think you need a
recursive descent parser. A simple EBNF parser is most likely adequate.
There seems to be a bit of confusion here. EBNF and recursive descent
are orthogonal. EBNF is a language in which you express a grammar --
i.e. you specify the language that will be accepted by the parser.

Recursive descent is a method of implementing a parser. A recursive
descent parser is a top-down parser. As you'd guess when it's expressed
that way, the primary alternative is a bottom-up parser.

Using Boost.spirit, you specify the input in a modified form of EBNF,
and the library produces a recursive descent parser from that input.
Bottom-up parsers are most often produced by yacc, bison, byacc, and
other such parser generator tools.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 9 '07 #6

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

Similar topics

9
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking...
1
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
1
by: Tony Johansson | last post by:
Hello! I'm reading about design pattern adaptor in the GOF book and there is something that sounds strange. When you use the adaptor design pattern you have these participants. *Target -...
10
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
2
by: Tinu | last post by:
Hi, I have some kind of basic-design question. While developing my web application I asked myself if it makes sense to use some kind of command pattern. How do others implement the functional...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
4
by: FluffyCat | last post by:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Command Pattern. Since you all enjoyed the Visitor Pattern so much yesterday, today I have the Command Pattern...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.