// File: /workspace/github_cpp_makefile_star2_O3/SeanLee97[P]LL1-Parser/grammar.cpp
// Function: Grammar::parse()
void Grammar::parse() {
    stack<int> parse_stack;
    int expected_symbol;
    token input_tok;
    parse_stack.push(start_symbol);
    input_tok = scan();

    do {
    	expected_symbol = parse_stack.top();
    	if(input_tok.num == 32) {
    		cout << "\nError: Invalid token \"" << input_tok.image << "\" at row: " << input_tok.line << " column: " << input_tok.column <<endl;
    		exit(0);
    		input_tok = scan();
    	}
    	if(expected_symbol < 0) {
    		if(input_tok.num == abs(expected_symbol)) {    			
    			cout << "Match: " << input_tok.image << "\n";
    			input_tok = scan(); 
    			parse_stack.pop();
    		} else {
    			cout <<"Error: \"" << input_tok.image << "\" at row: " << input_tok.line << " column: " << input_tok.column << " was not matched with expected \"" << terminal_names[abs(expected_symbol)] <<"\""<<endl;
    			exit(0);
    			parse_stack.pop();
    		}
    	} else if (expected_symbol > 0) {

    		if(parse_table[abs(expected_symbol)-1][input_tok.num-1] == 0) {
    			vector<int> this_nonterminals_first = first_sets[expected_symbol];
    			vector<int> this_nonterminals_follow = follow_sets[expected_symbol];
    			cout << "Error: Invalid input for symbol pair: (" << nonterminal_names[expected_symbol] << ", " << input_tok.image << ")"<<endl;
    			exit(0);
    			do {
    				input_tok = scan();
    				if(_in_array(this_nonterminals_first, input_tok.num)) {
    					cout << "\tInput token: \"" << input_tok.image << "\" was found in \"" << nonterminal_names[expected_symbol] << "\"\'s first set."<<endl;
    					break;
    				} else if (_in_array(this_nonterminals_follow, input_tok.num)) {
    					cout << "\tInput token: \"" << input_tok.image << "\" was found in \"" << nonterminal_names[expected_symbol] << "\"'s first set."<<endl;
    					parse_stack.pop();
    					break;
    				}
    			} while(input_tok.num != tok_eof);
    		} else {
    			vector<int> pred_rule = right_hand_sides[parse_table[abs(expected_symbol) - 1][input_tok.num - 1] - 1];


    			cout << "Predicted: " << nonterminal_names[expected_symbol] << " -> ";
				parse_stack.pop();

				int i = 0;
				while(pred_rule[i] != 0) {
					parse_stack.push(pred_rule[i]);
					++i;
				}

				for(i = i-1; i >= 0; i--) {
					if(pred_rule[i] < 0)
						cout << terminal_names[abs(pred_rule[i])] << " ";
					else
						cout << nonterminal_names[abs(pred_rule[i])] << " ";
				}
				i = 0; 
				cout << endl; 

			}
    	}
    } while (parse_stack.top() != -1);
    cout << "succeed!" << endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %r14
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x7100
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rax
je 0x5376
movl 0x5f59(%rip), %ecx      # 0xb2c0
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, -0xc0(%rbp)
jmp 0x5389
leaq 0x5f43(%rip), %rsi      # 0xb2c0
leaq -0xf0(%rbp), %rdi
callq 0x7288
leaq -0x98(%rbp), %rdi
callq 0x2760
leaq -0x50(%rbp), %r15
leaq -0x88(%rbp), %r14
movups -0x10(%r14), %xmm0
movaps %xmm0, -0x10(%r15)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2240
movq (%r14), %rdi
movq 0x20(%r14), %rax
movq %rax, 0x20(%r15)
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x53d5
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x21d0
leaq -0xf0(%rbp), %rcx
movq 0x30(%rcx), %rax
movq 0x38(%rcx), %rdi
movl $0x200, %r14d           # imm = 0x200
movq 0x5bdf(%rip), %r13      # 0xafd0
movq %rax, %rdx
cmpq %rdi, %rax
jne 0x5407
movq -0xa8(%rbp), %rcx
movq -0x8(%rcx), %rdx
addq %r14, %rdx
movslq -0x60(%rbp), %rcx
cmpq $0x20, %rcx
je 0x57d6
movl -0x4(%rdx), %r12d
testl %r12d, %r12d
js 0x54cc
testq %r12, %r12
je 0x5739
movq 0x2b0(%rbx), %rax
leaq (%r12,%r12,2), %r15
movq -0x18(%rax,%r15,8), %rax
movslq -0x4(%rax,%rcx,4), %rax
testq %rax, %rax
je 0x5855
movq 0x2e0(%rbx), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rsi
addq $-0x18, %rsi
leaq -0x98(%rbp), %rdi
callq 0x5ebc
movl $0xb, %edx
movq %r13, %rdi
leaq 0x2e06(%rip), %rsi      # 0x827d
callq 0x2200
movq 0x250(%rbx), %rax
shlq $0x5, %r12
movq (%rax,%r12), %rsi
movq 0x8(%rax,%r12), %rdx
movq %r13, %rdi
callq 0x2200
movl $0x4, %edx
movq %rax, %rdi
leaq 0x2de2(%rip), %rsi      # 0x8289
callq 0x2200
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rdi
cmpq %rdi, %rax
je 0x557b
addq $-0x4, %rax
jmp 0x55b6
negl %r12d
cmpl %r12d, %ecx
jne 0x5908
movl $0x7, %edx
movq %r13, %rdi
leaq 0x2d37(%rip), %rsi      # 0x821e
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rdi
callq 0x2200
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2d1b(%rip), %rsi      # 0x8226
callq 0x2200
leaq -0x98(%rbp), %rdi
callq 0x2760
movups -0x98(%rbp), %xmm0
movaps %xmm0, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x2240
movq -0x88(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x555b
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x21d0
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rdi
cmpq %rdi, %rax
je 0x56f6
addq $-0x4, %rax
jmp 0x5732
movl $0x200, %esi            # imm = 0x200
callq 0x21d0
movq -0xa8(%rbp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, -0xa8(%rbp)
movq -0x8(%rax), %rax
movq %rax, -0xb8(%rbp)
leaq 0x200(%rax), %rcx
movq %rcx, -0xb0(%rbp)
addq $0x1fc, %rax            # imm = 0x1FC
movq %rax, -0xc0(%rbp)
movq -0x98(%rbp), %rax
movl (%rax), %ecx
testl %ecx, %ecx
je 0x56a3
movl $0x4, %r12d
movl $0x1, %edx
xorl %r14d, %r14d
movq %rax, %rsi
movl %edx, %r15d
movq -0xc0(%rbp), %rdx
movq -0xb0(%rbp), %rdi
addq $-0x4, %rdi
cmpq %rdi, %rdx
je 0x5608
movl %ecx, (%rdx)
addq $0x4, %rdx
movq %rdx, -0xc0(%rbp)
jmp 0x561b
leaq -0xf0(%rbp), %rdi
callq 0x7288
movq -0x98(%rbp), %rax
incq %r14
leaq (%rax,%r12), %rsi
movl (%rax,%r12), %ecx
addq $0x4, %r12
leal 0x1(%r15), %edx
testl %ecx, %ecx
jne 0x55df
testl %r14d, %r14d
movl $0x200, %r14d           # imm = 0x200
jle 0x56a3
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
incq %r15
movq -0x98(%rbp), %rax
movl -0x8(%rax,%r15,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leaq 0x250(%rbx), %rdx
leaq 0x238(%rbx), %rsi
testl %eax, %eax
cmovsq %rsi, %rdx
movq (%rdx), %rax
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r13, %rdi
callq 0x2200
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2c40(%rip), %rsi      # 0x82d5
callq 0x2200
decq %r15
cmpq $0x1, %r15
ja 0x5647
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x56e6
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rdi
jmp 0x5739
movl $0x200, %esi            # imm = 0x200
callq 0x21d0
movq -0xa8(%rbp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, -0xa8(%rbp)
movq -0x8(%rax), %rdi
movq %rdi, -0xb8(%rbp)
leaq 0x200(%rdi), %rax
movq %rax, -0xb0(%rbp)
leaq 0x1fc(%rdi), %rax
movq %rax, -0xc0(%rbp)
movq %rax, %rcx
cmpq %rdi, %rax
jne 0x574f
movq -0xa8(%rbp), %rcx
movq -0x8(%rcx), %rcx
addq %r14, %rcx
cmpl $-0x1, -0x4(%rcx)
jne 0x53f1
movq 0x5870(%rip), %rbx      # 0xafd0
leaq 0x2b27(%rip), %rsi      # 0x828e
movl $0x8, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq 0x583e(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x57b8
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21d0
leaq -0xf0(%rbp), %rdi
callq 0x6366
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x57f3(%rip), %rdi      # 0xafd0
leaq 0x2a0d(%rip), %rsi      # 0x81f1
movl $0x17, %edx
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x57d3(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %rbx
leaq 0x29fd(%rip), %rsi      # 0x8209
movl $0xa, %edx
movq %rax, %rdi
callq 0x2200
movl -0x30(%rbp), %esi
movq %rbx, %rdi
callq 0x22d0
movq %rax, %rbx
leaq 0x29e6(%rip), %rsi      # 0x8214
movl $0x9, %edx
movq %rax, %rdi
callq 0x2200
movl -0x2c(%rbp), %esi
movq %rbx, %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2230
shlq $0x3, %r15
movq 0x268(%rbx), %rsi
addq %r15, %rsi
leaq -0x98(%rbp), %rdi
callq 0x5ebc
addq 0x280(%rbx), %r15
leaq -0x108(%rbp), %rdi
movq %r15, %rsi
callq 0x5ebc
movq 0x5744(%rip), %rdi      # 0xafd0
leaq 0x29bf(%rip), %rsi      # 0x8252
movl $0x27, %edx
callq 0x2200
movq 0x250(%rbx), %rax
shlq $0x5, %r12
movq (%rax,%r12), %rsi
movq 0x8(%rax,%r12), %rdx
movq 0x5718(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %rbx
leaq 0x29b3(%rip), %rsi      # 0x827a
movl $0x2, %edx
movq %rax, %rdi
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x2200
movq %rax, %rbx
leaq 0x2a79(%rip), %rsi      # 0x8367
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20e0
jmp 0x584e
movq 0x56c1(%rip), %rdi      # 0xafd0
leaq 0x2912(%rip), %rsi      # 0x8228
movl $0x8, %edx
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x56a1(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %r14
leaq 0x28cb(%rip), %rsi      # 0x8209
movl $0xa, %edx
movq %rax, %rdi
callq 0x2200
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x28b4(%rip), %rsi      # 0x8214
movl $0x9, %edx
movq %rax, %rdi
callq 0x2200
movl -0x2c(%rbp), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x28af(%rip), %rsi      # 0x8231
movl $0x20, %edx
movq %rax, %rdi
callq 0x2200
movl %r12d, %eax
movq 0x238(%rbx), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
movq %r14, %rdi
callq 0x2200
movq %rax, %rbx
leaq 0x2877(%rip), %rsi      # 0x822f
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20e0
jmp 0x584e
jmp 0x5a02
movq %rax, %rbx
movq -0x108(%rbp), %rdi
testq %rdi, %rdi
je 0x5a0c
movq -0xf8(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x5a0c
jmp 0x5a09
jmp 0x5a02
jmp 0x5a02
jmp 0x5a02
jmp 0x5a02
jmp 0x5a09
jmp 0x5a09
movq %rax, %rbx
jmp 0x5a27
jmp 0x5a09
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x5a27
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5a40
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21d0
leaq -0xf0(%rbp), %rdi
callq 0x6366
movq %rbx, %rdi
callq 0x22e0