Writing a parser the right way?
kylotan at gmail.com
Wed Sep 21 14:46:37 CEST 2005
> I'm writing a parser for english language. This is a simple function to
> identify, what kind of sentence we have. Do you think, this class
> wrapping is right to represent the result of the function? Further
> parsing then checks isinstance(text, Declarative).
> class Sentence(str): pass
> class Declarative(Sentence): pass
> class Question(Sentence): pass
> class Command(Sentence): pass
As far as the parser is concerned, making these separate classes is
unnecessary when you could just store the sentence type as a normal
data member of Sentence. So the answer to your question is no, in my
However, when you come to actually use the resulting Sentence objects,
perhaps the behaviour is different? If you're looking to use a standard
interface to Sentences but are going to be doing substantially
different processing depending on which sentence type you have, then
yes, this class hierarchy may be useful to you.
More information about the Python-list