Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I'm looking for help for a C++ program that utilizes stacks/hash tables that has thefollowing algorithm: The algorithm to determine if thestart and end-tags balance
I'm looking for help for a C++ program that utilizes stacks/hash tables that has thefollowing algorithm:
The algorithm to determine if thestart and end-tags balance uses a Stack data structure to keeptrack of previously read start-tags. The algorithm is:
1. Readthe input until the beginning of a tag is detected. (i.e. tagsbegin with <: if the next character is a / (slash), then it isan end-tag; otherwise it is a start-tag).
2. Readthe tag's identity. (e.g. both tags and havethe same identity: 'x').
3. Ifthe tag was a start-tag, push it onto the Stack.
4.Otherwise, it is an end-tag. In this case, pop the Stack (whichcontains a previously pushed start-tag identity) and verify thatthe popped identity is the same as the the end-tag just processed.Note: if the stack cannot be popped (because it isempty), the input is invalid; the algorithm should STOP.
5. Ifthe identities do not match, the XML expression isinvalid. STOP.
6. Ifthey do match, then no error has been detected (yet!).
7. Ifthere is still input that has not yet been processed, go back tothe first step.
8. Otherwise (no moreinput) then the input is valid unless the Stack isnot empty. Indicate whether theinput is valid (Stack empty) or invalid and STOP.
Example of XML tags:
XML
Valid?
Explanation
Yes
"a" tags balance
Yes
"a" outer tags and "b" inner tags balance
No
"a" end-tags does not match start-tag ("b")
Yes
all tags balance
No
"Baker" end-tag does not matchstart-tag ("baker") (i.e. the tag names arecase-sensitive.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started