Re: Design pattern question
alebcn75@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/Semantic_analysis_%28computer_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.