// File: /workspace/github_c_makefile_star2_O1/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 0xa165
callq 0xb17b
movq %rbx, %rdi
callq 0x7396
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, -0x40(%rbp)
movq %r12, %rdi
callq 0x7823
movq %rax, %r13
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movq %rbx, -0x50(%rbp)
movq %rbx, %r12
movq $0x0, -0x38(%rbp)
movq %r14, -0x48(%rbp)
cmpl $0x6e, (%r13)
jne 0xb8e0
movl $0x1, (%r14)
xorl %eax, %eax
callq 0xf1c3
leaq 0x6b6c(%rip), %rdi      # 0x12419
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xf1ae
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xf292
movq 0x8(%r13), %rsi
leaq 0x6b49(%rip), %rdi      # 0x12422
xorl %eax, %eax
callq 0x10b0
testq %r15, %r15
je 0xb8ef
cmpl $0x0, (%r15)
js 0xbc80
movl (%r13), %eax
cmpq $0x70, %rax
jne 0xb915
testq %r15, %r15
je 0xbc77
cmpl $0x70, (%r15)
je 0xbca6
testq %r15, %r15
je 0xbc77
movl (%r15), %ecx
leal -0x36(%rcx), %edx
cmpl $0x3c, %edx
ja 0xb946
cmpl %eax, %ecx
jne 0xba14
movq 0x8(%r15), %rbx
movq %r15, %rdi
callq 0x1030
cmpl $0x0, -0x38(%rbp)
je 0xba9e
movq %r12, %r14
jmp 0xbabe
cmpl $0x35, %ecx
ja 0xb891
imulq $0x258, %rcx, %rcx      # imm = 0x258
leaq 0xee13(%rip), %rdx      # 0x1a770
addq %rdx, %rcx
movl (%rcx,%rax,4), %ebx
cmpl $-0x1, %ebx
je 0xbaf2
movq %r15, %rdi
leaq 0x24d9a(%rip), %rsi     # 0x30710
movl %ebx, %edx
callq 0xb5cd
movq %rax, %r15
cmpl $0x0, -0x38(%rbp)
movl $0x1, %eax
movq %rax, -0x38(%rbp)
jne 0xb891
movl %ebx, 0x20(%r12)
movq %r12, %rdi
leaq 0x24d6e(%rip), %rsi     # 0x30710
movl %ebx, %edx
callq 0xb6b9
movq 0x28(%r12), %rax
cmpl $0x0, (%rax)
movq %rax, %r12
movl $0x0, %ecx
movq %rcx, -0x38(%rbp)
jne 0xb891
movq 0x8(%rax), %rcx
cmpl $0x36, (%rcx)
movq %rax, %r12
movl $0x0, %edx
movq %rdx, -0x38(%rbp)
jne 0xb891
movl 0x10(%r13), %edx
movl %edx, 0x10(%rcx)
movq 0x30(%rax), %r12
movl $0x0, %ecx
movq %rcx, -0x38(%rbp)
testq %r12, %r12
jne 0xb891
movq 0x38(%rax), %rax
movq $0x0, -0x38(%rbp)
testq %rax, %rax
je 0xbc6f
movq 0x30(%rax), %r12
jmp 0xb9f0
movl $0x1, (%r14)
movl -0x2c(%rbp), %eax
cmpl 0x10(%r13), %eax
je 0xbc26
addl $-0x62, %ecx
cmpl $0x5, %ecx
ja 0xbbd0
movl $0x25, %eax
btl %ecx, %eax
jae 0xbbd0
xorl %eax, %eax
callq 0xf1e4
leaq 0x69c9(%rip), %rdi      # 0x12419
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
decl %edi
callq 0xf1ae
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xf292
movslq (%r15), %rax
leaq (%rax,%rax,4), %rax
leaq 0xd57f(%rip), %rcx      # 0x19000
leaq (%rcx,%rax,8), %rsi
leaq 0x69eb(%rip), %rdi      # 0x12477
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
decl %eax
jmp 0xbc23
movq 0x8(%r13), %rax
movq 0x8(%r12), %rcx
movq %rax, 0x8(%rcx)
movl 0x10(%r13), %eax
movq 0x8(%r12), %rcx
movl %eax, 0x10(%rcx)
cmpl $0x0, (%r12)
je 0xbad9
movq -0x40(%rbp), %rdi
callq 0x7823
movq %rax, %r13
movq %rbx, %r15
movq %r14, %r12
movq -0x48(%rbp), %r14
jmp 0xb891
movq 0x30(%r12), %r14
testq %r14, %r14
jne 0xbabe
movq 0x38(%r12), %r12
testq %r12, %r12
jne 0xbad9
xorl %r14d, %r14d
jmp 0xbabe
movl $0x1, (%r14)
movl -0x2c(%rbp), %eax
cmpl 0x10(%r13), %eax
je 0xbb31
xorl %eax, %eax
callq 0xf1e4
leaq 0x6909(%rip), %rdi      # 0x12419
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xf1ae
movl $0xa, %edi
callq 0x1040
movl 0x10(%r13), %eax
movl %eax, -0x2c(%rbp)
movslq (%r15), %rax
leaq 0x25855(%rip), %rcx     # 0x31390
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0xbb59
movl (%r13), %ecx
cmpl %ecx, (%rax)
je 0xbc43
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0xbb48
movl $0x8, %ebx
cmpl $0x70, (%r13)
je 0xbc5b
movq -0x40(%rbp), %rdi
callq 0x7823
movq %rax, %r13
movl $0x5, %ebx
cmpl $0x6e, (%rax)
jne 0xbc5b
movl $0x1, (%r14)
xorl %eax, %eax
callq 0xf1c3
leaq 0x6881(%rip), %rdi      # 0x12419
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xf1ae
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xf292
movq 0x8(%r13), %rsi
leaq 0x685e(%rip), %rdi      # 0x12422
xorl %eax, %eax
callq 0x10b0
jmp 0xbc5b
xorl %eax, %eax
callq 0xf1e4
leaq 0x683b(%rip), %rdi      # 0x12419
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xf1ae
movl $0x20, %edi
callq 0x1040
xorl %eax, %eax
callq 0xf292
movslq (%r15), %rax
leaq (%rax,%rax,4), %rax
leaq 0xd3f3(%rip), %rcx      # 0x19000
leaq (%rcx,%rax,8), %rsi
leaq 0x685f(%rip), %rdi      # 0x12477
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
movl %eax, -0x2c(%rbp)
movq 0x8(%r15), %rbx
movq %r15, %rdi
callq 0x1030
movl $0x1, %eax
movq %rax, -0x38(%rbp)
movq %rbx, %r15
jmp 0xb891
movq 0x8(%r15), %r14
movq %r15, %rdi
callq 0x1030
movl $0x5, %ebx
movq %r14, %r15
movq -0x48(%rbp), %r14
movl $0x1, %eax
movq %rax, -0x38(%rbp)
cmpl $0x5, %ebx
je 0xb891
jmp 0xbcc2
xorl %r12d, %r12d
jmp 0xb891
leaq 0x67dc(%rip), %rbx      # 0x1245a
jmp 0xbc8e
movl $0x1, (%r14)
leaq 0x67a6(%rip), %rbx      # 0x12434
xorl %eax, %eax
callq 0xf1e4
xorl %eax, %eax
callq 0xf292
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq -0x40(%rbp), %rdi
callq 0x7444
movq -0x50(%rbp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x8, %ebx
je 0xbca6
jmp 0xbcaf