// File: /workspace/github_cpp_makefile_star2_O2/ben-strasser[P]latex-checker/latex-checker.cpp
// Function: main
int main(){

	int line_num = 1;
	try{
		stack<Tag>s;

		
		auto skip_spaces = [&]{
			while(isspace(cin.peek())){
				if(cin.peek() == '\n'){
					++line_num;
				}
				cin.ignore();
			}
		};

		auto read_word = []{
			string word;
			while(isalnum(cin.peek()) || cin.peek() == '*')
				word += cin.get();
			return word;
		};

		auto check_close_tag = [&](string close_tag){
			if(s.empty()){
				if(close_tag == "}")
					throw string("} has no corresponding {");
				else if(close_tag == "]")
					throw string("\\] has no corresponding \\[");
				else 
					throw string("\"\\end{"+close_tag+"}\" has no corresponding \"\\begin{"+close_tag+"}\"");
			} else {
				auto x = s.top();
				s.pop();

				string wanted_close_tag = x.tag;
				if(wanted_close_tag == "{")
					wanted_close_tag = "}";
				if(wanted_close_tag == "[")
					wanted_close_tag = "]";


				if(close_tag != wanted_close_tag){
					ostringstream out;
					out << "on line "<<x.line_num << " a ";
					if(x.tag == "{")
						out << '{';
					else if(x.tag == "[")
						out << "\\[";
					else if(x.tag == "$")
						out << "$";
					else if(x.tag == "$$")
						out << "$$";
					else
						out << "\"\\begin{"<<x.tag << "}\"";
					out << " was opened but has not yet been closed. The ";
					if(close_tag == "}")
						out << '}';
					else if(close_tag == "]")
						out << "\\]";
					else if(close_tag == "$")
						out << "$";
					else if(close_tag == "$$")
						out << "$$";
					else
						out << "\"\\end{"<<close_tag << "}\"";
					out << " does not fit.";

					int unclosed = 1;
					while(!s.empty()){
						x = s.top();
						s.pop();

						wanted_close_tag = x.tag;
						if(wanted_close_tag == "{")
							wanted_close_tag = "}";
						if(wanted_close_tag == "[")
							wanted_close_tag = "]";
					
						if(close_tag == wanted_close_tag){
							out << " A matching ";
							if(x.tag == "{")
								out << '{';
							else if(x.tag == "[")
								out << "\\[";
							else if(x.tag == "$")
								out << "$";
							else if(x.tag == "$$")
								out << "$$";
							else
								out << "\"\\begin{"<<x.tag << "}\"";
							out << " was opened on line "<<x.line_num <<" but ";
							if(unclosed == 1)
								out << "one parenthesis";
							else
								out << unclosed << " parentheses";
							out << " must be closed first.";
							throw string(out.str());
						}else
							++unclosed;
					}
					out << " No matching tag was open.";
					throw string(out.str());
				}
			}
		};

		char c;
		while(c = cin.get(), cin){
			if(c == '%'){
				while(c = cin.get(), cin)
					if(c == '\n')
						break;
				++line_num;
			} else if(c == '\n'){
				++line_num;
			} else if(c == '{'){
				s.push(Tag{line_num, "{"});
			} else if(c == '}'){
				check_close_tag("}");
			} else if(c == '$'){
				string tag = "$";
				if(cin.peek() == '$'){
					cin.ignore();
					tag = "$$";
				}
				if(!s.empty() && s.top().tag == tag)
					check_close_tag(tag);
				else
					s.push(Tag{line_num, tag});
			} else if(c == '\\'){
				if(cin.peek() == '['){
					cin.ignore();
					s.push(Tag{line_num, "["});
				}else if(cin.peek() == ']'){
					cin.ignore();
					check_close_tag("]");
				}else{
					string cmd = read_word();
					if(cmd == "begin"){
						skip_spaces();
						if(cin.get() != '{')
							throw ("\"\\begin\" must be followed by an {");
						string tag = read_word();
						if(cin.get() != '}')
							throw ("\"\\begin{"+tag+"\" must be followed by an }");
						s.push(Tag{line_num, tag});
					}else if(cmd == "end"){
						skip_spaces();
						if(cin.get() != '{')
							throw ("\"\\begin\" must be followed by an {");
						string tag = read_word();
						if(cin.get() != '}')
							throw ("\"\\begin{"+tag+"\" must be followed by an }");
						check_close_tag(tag);
					}else if(cmd == ""){
						// next character is escaped. For example \{. This is not parenthesis that we should check for.
						c = cin.get();
						if(c == '\n')
							++line_num;
					}else if(is_begin_variation(cmd)){
						cout << "Warning in line "<<line_num<<": You wrote \""<< cmd << "{\". Did you mean \"begin{\"?" << endl;
					}else if(is_end_variation(cmd)){
						cout << "Warning in line "<<line_num<<": You wrote \""<< cmd << "{\". Did you mean \"end{\"?" << endl;
					}
				}
			}
		}

		if(!s.empty()){
			ostringstream out;
			out << "not all parentheses were closed, still open are :";
			while(!s.empty()){
				out << '\n';
				auto x = s.top();
				s.pop();
				if(x.tag == "[" || x.tag == "{")
					out << x.tag;
				else
					out << "\"\\begin{"<<x.tag<<"}\"";
				out << " opened on line "<<x.line_num;
			}
			throw string(out.str());
		}
	}catch(string&msg){
		cout << "Error in line "<<line_num<<": " << msg << endl;
		return 1;
	}catch(exception&err){
		cerr << "Exception: "<<err.what() << endl;
		return 2;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movl $0x1, 0x4(%rsp)
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x3664
leaq 0x4(%rsp), %rax
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x8(%rsp), %rax
movq %rbx, (%rax)
movq 0x4926(%rip), %rbp      # 0x6fd8
leaq 0x88(%rsp), %r15
leaq 0xf0(%rsp), %r12
leaq 0x288(%rsp), %r14
movq %rbp, %rdi
callq 0x21c0
movq (%rbp), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rbp,%rcx)
jne 0x2aee
cmpb $0xa, %al
je 0x272a
movzbl %al, %eax
cmpl $0x24, %eax
je 0x27e3
cmpl $0x7d, %eax
je 0x2730
cmpl $0x5c, %eax
je 0x278b
cmpl $0x7b, %eax
je 0x275c
cmpl $0x25, %eax
jne 0x26ca
movq %rbp, %rdi
callq 0x21c0
cmpb $0xa, %al
je 0x272a
movq (%rbp), %rax
movq -0x18(%rax), %rax
movl 0x20(%rbp,%rax), %eax
andl $0x5, %eax
je 0x270d
incl 0x4(%rsp)
jmp 0x26ca
movq %r14, %rdi
leaq 0x193b(%rip), %rsi      # 0x4075
movq %r12, %rdx
callq 0x20a0
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2f88
movq %r14, %rdi
callq 0x22d8
jmp 0x26ca
movl 0x4(%rsp), %eax
movl %eax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x19bf(%rip), %rsi      # 0x4135
movq %r15, %rdx
callq 0x20a0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c32
jmp 0x27d1
movq %rbp, %rdi
callq 0x2090
cmpl $0x5b, %eax
jne 0x28ba
movq %rbp, %rdi
callq 0x2290
movl 0x4(%rsp), %eax
movl %eax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x1992(%rip), %rsi      # 0x4150
movq %r15, %rdx
callq 0x20a0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c32
leaq 0xf8(%rsp), %rdi
callq 0x22d8
jmp 0x26ca
movq %r15, %rdi
leaq 0x1845(%rip), %rsi      # 0x4032
movq %r12, %rdx
callq 0x20a0
movq %rbp, %rdi
callq 0x2090
cmpl $0x24, %eax
jne 0x2819
movq %rbp, %rdi
callq 0x2290
movq %r15, %rdi
leaq 0x181d(%rip), %rsi      # 0x4031
callq 0x22b0
movq 0x68(%rsp), %rdi
cmpq 0x48(%rsp), %rdi
je 0x287a
cmpq 0x70(%rsp), %rdi
jne 0x2840
movq 0x80(%rsp), %rax
movq -0x8(%rax), %rdi
movl $0x1e0, %eax            # imm = 0x1E0
addq %rax, %rdi
addq $-0x20, %rdi
movq %r15, %rsi
callq 0x367f
testb %al, %al
je 0x287a
leaq 0xd0(%rsp), %rdi
movq %r15, %rsi
callq 0x21f0
leaq 0x8(%rsp), %rdi
leaq 0xd0(%rsp), %r13
movq %r13, %rsi
callq 0x2f88
movq %r13, %rdi
jmp 0x28a8
movl 0x4(%rsp), %eax
movl %eax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
movq %r15, %rsi
callq 0x21f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c32
leaq 0xf8(%rsp), %rdi
callq 0x22d8
movq %r15, %rdi
callq 0x22d8
jmp 0x26ca
movq %rbp, %rdi
callq 0x2090
cmpl $0x5d, %eax
jne 0x2903
movq %rbp, %rdi
callq 0x2290
leaq 0x268(%rsp), %r13
movq %r13, %rdi
leaq 0x18cc(%rip), %rsi      # 0x41ad
movq %r12, %rdx
callq 0x20a0
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2f88
movq %r13, %rdi
callq 0x22d8
jmp 0x26ca
movq %r15, %rdi
callq 0x35ac
movq %r15, %rdi
leaq 0x171f(%rip), %rsi      # 0x4034
callq 0x36a2
testb %al, %al
je 0x2993
leaq 0x10(%rsp), %rdi
callq 0x361e
movq %rbp, %rdi
callq 0x21c0
cmpl $0x7b, %eax
jne 0x2c32
leaq 0x18(%rsp), %rdi
callq 0x35ac
movq %rbp, %rdi
callq 0x21c0
cmpl $0x7d, %eax
jne 0x2c55
movl 0x4(%rsp), %eax
movl %eax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x21f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3c32
leaq 0xf8(%rsp), %rdi
callq 0x22d8
leaq 0x18(%rsp), %rdi
jmp 0x28a8
movq %r15, %rdi
leaq 0x16da(%rip), %rsi      # 0x4077
callq 0x36a2
testb %al, %al
je 0x2a0f
leaq 0x10(%rsp), %rdi
callq 0x361e
movq %rbp, %rdi
callq 0x21c0
cmpl $0x7b, %eax
jne 0x2c32
movq %r12, %rdi
callq 0x35ac
movq %rbp, %rdi
callq 0x21c0
cmpl $0x7d, %eax
jne 0x2cb0
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x21f0
leaq 0x8(%rsp), %rdi
leaq 0xb0(%rsp), %r13
movq %r13, %rsi
callq 0x2f88
movq %r13, %rdi
callq 0x22d8
movq %r12, %rdi
jmp 0x28a8
movq %r15, %rdi
leaq 0x16f5(%rip), %rsi      # 0x410e
callq 0x36a2
testb %al, %al
je 0x2a3b
movq %rbp, %rdi
callq 0x21c0
cmpb $0xa, %al
jne 0x28ad
incl 0x4(%rsp)
jmp 0x28ad
movq %r15, %rdi
callq 0x262c
testb %al, %al
je 0x2a89
movq 0x457a(%rip), %rdi      # 0x6fc8
leaq 0x1626(%rip), %rsi      # 0x407b
callq 0x2180
movl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x161c(%rip), %rsi      # 0x408c
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
leaq 0x1613(%rip), %rsi      # 0x409a
jmp 0x2ad9
movq %r15, %rdi
callq 0x264e
testb %al, %al
je 0x28ad
movq 0x4528(%rip), %rdi      # 0x6fc8
leaq 0x15d4(%rip), %rsi      # 0x407b
callq 0x2180
movl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x2270
movq %rax, %rdi
leaq 0x15ca(%rip), %rsi      # 0x408c
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
leaq 0x15dc(%rip), %rsi      # 0x40b5
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20e0
jmp 0x28ad
movq 0x68(%rsp), %rax
cmpq 0x48(%rsp), %rax
jne 0x2b18
leaq 0x38(%rsp), %rdi
callq 0x37a4
xorl %eax, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf0(%rsp), %rdi
callq 0x21e0
leaq 0x15a2(%rip), %rsi      # 0x40ce
leaq 0xf0(%rsp), %rdi
callq 0x2180
leaq 0x90(%rsp), %r15
leaq 0xf0(%rsp), %rbx
leaq 0x88(%rsp), %r14
leaq 0x38(%rsp), %r12
leaq 0x15f3(%rip), %r13      # 0x4150
leaq 0x166b(%rip), %rbp      # 0x41cf
movq 0x68(%rsp), %rax
cmpq 0x48(%rsp), %rax
je 0x2d05
movq %rbx, %rdi
pushq $0xa
popq %rsi
callq 0x21b0
movq 0x68(%rsp), %rsi
cmpq 0x70(%rsp), %rsi
jne 0x2b9f
movq 0x80(%rsp), %rax
movq -0x8(%rax), %rsi
movl $0x1e0, %eax            # imm = 0x1E0
addq %rax, %rsi
addq $-0x28, %rsi
movq %r14, %rdi
callq 0x3744
movq %r12, %rdi
callq 0x3bbc
movq %r15, %rdi
movq %r13, %rsi
callq 0x36a2
testb %al, %al
jne 0x2bd5
movq %r15, %rdi
leaq 0x1569(%rip), %rsi      # 0x4135
callq 0x36a2
testb %al, %al
je 0x2be2
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2160
jmp 0x2c0b
movq %rbx, %rdi
leaq 0x1585(%rip), %rsi      # 0x4171
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movq %rax, %rdi
leaq 0x14fa(%rip), %rsi      # 0x4100
callq 0x2180
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2180
movl 0x88(%rsp), %esi
movq %rax, %rdi
callq 0x2270
movq %r15, %rdi
callq 0x22d8
jmp 0x2b64
pushq $0x8
popq %rdi
callq 0x20f0
leaq 0x13f9(%rip), %rcx      # 0x403a
movq %rcx, (%rax)
movq 0x436d(%rip), %rsi      # 0x6fb8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2260
pushq $0x20
popq %rdi
callq 0x20f0
movq %rax, %r15
leaq 0x150a(%rip), %rsi      # 0x4171
leaq 0xf0(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x36ce
movb $0x1, %bpl
leaq 0x13d9(%rip), %rdx      # 0x405c
leaq 0xf0(%rsp), %rsi
movq %r15, %rdi
callq 0x36af
xorl %ebp, %ebp
leaq 0x40ec(%rip), %rsi      # 0x6d88
movq 0x430d(%rip), %rdx      # 0x6fb0
movq %r15, %rdi
callq 0x2260
jmp 0x2d36
pushq $0x20
popq %rdi
callq 0x20f0
movq %rax, %r15
leaq 0x14af(%rip), %rsi      # 0x4171
leaq 0x18(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x36ce
movb $0x1, %bpl
leaq 0x137e(%rip), %rdx      # 0x405c
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x36af
xorl %ebp, %ebp
leaq 0x4094(%rip), %rsi      # 0x6d88
movq 0x42b5(%rip), %rdx      # 0x6fb0
movq %r15, %rdi
callq 0x2260
jmp 0x2d36
pushq $0x20
popq %rdi
callq 0x20f0
movq %rax, %r15
leaq 0xf8(%rsp), %rsi
movq %rax, %rdi
callq 0x2230
leaq 0x4061(%rip), %rsi      # 0x6d88
movq 0x4282(%rip), %rdx      # 0x6fb0
movq %r15, %rdi
callq 0x2260
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x2120
jmp 0x2d8d
jmp 0x2d87
jmp 0x2d87
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x22d8
jmp 0x2d8d
movq %rdx, %rbx
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x22d8
testb %bpl, %bpl
jne 0x2d7a
jmp 0x2e17
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x2120
jmp 0x2e17
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x2050
jmp 0x2eb2
movq %rdx, %rbx
movq %rax, %r14
leaq 0xb0(%rsp), %rdi
callq 0x22d8
jmp 0x2e17
jmp 0x2e95
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x22d8
testb %bpl, %bpl
jne 0x2dd9
jmp 0x2e05
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x2120
jmp 0x2e05
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x22d8
jmp 0x2e05
jmp 0x2dff
movq %rdx, %rbx
movq %rax, %r14
leaq 0x18(%rsp), %rdi
jmp 0x2e66
jmp 0x2e95
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
jmp 0x2e66
jmp 0x2e95
movq %rdx, %rbx
movq %rax, %r14
jmp 0x2ebc
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0x268(%rsp), %rdi
jmp 0x2ea3
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
jmp 0x2e66
jmp 0x2e83
jmp 0x2eac
jmp 0x2e95
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x22d8
jmp 0x2e9b
jmp 0x2e95
movq %rdx, %rbx
movq %rax, %r14
leaq 0x288(%rsp), %rdi
jmp 0x2ea3
jmp 0x2eac
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
jmp 0x2ea3
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0x88(%rsp), %rdi
callq 0x22d8
jmp 0x2eb2
jmp 0x2eac
movq %rdx, %rbx
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x37a4
cmpl $0x2, %ebx
jne 0x2f16
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
movq 0x40f5(%rip), %rdi      # 0x6fc8
leaq 0x1235(%rip), %rsi      # 0x410f
callq 0x2180
movl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x2270
leaq 0x121a(%rip), %rsi      # 0x410c
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
callq 0x2250
pushq $0x1
jmp 0x2f5f
cmpl $0x1, %ebx
jne 0x2f77
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
movq 0x40c3(%rip), %rdi      # 0x6ff0
leaq 0x11cf(%rip), %rsi      # 0x4103
callq 0x2180
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x2180
movq %rax, %rdi
callq 0x20e0
callq 0x2250
pushq $0x2
popq %rax
jmp 0x2b06
movq %rax, %r14
callq 0x2250
jmp 0x2f77
movq %rax, %r14
callq 0x2250
movq %r14, %rdi
callq 0x2280
movq %rax, %rdi
callq 0x3755
nop