#!/usr/bin/env python

""" 
   Copyright (C) 2001-2003 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""

import sys

input = sys.stdin
output = sys.stdout
error = sys.stderr
source = input.read()
l = len(source)
current_tag_name = ''
current_position = 0

def main():
	sys.exit(not read())

def read():
	return read_bloc()

def read_bloc():
	global current_tag_name

	if not read_begin_tag():
		return 0
	name = current_tag_name
	
	while read_text() or read_bloc():
		continue
	
	if not read_end_tag(name):
		error.write('syntax error : end tag expected\n')
		sys.exit(1)

	return 1

def read_begin_tag():
	global current_tag_name, current_position, source, l
	
	current_tag_name = ''
	if current_position + 7 < l and source[current_position:current_position+7] == '\\begin{':
		current_position = current_position + 7
		old_position = current_position
		while current_position < l and not source[current_position] == '}':
			current_position = current_position + 1

		if current_position == l or not source[current_position] == '}':
			error.write('syntax error : } after begin tag expected\n')
			sys.exit(1)
		else:
			current_tag_name = source[old_position:current_position]			
			output.write('<%s>' % current_tag_name)
			current_position = current_position + 1
			return 1

	return 0

def read_end_tag(name):
	global current_position, source, l
	
	if current_position + 5 < l and source[current_position:current_position+5] == '\\end{':
		current_position = current_position + 5
		old_position = current_position
		while current_position < l and not source[current_position] == '}':
			current_position = current_position + 1

		if current_position == l or not source[current_position] == '}':
			error.write('syntax error : } after end tag expected\n')
			sys.exit(1)
		else:
			tag_name = source[old_position:current_position]			
			output.write('</%s>' % tag_name)
			if name == tag_name:
				current_position = current_position + 1
				return 1
			else:
				error.write('syntax error : end tag %s expected while %s found\n' % (name, tag_name))
				sys.exit(1)
	return 0

def read_text():
	global current_position, source, l
	
	old_position = current_position
	
	while current_position < l:
		
		while current_position < l and not source[current_position] == '\\':
			current_position = current_position + 1
			
		if current_position + 7 < l and source[current_position:current_position+7] == '\\begin{' \
		   or current_position + 5 < l and source[current_position:current_position+5] == '\\end{':
			
			if old_position < current_position:
				output.write(source[old_position:current_position])
				return 1
			else:
				return 0
			
		current_position = current_position + 1
	
	return 0

if __name__ == '__main__':
	main()

