// File: SeanLee97[P]LL1-Parser/grammar.cpp
// Function: Grammar::init(char*)
void Grammar::init(char* inputName) {
	fflush(stdout);
	_grammar_counter(inputName); 

	vector<vector<string> > nonterm_and_term_names = _process_grammar(inputName); 
	terminal_names = nonterm_and_term_names[0]; 
	nonterminal_names = nonterm_and_term_names[1];

	// 导空符号的记录，若Nonterminal -> epsilon 说明导空则为true
	vector<bool> is_epsilon(num_nonterminals); 
	full_rules = get_rule(inputName, is_epsilon); 
	first_sets = get_first(is_epsilon);
	follow_sets = get_follow(is_epsilon, first_sets);
	predict_sets = get_predict(is_epsilon, first_sets, follow_sets);

	parse_table.resize(nonterminal_names.size(), vector<int>(maxidx_terminal));
	for(int i = 0; i < nonterminal_names.size(); i++) {
		for(int j = 0; j < maxidx_terminal; j++) {
			parse_table[i][j] = 0;
		}
	}

	for(int i = 0; i < predict_sets.size(); i++) {
		for(int j = 0; j < predict_sets[i].size(); j++) {
			if(parse_table[full_rules[i][0] - 1][predict_sets[i][j] - 1]) {
				cout << "Error: table exist!";
				exit(0);
			}
			parse_table[full_rules[i][0] - 1][predict_sets[i][j] - 1] = i + 1;
		}
	}

	// 在此标记结束符，在每行末尾追加 0 作为结束标记
	right_hand_sides.resize(full_rules.size(), vector<int>(0));
	for(int i = 0; i < full_rules.size(); i++) {
		for(int j = full_rules[i].size() - 1; j > 0; j--) {
			if(full_rules[i][j] != 0) right_hand_sides[i].push_back(full_rules[i][j]); 
		}
		right_hand_sides[i].push_back(0); 
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq 0x1202b(%rip), %rax     # 0x18fb0
movq (%rax), %rdi
callq 0x3290
movq -0x1e8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6bd0
movq -0x1e8(%rbp), %rsi
movq -0x10(%rbp), %rdx
leaq -0x28(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x65e0
movq -0x1f0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8b40
movq -0x1e8(%rbp), %rdi
movq %rax, %rsi
addq $0x238, %rdi            # imm = 0x238
callq 0x8b60
jmp 0x6fe0
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x8b40
movq -0x1e8(%rbp), %rdi
movq %rax, %rsi
addq $0x250, %rdi            # imm = 0x250
callq 0x8b60
jmp 0x7006
movq -0x1e8(%rbp), %rax
movslq 0x4(%rax), %rax
movq %rax, -0x200(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x8e50
movq -0x200(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x8e70
jmp 0x7041
leaq -0x61(%rbp), %rdi
callq 0x8eb0
movq -0x1e8(%rbp), %rsi
movq -0x10(%rbp), %rdx
leaq -0x80(%rbp), %rdi
leaq -0x60(%rbp), %rcx
callq 0x4870
jmp 0x7064
movq -0x1e8(%rbp), %rdi
addq $0x2c8, %rdi            # imm = 0x2C8
leaq -0x80(%rbp), %rsi
movq %rsi, -0x208(%rbp)
callq 0x8ed0
movq -0x208(%rbp), %rdi
callq 0x3710
leaq -0xc0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8f00
jmp 0x70a0
movq -0x1e8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x4f10
jmp 0x70bc
movq -0x1e8(%rbp), %rdi
addq $0x268, %rdi            # imm = 0x268
leaq -0x98(%rbp), %rsi
movq %rsi, -0x210(%rbp)
callq 0x8ed0
movq -0x210(%rbp), %rdi
callq 0x3710
leaq -0xc0(%rbp), %rdi
callq 0x9080
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8f00
jmp 0x7107
movq -0x1e8(%rbp), %rsi
addq $0x268, %rsi            # imm = 0x268
leaq -0x118(%rbp), %rdi
callq 0x90a0
jmp 0x7123
movq -0x1e8(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
leaq -0x100(%rbp), %rdx
leaq -0x118(%rbp), %rcx
callq 0x5620
jmp 0x7146
movq -0x1e8(%rbp), %rdi
addq $0x280, %rdi            # imm = 0x280
leaq -0xd8(%rbp), %rsi
movq %rsi, -0x218(%rbp)
callq 0x8ed0
movq -0x218(%rbp), %rdi
callq 0x3710
leaq -0x118(%rbp), %rdi
callq 0x3710
leaq -0x100(%rbp), %rdi
callq 0x9080
leaq -0x158(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8f00
jmp 0x719d
movq -0x1e8(%rbp), %rsi
addq $0x268, %rsi            # imm = 0x268
leaq -0x170(%rbp), %rdi
callq 0x90a0
jmp 0x71b9
movq -0x1e8(%rbp), %rsi
addq $0x280, %rsi            # imm = 0x280
leaq -0x188(%rbp), %rdi
callq 0x90a0
jmp 0x71d5
movq -0x1e8(%rbp), %rsi
leaq -0x130(%rbp), %rdi
leaq -0x158(%rbp), %rdx
leaq -0x170(%rbp), %rcx
leaq -0x188(%rbp), %r8
callq 0x5f80
jmp 0x71ff
movq -0x1e8(%rbp), %rdi
addq $0x298, %rdi            # imm = 0x298
leaq -0x130(%rbp), %rsi
movq %rsi, -0x240(%rbp)
callq 0x8ed0
movq -0x240(%rbp), %rdi
callq 0x3710
leaq -0x188(%rbp), %rdi
callq 0x3710
leaq -0x170(%rbp), %rdi
callq 0x3710
leaq -0x158(%rbp), %rdi
callq 0x9080
movq -0x1e8(%rbp), %rdi
movq %rdi, %rax
addq $0x2b0, %rax            # imm = 0x2B0
movq %rax, -0x238(%rbp)
addq $0x250, %rdi            # imm = 0x250
callq 0x8e30
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
movq %rcx, -0x230(%rbp)
movslq (%rax), %rax
movq %rax, -0x228(%rbp)
leaq -0x1a1(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x8550
movq -0x228(%rbp), %rsi
movq -0x220(%rbp), %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x8570
jmp 0x72bd
movq -0x230(%rbp), %rsi
movq -0x238(%rbp), %rdi
leaq -0x1a0(%rbp), %rdx
callq 0x9190
jmp 0x72d9
leaq -0x1a0(%rbp), %rdi
callq 0x3900
leaq -0x1a1(%rbp), %rdi
callq 0x3ae0
movl $0x0, -0x1a8(%rbp)
movq -0x1e8(%rbp), %rdi
movslq -0x1a8(%rbp), %rax
movq %rax, -0x248(%rbp)
addq $0x250, %rdi            # imm = 0x250
callq 0x8e30
movq %rax, %rcx
movq -0x248(%rbp), %rax
cmpq %rcx, %rax
jae 0x74bc
movl $0x0, -0x1ac(%rbp)
movq -0x1e8(%rbp), %rcx
movl -0x1ac(%rbp), %eax
cmpl (%rcx), %eax
jge 0x74a6
movq -0x1e8(%rbp), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
movslq -0x1a8(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movslq -0x1ac(%rbp), %rsi
callq 0x86c0
movl $0x0, (%rax)
movl -0x1ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ac(%rbp)
jmp 0x7339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x790e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x8eb0
jmp 0x790e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x7905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x9080
jmp 0x7905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x740d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x3710
leaq -0x100(%rbp), %rdi
callq 0x9080
jmp 0x7905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x745e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x7452
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x3710
leaq -0x170(%rbp), %rdi
callq 0x3710
leaq -0x158(%rbp), %rdi
callq 0x9080
jmp 0x7905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x7495
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x3900
leaq -0x1a1(%rbp), %rdi
callq 0x3ae0
jmp 0x7905
jmp 0x74a8
movl -0x1a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1a8(%rbp)
jmp 0x72fb
movl $0x0, -0x1b0(%rbp)
movq -0x1e8(%rbp), %rdi
movslq -0x1b0(%rbp), %rax
movq %rax, -0x250(%rbp)
addq $0x298, %rdi            # imm = 0x298
callq 0x8790
movq %rax, %rcx
movq -0x250(%rbp), %rax
cmpq %rcx, %rax
jae 0x76c5
movl $0x0, -0x1b4(%rbp)
movq -0x1e8(%rbp), %rdi
movslq -0x1b4(%rbp), %rax
movq %rax, -0x258(%rbp)
addq $0x298, %rdi            # imm = 0x298
movslq -0x1b0(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
callq 0x87c0
movq %rax, %rcx
movq -0x258(%rbp), %rax
cmpq %rcx, %rax
jae 0x76af
movq -0x1e8(%rbp), %rdi
movq %rdi, %rax
addq $0x2b0, %rax            # imm = 0x2B0
movq %rax, -0x268(%rbp)
addq $0x2c8, %rdi            # imm = 0x2C8
movslq -0x1b0(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x86c0
movq -0x268(%rbp), %rdi
movl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x86a0
movq -0x1e8(%rbp), %rdi
movq %rax, -0x260(%rbp)
addq $0x298, %rdi            # imm = 0x298
movslq -0x1b0(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movslq -0x1b4(%rbp), %rsi
callq 0x86c0
movq -0x260(%rbp), %rdi
movl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x86c0
cmpl $0x0, (%rax)
je 0x75f6
movq 0x119ef(%rip), %rdi     # 0x18fd0
leaq 0x9c71(%rip), %rsi      # 0x11259
callq 0x3200
jmp 0x75ef
xorl %edi, %edi
callq 0x32b0
movq -0x1e8(%rbp), %rdi
movl -0x1b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x26c(%rbp)
movq %rdi, %rax
addq $0x2b0, %rax            # imm = 0x2B0
movq %rax, -0x280(%rbp)
addq $0x2c8, %rdi            # imm = 0x2C8
movslq -0x1b0(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x86c0
movq -0x280(%rbp), %rdi
movl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x86a0
movq -0x1e8(%rbp), %rdi
movq %rax, -0x278(%rbp)
addq $0x298, %rdi            # imm = 0x298
movslq -0x1b0(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movslq -0x1b4(%rbp), %rsi
callq 0x86c0
movq -0x278(%rbp), %rdi
movl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x86c0
movl -0x26c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x7504
jmp 0x76b1
movl -0x1b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b0(%rbp)
jmp 0x74c6
movq -0x1e8(%rbp), %rdi
movq %rdi, %rax
addq $0x2e0, %rax            # imm = 0x2E0
movq %rax, -0x298(%rbp)
addq $0x2c8, %rdi            # imm = 0x2C8
callq 0x8790
movq %rax, -0x290(%rbp)
leaq -0x1d1(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x8550
movq -0x288(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x1d0(%rbp), %rdi
callq 0x8570
jmp 0x771b
movq -0x290(%rbp), %rsi
movq -0x298(%rbp), %rdi
leaq -0x1d0(%rbp), %rdx
callq 0x9190
jmp 0x7737
leaq -0x1d0(%rbp), %rdi
callq 0x3900
leaq -0x1d1(%rbp), %rdi
callq 0x3ae0
movl $0x0, -0x1d8(%rbp)
movq -0x1e8(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
movq %rax, -0x2a0(%rbp)
addq $0x2c8, %rdi            # imm = 0x2C8
callq 0x8790
movq %rax, %rcx
movq -0x2a0(%rbp), %rax
cmpq %rcx, %rax
jae 0x78ea
movq -0x1e8(%rbp), %rdi
addq $0x2c8, %rdi            # imm = 0x2C8
movslq -0x1d8(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
callq 0x87c0
subq $0x1, %rax
movl %eax, -0x1dc(%rbp)
cmpl $0x0, -0x1dc(%rbp)
jle 0x789f
movq -0x1e8(%rbp), %rdi
addq $0x2c8, %rdi            # imm = 0x2C8
movslq -0x1d8(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movslq -0x1dc(%rbp), %rsi
callq 0x86c0
cmpl $0x0, (%rax)
je 0x7889
movq -0x1e8(%rbp), %rdi
addq $0x2e0, %rdi            # imm = 0x2E0
movslq -0x1d8(%rbp), %rsi
callq 0x86a0
movq -0x1e8(%rbp), %rdi
movq %rax, -0x2a8(%rbp)
addq $0x2c8, %rdi            # imm = 0x2C8
movslq -0x1d8(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movslq -0x1dc(%rbp), %rsi
callq 0x86c0
movq -0x2a8(%rbp), %rdi
movq %rax, %rsi
callq 0x8900
jmp 0x7853
jmp 0x7889
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x787b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x3900
leaq -0x1d1(%rbp), %rdi
callq 0x3ae0
jmp 0x7905
jmp 0x788b
movl -0x1dc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1dc(%rbp)
jmp 0x77b9
movq -0x1e8(%rbp), %rdi
addq $0x2e0, %rdi            # imm = 0x2E0
movslq -0x1d8(%rbp), %rsi
callq 0x86a0
movq %rax, %rdi
movl $0x0, -0x1e0(%rbp)
leaq -0x1e0(%rbp), %rsi
callq 0x88d0
jmp 0x78d4
jmp 0x78d6
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x7759
leaq -0x60(%rbp), %rdi
callq 0x9080
leaq -0x28(%rbp), %rdi
callq 0x8dd0
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x9080
leaq -0x28(%rbp), %rdi
callq 0x8dd0
movq -0x30(%rbp), %rdi
callq 0x3390