// File: /workspace/github_cpp_makefile_star2_O1/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 0x71cc
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 0x547e
movl 0x5e51(%rip), %ecx      # 0xb2c0
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, -0xc0(%rbp)
jmp 0x5491
leaq 0x5e3b(%rip), %rsi      # 0xb2c0
leaq -0xf0(%rbp), %rdi
callq 0x7354
leaq -0x98(%rbp), %rdi
callq 0x2718
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 0x54dd
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x21d0
movl $0x200, %r14d           # imm = 0x200
movq 0x5ae6(%rip), %r12      # 0xafd0
leaq 0x2de4(%rip), %r15      # 0x82d5
movq -0xc0(%rbp), %rcx
cmpq -0xb8(%rbp), %rcx
jne 0x550f
movq -0xa8(%rbp), %rax
movq -0x8(%rax), %rcx
addq %r14, %rcx
movslq -0x60(%rbp), %rax
cmpq $0x20, %rax
je 0x58cc
movl -0x4(%rcx), %r13d
testl %r13d, %r13d
js 0x55e5
testq %r13, %r13
je 0x5827
movq 0x2b0(%rbx), %rcx
leaq (,%r13,2), %rsi
addq %r13, %rsi
movq -0x18(%rcx,%rsi,8), %rdx
cmpl $0x0, -0x4(%rdx,%rax,4)
je 0x594b
leaq (%rcx,%rsi,8), %rcx
movq -0x18(%rcx), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movq 0x2e0(%rbx), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rsi
addq $-0x18, %rsi
leaq -0x98(%rbp), %rdi
callq 0x5fba
movl $0xb, %edx
movq %r12, %rdi
leaq 0x2ced(%rip), %rsi      # 0x827d
callq 0x2200
movq 0x250(%rbx), %rax
shlq $0x5, %r13
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
movq %r12, %rdi
callq 0x2200
movl $0x4, %edx
movq %rax, %rdi
leaq 0x2cc9(%rip), %rsi      # 0x8289
callq 0x2200
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rdi
cmpq %rdi, %rax
je 0x5694
addq $-0x4, %rax
jmp 0x56cf
negl %r13d
cmpl %r13d, %eax
jne 0x5a07
movl $0x7, %edx
movq %r12, %rdi
leaq 0x2c1e(%rip), %rsi      # 0x821e
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r12, %rdi
callq 0x2200
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2c02(%rip), %rsi      # 0x8226
callq 0x2200
leaq -0x98(%rbp), %rdi
callq 0x2718
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 0x5674
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x21d0
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rdi
cmpq %rdi, %rax
je 0x57e5
addq $-0x4, %rax
jmp 0x5820
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), %rsi
movl (%rsi), %eax
xorl %r13d, %r13d
testl %eax, %eax
je 0x5737
movl $0x4, %r14d
movq -0xc0(%rbp), %rcx
movq -0xb0(%rbp), %rdx
addq $-0x4, %rdx
cmpq %rdx, %rcx
je 0x5712
movl %eax, (%rcx)
addq $0x4, %rcx
movq %rcx, -0xc0(%rbp)
jmp 0x571e
leaq -0xf0(%rbp), %rdi
callq 0x7354
movq -0x98(%rbp), %rsi
movl (%rsi,%r14), %eax
addq %r14, %rsi
incl %r13d
addq $0x4, %r14
testl %eax, %eax
jne 0x56ec
testl %r13d, %r13d
movl $0x200, %r14d           # imm = 0x200
jle 0x57a0
movl %r13d, %r13d
incq %r13
movq -0x98(%rbp), %rax
movl -0x8(%rax,%r13,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 %r12, %rdi
callq 0x2200
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2200
decq %r13
cmpq $0x1, %r13
jg 0x5748
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x5827
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x5827
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 -0xc0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jne 0x5845
movq -0xa8(%rbp), %rax
movq -0x8(%rax), %rax
addq %r14, %rax
cmpl $-0x1, -0x4(%rax)
jne 0x54f1
movq 0x577a(%rip), %rbx      # 0xafd0
leaq 0x2a31(%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 0x5748(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x58ae
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21d0
leaq -0xf0(%rbp), %rdi
callq 0x643c
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x56fd(%rip), %rdi      # 0xafd0
leaq 0x2917(%rip), %rsi      # 0x81f1
movl $0x17, %edx
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x56dd(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %rbx
leaq 0x2907(%rip), %rsi      # 0x8209
movl $0xa, %edx
movq %rax, %rdi
callq 0x2200
movl -0x30(%rbp), %esi
movq %rbx, %rdi
callq 0x22d0
movq %rax, %rbx
leaq 0x28f0(%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, %rsi
addq 0x268(%rbx), %rsi
leaq -0x98(%rbp), %rdi
callq 0x5fba
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x3, %rsi
addq 0x280(%rbx), %rsi
leaq -0x108(%rbp), %rdi
callq 0x5fba
movq 0x5645(%rip), %rdi      # 0xafd0
leaq 0x28c0(%rip), %rsi      # 0x8252
movl $0x27, %edx
callq 0x2200
movq 0x250(%rbx), %rax
shlq $0x5, %r13
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
movq 0x5619(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %rbx
leaq 0x28b4(%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 0x297a(%rip), %rsi      # 0x8367
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20e0
jmp 0x5944
movq 0x55c2(%rip), %rdi      # 0xafd0
leaq 0x2813(%rip), %rsi      # 0x8228
movl $0x8, %edx
callq 0x2200
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x55a2(%rip), %rdi      # 0xafd0
callq 0x2200
movq %rax, %r14
leaq 0x27cc(%rip), %rsi      # 0x8209
movl $0xa, %edx
movq %rax, %rdi
callq 0x2200
movl -0x30(%rbp), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x27b5(%rip), %rsi      # 0x8214
movl $0x9, %edx
movq %rax, %rdi
callq 0x2200
movl -0x2c(%rbp), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x27b0(%rip), %rsi      # 0x8231
movl $0x20, %edx
movq %rax, %rdi
callq 0x2200
movl %r13d, %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 0x2778(%rip), %rsi      # 0x822f
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x20e0
jmp 0x5944
jmp 0x5b01
movq %rax, %rbx
movq -0x108(%rbp), %rdi
testq %rdi, %rdi
je 0x5b0b
movq -0xf8(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x5b0b
jmp 0x5b08
jmp 0x5b01
jmp 0x5b01
jmp 0x5b01
jmp 0x5b01
jmp 0x5b08
jmp 0x5b08
movq %rax, %rbx
jmp 0x5b26
jmp 0x5b08
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x5b26
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5b3f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x21d0
leaq -0xf0(%rbp), %rdi
callq 0x643c
movq %rbx, %rdi
callq 0x22e0
nop