Your task is to write a program that checks html files, (web pages) to determine whether the embedded XHTML tags are balanced. Balanced tags are necessary for the file to be a valid XHTML file as explained above.
The first phase takes care of reading the file and finding all of the tags from the file. The first phase will print out the tags as it finds them and also save them into a queue of tags. If the input file ends in the middle of a tag, the program will report the error and end.
The second phase of the program takes the queue of tags generated in phase one and analyzes the sequence of tags to make sure that they are properly balanced. This phase will make use of the queue and a stack, as described on the previous slide. As the tags are matched, you should print the matching tags. If the tag is self-closing, you should print it and also that it is self-closing.
This is what i have so far.My phase 2 is not running, can someone please help me
def printGreeting():
print 'Welcome bla bla bla bla\n\n'
def phase1(file, queue):
line_num = 0
start = 0
end = 0
ok_tag = False
for line in file:
line_num += 1
if line.count('>') != line.count('<'):
print 'You have an error in line ', line_num
return False
for i in range (len(line)):
if line[i] == '<':
start = i
ok_tag = True
if line[i] == '>':
if ok_tag:
end = i
tag = line[start:end + 1]
queue.append(tag)
print tag
ok_tag = False
return True
def openTag(tag):
if tag[1] == '/':
return False
return True
def isMatching(tag1, tag2):
if openTag(tag1):
if openTag(tag2) == False:
def selfClosing(tag):
if tag[-2] == '/':
return True
return False
def main():
printGreeting()
filename = raw_input('Please enter the file name: ')
file = open(filename, 'r')
queue = []
#Phase 1
if phase1(file, queue) == False:
print 'File ends in the middle of a tag\n\n'
file.close()
sys.exit()
else:
print 'Phase 1: End of file was reached for', filename, 'with no errors\n'
#Phase 2
print '\PHASE 2'
stack = []
for tag in queue:
if selfClosing(tag):
print tag, 'is self-closing'
else:
if openTag(tag):
push(stack, tag)
else:
tag1 = pop(stack)
if tag1 == None:
print tag, 'has no match'
sys.exit()
if isMatching(tag, tag1): file.close()
sys.exit()
else:
print 'Phase 1: End of file was reached for', filename, 'with no errors\n'
#Phase 2
print '\PHASE 2'
stack = []
for tag in queue:
if selfClosing(tag):
print tag, 'is self-closing'
else:
if openTag(tag):
push(stack, tag)
else:
tag1 = pop(stack)
if tag1 == None:
print tag, 'has no match'
sys.exit()
if isMatching(tag, tag1):
print tag1, 'is matching', tag
else:
print tag1, 'does not match', tag
sys.exit()
print 'Phase 2: The tags match in this document.\n'
main()