// File: /workspace/github_c_makefile_star2_O3/anuwu[P]ERPLAG-Compiler/parser.c
// Function: parse
treeNode* parse (FILE *fp)
{
	fillUtilityArray();
	init_parser () ;

 	int rule_index , while_count = 1 ; 	
	twinBuffer *twinBuf = lexer_init (fp) ;
	token* tk ;
 
	stacknode* stack = initStack () ;
	stack = push (stack, program) ;
	treeNode *root = create_root () ;
 	treeNode* current_node ;
	current_node = root ;

	tk = getNextToken (twinBuf) ;
	int flag=0;
	int current=-1;

	while (1)
	{		
		if(tk->id == TK_LEXERROR)
		{
			root->syntax_error=1;
			errLex () ;
			printf ("at line ") ;
			errLine (tk->lineNumber) ;
			printf (" ") ;
			errArrow () ;
			#ifdef _WIN64
				printf (" %s\n", tk->lexeme) ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf (ANSI_BOLD ANSI_RED " %s\n" ANSI_RESET, tk->lexeme) ;
			#endif
		}
		
		if(stack!=NULL && stack->key<0)
		{
			flag=1;
			root->syntax_error=1;
			errParse () ;
			errArrow () ;
			#ifdef _WIN64
				printf ("Invalid symbol on stack\n") ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf(ANSI_BOLD ANSI_RED "Invalid symbol on stack\n" ANSI_RESET);
			#endif
			break;
		}

		if (tk->id == TK_EOF)
		{
			if(stack!= NULL && stack->key==TK_EOF)
				break ;
			
			if(stack==NULL)
			{
				errParse () ;
				errArrow () ;
				#ifdef _WIN64
					printf ("Stack is empty\n") ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf(ANSI_BOLD ANSI_RED "Stack is empty\n" ANSI_RESET);
				#endif
				break;
			}
		}
 
		if (isTerminal(stack->key)) // We are getting a terminal on top of the stack
		{
			if (stack->key == tk->id) // If top of the stack matches with the input symbol
			{
				stack = pop (stack); // Pop the element from top of the stack and move ahead
				if(flag==0) // No error has been encountered yet
				{
					//Add the terminal to the parse tree
					current_node->tnt.term->lexeme =  tk->lexeme ; 
					current_node->tnt.term->lineNumber = tk->lineNumber ;
					current_node=nextTreeNode(current_node);
				}

				// Get the new look-ahead symbol
				tk = getNextToken (twinBuf) ;
			}
			else   // The top of stack is a terminal but doesn't match with the input symbol
			{
				root->syntax_error=1;
				if(current != tk->lineNumber)
				{
					if(stack->key!= TK_SEMICOL && stack->key!= TK_ENDDEF && stack->key!= TK_DRIVERENDDEF)
					{
						errParse () ;
						printf ("at line ") ;
						errLine (tk->lineNumber) ;
						printf (" ") ;
						errArrow () ;
						#ifdef _WIN64
							printf ("%s symbol was expected\n", utility_array[stack->key]) ;
						#endif
						#if defined __linux__ || defined __MACH__
							printf (ANSI_BOLD ANSI_RED "%s" ANSI_RESET " symbol was expected\n", utility_array[stack->key]);
						#endif
						current=tk->lineNumber;
					}
					else
					{
						errParse () ;
						printf ("at line ") ;
						errLine (tk->lineNumber - 1) ;
						printf (" ") ;
						errArrow () ;
						#ifdef _WIN64
							printf ("%s symbol was expected\n", utility_array[stack->key]) ;
						#endif
						#if defined __linux__ || defined __MACH__
							printf (ANSI_BOLD ANSI_RED "%s" ANSI_RESET " symbol was expected\n", utility_array[stack->key]);
						#endif
						current=tk->lineNumber-1;
					}
					
				}
				stacknode* temp=stack;
				if(stack==NULL)
				{
					errParse () ;
					errArrow () ;
					#ifdef _WIN64
						printf ("Stack is empty\n") ;
					#endif
					#if defined __linux__ || defined __MACH__
						printf(ANSI_BOLD ANSI_RED "Stack is empty\n" ANSI_RESET);
					#endif
					break;
				}
				 stack=stack->next;

				free(temp); // Poping the stack
				flag=1; // There is an error
				
			}
		}
		else if (isNonTerminal(stack->key))
		{
			
			rule_index = parsetable[stack->key][tk->id] ;
 
			if (rule_index != -1)
			{
				stack = pushRule (stack , allRules, rule_index) ;
				if(flag==0)
				{
					current_node->gcode = rule_index ;

					addRulesParseTree (current_node , allRules, rule_index);
					current_node = current_node->child ;
 
					if(current_node->tag == TERMINAL && current_node->tnt.term->id == TK_EPS)
					{
						current_node->tnt.term->lineNumber = tk->lineNumber ;
						current_node=nextTreeNode(current_node);
					}
				}
			}
			else
			{
				root->syntax_error=1;
				if(current != tk->lineNumber)
				{
					errParse () ;
					printf ("at line ");
					errLine (tk->lineNumber) ;
					printf ("\n") ;
					current=tk->lineNumber;
				}

				if(stack==NULL)
				{
					errParse () ;
					errArrow () ;
					#ifdef _WIN64
						printf ("Stack is empty\n") ;
					#endif
					#if defined __linux__ || defined __MACH__
						printf(ANSI_BOLD ANSI_RED "Stack is empty\n" ANSI_RESET);
					#endif
					break;
				}
				flag=1;
					while(1)
					{
						node* temp=NULL;
						temp=follows[stack->key];
						while(temp != NULL && temp->key != tk->id)
							temp=temp->next;

						if(stack!=NULL && temp!=NULL)
						{
							stack=pop(stack);
							break;
						}

						else
						{
							if(tk->id != TK_EOF)
							{
								tk=getNextToken(twinBuf);
								
								if(tk->id == TK_LEXERROR)
								{
									root->syntax_error=1;
									errLex () ;
									printf ("at line ") ;
									errLine (tk->lineNumber) ;
									printf (" ") ;
									errArrow () ;
									#ifdef _WIN64
										printf (" %s\n", tk->lexeme) ;
									#endif
									#if defined __linux__ || defined __MACH__
										printf (ANSI_BOLD ANSI_RED " %s\n" ANSI_RESET, tk->lexeme) ;
									#endif
								}
								break;
							}

							if(tk->id == TK_EOF)
								goto labelabc;
							
						}
					}
			}
		}
 
		while_count++ ;
	}

 	labelabc:
 	lexer_destroy (twinBuf) ;

	return root ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
callq 0x96e6
callq 0xa6cf
movq %rbx, %rdi
callq 0x734e
movq %rax, %r12
movl $0x10, %edi
callq 0x1120
movq %rax, %rbx
movl $0x70, (%rax)
movq $0x0, 0x8(%rax)
movl $0x10, %edi
callq 0x1120
movq %rax, %r15
xorl %r14d, %r14d
movl %r14d, (%rax)
movq %rbx, 0x8(%rax)
movl $0x40, %edi
callq 0x1120
movq %rax, %rbx
movl $0x1, (%rax)
movl %r14d, 0x8(%rax)
leaq 0x24(%rax), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x24(%rax)
movups %xmm0, 0x30(%rax)
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movups %xmm0, 0x10(%rax)
movq %r12, -0x48(%rbp)
movq %r12, %rdi
callq 0x77d6
movq %rax, %r13
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movq %rbx, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq %r14, -0x40(%rbp)
cmpl $0x6e, (%r13)
jne 0xae1a
movl $0x1, (%r14)
xorl %eax, %eax
callq 0xe947
leaq 0x6806(%rip), %rdi      # 0x115ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xe932
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xea16
movq 0x8(%r13), %rsi
leaq 0x67e3(%rip), %rdi      # 0x115f6
xorl %eax, %eax
callq 0x10b0
testq %r15, %r15
je 0xae6c
movslq (%r15), %rcx
testq %rcx, %rcx
js 0xb186
movl (%r13), %eax
cmpq $0x70, %rax
jne 0xae3e
cmpl $0x70, %ecx
je 0xb1b5
leal -0x36(%rcx), %edx
cmpl $0x3c, %edx
ja 0xae82
cmpl %eax, %ecx
jne 0xaf52
movq 0x8(%r15), %r12
movq %r15, %rdi
callq 0x1030
cmpl $0x0, -0x38(%rbp)
je 0xafdc
movq %rbx, %r14
jmp 0xaff8
cmpl $0x70, (%r13)
movl $0x0, %r15d
jne 0xadcb
jmp 0xb196
cmpl $0x35, %ecx
ja 0xadcb
imulq $0x258, %rcx, %rcx      # imm = 0x258
leaq 0xe8d7(%rip), %rdx      # 0x19770
addq %rdx, %rcx
movl (%rcx,%rax,4), %r12d
cmpl $-0x1, %r12d
je 0xb02a
movq %r15, %rdi
leaq 0x2485c(%rip), %rsi     # 0x2f710
movl %r12d, %edx
callq 0xab21
movq %rax, %r15
cmpl $0x0, -0x38(%rbp)
movl $0x1, %eax
movq %rax, -0x38(%rbp)
jne 0xadcb
movl %r12d, 0x20(%rbx)
movq %rbx, %rdi
leaq 0x24830(%rip), %rsi     # 0x2f710
movl %r12d, %edx
callq 0xac0a
movq 0x28(%rbx), %rax
cmpl $0x0, (%rax)
movq %rax, %rbx
movl $0x0, %ecx
movq %rcx, -0x38(%rbp)
jne 0xadcb
movq 0x8(%rax), %rcx
cmpl $0x36, (%rcx)
movq %rax, %rbx
movl $0x0, %edx
movq %rdx, -0x38(%rbp)
jne 0xadcb
movl 0x10(%r13), %edx
movl %edx, 0x10(%rcx)
movq 0x30(%rax), %rbx
movl $0x0, %ecx
movq %rcx, -0x38(%rbp)
testq %rbx, %rbx
jne 0xadcb
movq 0x38(%rax), %rax
movq $0x0, -0x38(%rbp)
testq %rax, %rax
je 0xb17f
movq 0x30(%rax), %rbx
jmp 0xaf2e
movl $0x1, (%r14)
movl -0x2c(%rbp), %eax
cmpl 0x10(%r13), %eax
je 0xb15e
addl $-0x62, %ecx
cmpl $0x5, %ecx
ja 0xb108
movl $0x25, %eax
btl %ecx, %eax
jae 0xb108
xorl %eax, %eax
callq 0xe968
leaq 0x665f(%rip), %rdi      # 0x115ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
decl %edi
callq 0xe932
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xea16
movslq (%r15), %rax
leaq (%rax,%rax,4), %rax
leaq 0xd041(%rip), %rcx      # 0x18000
leaq (%rcx,%rax,8), %rsi
leaq 0x6681(%rip), %rdi      # 0x1164b
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
decl %eax
jmp 0xb15b
movq 0x8(%r13), %rax
movq 0x8(%rbx), %rcx
movq %rax, 0x8(%rcx)
movl 0x10(%r13), %eax
movq 0x8(%rbx), %rcx
movl %eax, 0x10(%rcx)
cmpl $0x0, (%rbx)
je 0xb013
movq -0x48(%rbp), %rdi
callq 0x77d6
movq %rax, %r13
movq %r12, %r15
movq %r14, %rbx
movq -0x40(%rbp), %r14
jmp 0xadcb
movq 0x30(%rbx), %r14
testq %r14, %r14
jne 0xaff8
movq 0x38(%rbx), %rbx
testq %rbx, %rbx
jne 0xb013
xorl %r14d, %r14d
jmp 0xaff8
movl $0x1, (%r14)
movl -0x2c(%rbp), %ecx
cmpl 0x10(%r13), %ecx
je 0xb06d
xorl %eax, %eax
callq 0xe968
leaq 0x65a5(%rip), %rdi      # 0x115ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xe932
movl $0xa, %edi
callq 0x1040
movl (%r13), %eax
movl 0x10(%r13), %ecx
movl %ecx, -0x2c(%rbp)
movslq (%r15), %rcx
leaq 0x25319(%rip), %rdx     # 0x30390
movq (%rdx,%rcx,8), %rcx
testq %rcx, %rcx
je 0xb08e
cmpl %eax, (%rcx)
je 0xb15e
movq 0x8(%rcx), %rcx
jmp 0xb07b
cmpl $0x70, %eax
movq -0x40(%rbp), %r14
je 0xb1b5
movq -0x48(%rbp), %rdi
callq 0x77d6
movq %rax, %r13
movl $0x1, %eax
movq %rax, -0x38(%rbp)
cmpl $0x6e, (%r13)
jne 0xadcb
movl $0x1, (%r14)
xorl %eax, %eax
callq 0xe947
leaq 0x651d(%rip), %rdi      # 0x115ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xe932
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xea16
movq 0x8(%r13), %rsi
leaq 0x64fa(%rip), %rdi      # 0x115f6
xorl %eax, %eax
callq 0x10b0
jmp 0xadcb
xorl %eax, %eax
callq 0xe968
leaq 0x64d7(%rip), %rdi      # 0x115ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xe932
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xea16
movslq (%r15), %rax
leaq (%rax,%rax,4), %rax
leaq 0xcebb(%rip), %rcx      # 0x18000
leaq (%rcx,%rax,8), %rsi
leaq 0x64fb(%rip), %rdi      # 0x1164b
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
movl %eax, -0x2c(%rbp)
movq 0x8(%r15), %r14
movq %r15, %rdi
callq 0x1030
movl $0x1, %eax
movq %rax, -0x38(%rbp)
movq %r14, %r15
movq -0x40(%rbp), %r14
jmp 0xadcb
xorl %ebx, %ebx
jmp 0xadcb
movl $0x1, (%r14)
leaq 0x6474(%rip), %rbx      # 0x11608
jmp 0xb19d
leaq 0x6491(%rip), %rbx      # 0x1162e
xorl %eax, %eax
callq 0xe968
xorl %eax, %eax
callq 0xea16
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq -0x48(%rbp), %rdi
callq 0x73fc
movq -0x50(%rbp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq