// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, %rbx
callq 0x8f1e
callq 0x9e5a
movq %rbx, %rdi
callq 0x6dba
movq %rax, %rbx
callq 0xa1c0
movq %rax, %rdi
xorl %esi, %esi
callq 0xa1d9
movq %rax, %r15
callq 0xa0a5
movq %rax, %r14
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x721c
movq %rax, %r13
pushq $-0x1
popq %rax
movl %eax, -0x2c(%rbp)
movq %r14, -0x38(%rbp)
xorl %ebx, %ebx
cmpl $0x6e, (%r13)
jne 0xa448
movl $0x1, 0x24(%r14)
xorl %eax, %eax
callq 0xd641
leaq 0x61d6(%rip), %rdi      # 0x105ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xd62c
pushq $0x20
popq %rdi
callq 0x1040
xorl %eax, %eax
callq 0xd6c8
movq 0x8(%r13), %rsi
leaq 0x61b5(%rip), %rdi      # 0x105f6
xorl %eax, %eax
callq 0x10b0
testq %r15, %r15
je 0xa4c0
movslq (%r15), %rax
testq %rax, %rax
js 0xa74f
movl (%r13), %ecx
cmpq $0x70, %rcx
jne 0xa46c
cmpl $0x70, %eax
je 0xa77f
leal -0x36(%rax), %edx
cmpl $0x3c, %edx
ja 0xa4d6
cmpl %ecx, %eax
jne 0xa564
movq %r15, %rdi
callq 0xa205
movq %rax, %r15
testl %ebx, %ebx
jne 0xa4af
movq 0x8(%r13), %rax
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rcx
movq %rax, 0x8(%rcx)
movl 0x10(%r13), %eax
movq 0x8(%rdi), %rcx
movl %eax, 0x10(%rcx)
callq 0xa371
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
callq 0x721c
movq %rax, %r13
jmp 0xa3fa
cmpl $0x70, (%r13)
movl $0x0, %r15d
jne 0xa3fa
jmp 0xa760
cmpl $0x35, %eax
ja 0xa3fa
imulq $0x258, %rax, %rax      # imm = 0x258
leaq 0xe273(%rip), %rdx      # 0x18760
addq %rdx, %rax
movl (%rax,%rcx,4), %r12d
cmpl $-0x1, %r12d
je 0xa5ea
movq %r15, %rdi
leaq 0x241f8(%rip), %rsi     # 0x2e700
movl %r12d, %edx
callq 0xa233
movq %rax, %r15
testl %ebx, %ebx
pushq $0x1
popq %rbx
jne 0xa3fa
movq -0x38(%rbp), %rbx
movl %r12d, 0x20(%rbx)
movq %rbx, %rdi
leaq 0x241d0(%rip), %rsi     # 0x2e700
movl %r12d, %edx
callq 0xa2dd
movq 0x28(%rbx), %rbx
movq %rbx, -0x38(%rbp)
cmpl $0x0, (%rbx)
movl $0x0, %ebx
jne 0xa3fa
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x36, (%rax)
jne 0xa3fa
jmp 0xa736
movl $0x1, 0x24(%r14)
movl -0x2c(%rbp), %ecx
cmpl 0x10(%r13), %ecx
je 0xa71f
addl $-0x62, %eax
cmpl $0x5, %eax
ja 0xa6cc
pushq $0x25
popq %rcx
btl %eax, %ecx
jae 0xa6cc
xorl %eax, %eax
callq 0xd659
leaq 0x604e(%rip), %rdi      # 0x105ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
decl %edi
callq 0xd62c
pushq $0x20
popq %rdi
callq 0x1040
xorl %eax, %eax
callq 0xd6c8
movslq (%r15), %rax
imulq $0x28, %rax, %rsi
leaq 0xca22(%rip), %rax      # 0x16ff0
addq %rax, %rsi
leaq 0x6073(%rip), %rdi      # 0x1064b
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
decl %eax
jmp 0xa71c
movl $0x1, 0x24(%r14)
movl -0x2c(%rbp), %eax
cmpl 0x10(%r13), %eax
je 0xa628
xorl %eax, %eax
callq 0xd659
leaq 0x5fe4(%rip), %rdi      # 0x105ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xd62c
pushq $0xa
popq %rdi
callq 0x1040
movl 0x10(%r13), %eax
movl %eax, -0x2c(%rbp)
movslq (%r15), %rax
leaq 0x24d4e(%rip), %rcx     # 0x2f380
leaq (%rcx,%rax,8), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xa65d
movl (%rax), %ecx
addq $0x8, %rax
cmpl (%r13), %ecx
jne 0xa636
movq %r15, %rdi
callq 0xa205
movq %rax, %r15
pushq $0x1
popq %rbx
jmp 0xa3fa
cmpl $0x70, (%r13)
je 0xa77f
movq -0x40(%rbp), %rdi
callq 0x721c
movq %rax, %r13
cmpl $0x6e, (%rax)
pushq $0x1
popq %rbx
jne 0xa3fa
movl $0x1, 0x24(%r14)
xorl %eax, %eax
callq 0xd641
leaq 0x5f57(%rip), %rdi      # 0x105ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xd62c
pushq $0x20
popq %rdi
callq 0x1040
xorl %eax, %eax
callq 0xd6c8
movq 0x8(%r13), %rsi
leaq 0x5f36(%rip), %rdi      # 0x105f6
xorl %eax, %eax
callq 0x10b0
jmp 0xa3fa
xorl %eax, %eax
callq 0xd659
leaq 0x5f13(%rip), %rdi      # 0x105ed
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %edi
callq 0xd62c
pushq $0x20
popq %rdi
callq 0x1040
xorl %eax, %eax
callq 0xd6c8
movslq (%r15), %rax
imulq $0x28, %rax, %rsi
leaq 0xc8e9(%rip), %rax      # 0x16ff0
addq %rax, %rsi
leaq 0x5f3a(%rip), %rdi      # 0x1064b
xorl %eax, %eax
callq 0x10b0
movl 0x10(%r13), %eax
movl %eax, -0x2c(%rbp)
movq 0x8(%r15), %rbx
movq %r15, %rdi
callq 0x1030
movq %rbx, %r15
pushq $0x1
popq %rbx
jmp 0xa3fa
movl 0x10(%r13), %ecx
movl %ecx, 0x10(%rax)
movq -0x38(%rbp), %rdi
callq 0xa371
movq %rax, -0x38(%rbp)
jmp 0xa3f8
movl $0x1, 0x24(%r14)
leaq 0x5eaa(%rip), %rbx      # 0x10608
jmp 0xa767
leaq 0x5ec7(%rip), %rbx      # 0x1062e
xorl %eax, %eax
callq 0xd659
xorl %eax, %eax
callq 0xd6c8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq -0x40(%rbp), %rdi
callq 0x6e5f
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq