before we look at the states and the parserlet' consider the output of this program we know we want tree of node objectsbut what does node look likewellclearly it'll need to know the name of the tag it is parsingand since it' treeit should probably maintain pointer to the parent node and list of the node' children in order some nodes have text valuebut not all of them let' look at this node class firstclass nodedef __init__(selftag_nameparent=none)self parent parent self tag_name tag_name self children [self text="def __str__(self)if self textreturn self tag_name "self text elsereturn self tag_name this class sets default attribute values upon initialization the __str__ method is supplied to help visualize the tree structure when we're finished nowlooking at the example documentwe need to consider what states our parser can be in clearly it' going to start in state where no nodes have yet been processed we'll need state for processing opening tags and closing tags and when we're inside tag with text contentswe'll have to process that as separate statetoo switching states can be trickyhow do we know if the next node is an opening taga closing tagor text nodewe could put little logic in each state to work this outbut it actually makes more sense to create new state whose sole purpose is figuring out which state we'll be switching to next if we call this transition state childnodewe end up with the following statesfirsttag childnode opentag closetag text