// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
leaq -0xf0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f8a
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x6dfb(%rip), %rsi      # 0xb2b0
movq %rbx, %rdi
callq 0x62a0
leaq -0xa0(%rbp), %rdi
callq 0x26f0
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %r13
movq %r13, %rsi
callq 0x4faa
leaq -0x90(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rcx
movq 0x30(%rcx), %rax
movq 0x38(%rcx), %rcx
leaq 0x250(%r14), %r15
leaq 0x238(%r14), %rbx
movq %r14, -0x30(%rbp)
cmpq %rcx, %rax
jne 0x4521
movq -0xa8(%rbp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movslq -0x68(%rbp), %rcx
cmpq $0x20, %rcx
je 0x4730
movl -0x4(%rax), %r12d
testl %r12d, %r12d
js 0x4655
testq %r12, %r12
je 0x46bb
movq %r13, %rdx
movq 0x2b0(%r14), %rax
imulq $0x18, %r12, %r13
movq -0x18(%rax,%r13), %rax
movslq -0x4(%rax,%rcx,4), %rax
testq %rax, %rax
je 0x4792
movq 0x2e0(%r14), %rcx
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rsi
addq $-0x18, %rsi
movq %rdx, %r13
movq %rdx, %rdi
callq 0x4c16
movq 0x6a45(%rip), %rdi      # 0xafd0
leaq 0x2ceb(%rip), %rsi      # 0x727d
callq 0x21a0
shlq $0x5, %r12
addq 0x250(%r14), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq %rax, %rdi
leaq 0x2cd2(%rip), %rsi      # 0x7289
callq 0x21a0
leaq -0xf0(%rbp), %rdi
callq 0x64fa
xorl %r12d, %r12d
xorl %r14d, %r14d
movq -0xa0(%rbp), %rsi
cmpl $0x0, (%rsi,%r12)
je 0x45f4
addq %r12, %rsi
leaq -0xf0(%rbp), %rdi
callq 0x62a0
incq %r14
addq $0x4, %r12
jmp 0x45ce
movq 0x69d5(%rip), %r12      # 0xafd0
testq %r14, %r14
jle 0x463f
movq -0xa0(%rbp), %rax
movl -0x4(%rax,%r14,4), %eax
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
testl %eax, %eax
movq %r15, %rax
cmovsq %rbx, %rax
shlq $0x5, %rsi
addq (%rax), %rsi
movq %r12, %rdi
callq 0x2190
decq %r14
movq %rax, %rdi
leaq 0x2c9d(%rip), %rsi      # 0x72d5
callq 0x21a0
jmp 0x45fb
movq %r12, %rdi
callq 0x20e0
movq %r13, %rdi
callq 0x2632
movq -0x30(%rbp), %r14
jmp 0x46bb
negl %r12d
cmpl %r12d, %ecx
jne 0x4826
movq 0x6968(%rip), %rdi      # 0xafd0
leaq 0x2baf(%rip), %rsi      # 0x721e
callq 0x21a0
movq %rax, %rdi
leaq -0x58(%rbp), %rsi
callq 0x2190
movq %rax, %rdi
leaq 0x2b9c(%rip), %rsi      # 0x7226
callq 0x21a0
movq %r13, %rdi
callq 0x26f0
leaq -0x68(%rbp), %rdi
movq %r13, %rsi
callq 0x4faa
leaq -0x90(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rdi
callq 0x64fa
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rax, %rdx
cmpq %rcx, %rax
jne 0x46e4
movq -0xa8(%rbp), %rdx
movq -0x8(%rdx), %rdx
movl $0x200, %esi            # imm = 0x200
addq %rsi, %rdx
cmpl $-0x1, -0x4(%rdx)
jne 0x4509
movq 0x68db(%rip), %rdi      # 0xafd0
leaq 0x2b92(%rip), %rsi      # 0x728e
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
leaq -0x58(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rdi
callq 0x4fea
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6899(%rip), %rdi      # 0xafd0
leaq 0x2ab3(%rip), %rsi      # 0x71f1
callq 0x21a0
movq %rax, %rdi
leaq -0x58(%rbp), %rsi
callq 0x2190
leaq 0x2ab3(%rip), %rsi      # 0x7209
movq %rax, %rdi
callq 0x21a0
movl -0x38(%rbp), %esi
movq %rax, %rdi
callq 0x22b0
leaq 0x2aa4(%rip), %rsi      # 0x7214
movq %rax, %rdi
callq 0x21a0
movl -0x34(%rbp), %esi
movq %rax, %rdi
callq 0x22b0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2200
movq 0x268(%r14), %rsi
addq %r13, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4c16
movq -0x30(%rbp), %rax
addq 0x280(%rax), %r13
leaq -0x108(%rbp), %rdi
movq %r13, %rsi
callq 0x4c16
movq 0x6807(%rip), %rdi      # 0xafd0
leaq 0x2a82(%rip), %rsi      # 0x7252
callq 0x21a0
shlq $0x5, %r12
movq -0x30(%rbp), %rcx
addq 0x250(%rcx), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
leaq 0x2a84(%rip), %rsi      # 0x727a
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
leaq -0x58(%rbp), %rsi
callq 0x2190
leaq 0x2b56(%rip), %rsi      # 0x7367
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
jmp 0x478b
movq 0x67a3(%rip), %rdi      # 0xafd0
leaq 0x29f4(%rip), %rsi      # 0x7228
callq 0x21a0
movq %rax, %rdi
leaq -0x58(%rbp), %rsi
callq 0x2190
leaq 0x29bd(%rip), %rsi      # 0x7209
movq %rax, %rdi
callq 0x21a0
movl -0x38(%rbp), %esi
movq %rax, %rdi
callq 0x22b0
leaq 0x29ae(%rip), %rsi      # 0x7214
movq %rax, %rdi
callq 0x21a0
movl -0x34(%rbp), %esi
movq %rax, %rdi
callq 0x22b0
leaq 0x29b1(%rip), %rsi      # 0x7231
movq %rax, %rdi
callq 0x21a0
movl %r12d, %esi
shlq $0x5, %rsi
addq 0x238(%r14), %rsi
movq %rax, %rdi
callq 0x2190
leaq 0x298a(%rip), %rsi      # 0x722f
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
jmp 0x478b
jmp 0x48db
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x2632
jmp 0x48e5
jmp 0x48e2
jmp 0x48db
jmp 0x48db
jmp 0x48e2
jmp 0x48db
jmp 0x48db
jmp 0x48e2
movq %rax, %rbx
jmp 0x48f1
jmp 0x48e2
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2632
leaq -0x58(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rdi
callq 0x4fea
movq %rbx, %rdi
callq 0x22c0