From: Franz S. <Fra...@ne...> - 2006-03-24 07:30:35
|
Hi everyone, I'm new to Spirit. I use Spirit to parse a couple of rather simple files and achievded fast progress with the great tools of Spirit. I now run in to a problem parsing an xml like file (just tags with attributes but not text and so on): <TestSuite> <Dummy a="theA" b="theB" > </Dummy> <Test name="StartUp"> <Include src="StartUp.olg"/> </Test> </TestSuite> I use the following grammar: name = sp::lexeme_d[(sp::alpha_p | '_' | '$'|'#') >> *(sp::alnum_p | '_' | '$'|'#')]; attributeValue = *(sp::anychar_p - (sp::ch_p('<') | '&' | '"')) ; attribute = ( name [sp::assign_a(action.mAttribute.mName)] >> '=' >> '"' >> attributeValue [sp::assign_a(action.mAttribute.mValue)] >> '"' ) [ boost::bind(&Action::pushAttribute,&action) ] ; tagData = name [sp::assign_a(action.mTagName)] >> *attribute; tagStart = ( '<' >> tagData >> '>' ) [ boost::bind(&Action::popStartTag,&action) ] ; tagEnd = ( sp::str_p("</") >> name [sp::assign_a(action.mTagName)] >> '>' ) [ boost::bind(&Action::popEndTag,&action) ] ; tagEmpty = ( '<' >> tagData >> "/>" ) [ boost::bind(&Action::popStartTag,&action) ] [ boost::bind(&Action::popEndTag,&action) ] ; element = tagEmpty | ( tagStart >> content >> tagEnd ) ; content = *element ; expression = content ; The problem is, that the pushAttribute actions are called twice for non empty tags (tagStart). I belive it's because it fisrt tries to parse an empty tag (tagEmpty) wich fails at the '>' but the attributes are already mached and therefore the pushAttribute action called the first time. Then it tries to parses the non empty tags (tagStar) maches the attributes again and therefore calls this actions again and I end up with having every attribute twice. I could solve my problem by adding a clearAttribute action to the '<' of 'tagStart' and 'tagEmpty', but I somhow don't like to do this. Is there an other way to write the or expression ' ( tagEmpty | ( tagStart >> content >> tagEnd ) ' so no "internal action" of 'tagEmpty' are called if it's not mached entirely? Or ore my expressions wrong in the first place? I'm very thankful for any help and comments. - Franz |