// File: /workspace/github_c_makefile_star2_O2/anuwu[P]ERPLAG-Compiler/parser.c
// Function: init_parser
void init_parser()
{
	rule_count =0;

	for(int i=0;i<SIZE;i++)
	{
		heads[i].ind=-1;
		heads[i].next=NULL;
	}

 
	// file handling

	int slash = 0 ;
	char grammarPath[500] ;
	strcpy (grammarPath, __FILE__) ;

	slash = strlen (grammarPath) - 1 ;

	#ifdef _WIN64
	while (grammarPath[slash] != '\\')
	#endif
	#if defined __linux__ || defined __MACH__	
	while (grammarPath[slash] != '/')
	#endif
		slash-- ;

	grammarPath[slash+1] = '\0' ;
	strcat (grammarPath, "grammar.txt") ;
	FILE* file_pointer=fopen(grammarPath, "r");
	
	int st=0; // 3 states 0-LHS 1-RHS_head 2-rests
	char tempstr[TNTLENGTH];
	node* tmp;
	
	while( fscanf(file_pointer,"%[^\n\r ])",tempstr) != EOF)
	{
		if(st==0){
			rule[rule_count].LHS = grammarStringToTokenID(tempstr);
			populate_hash(rule_count,grammarStringToTokenID(tempstr));
			st++;
		}
		else if(st==1){
			rule[rule_count].RHS = initnode(grammarStringToTokenID(tempstr));
			tmp = rule[rule_count].RHS;
			st++;
		}
		else if(st!=-1){
			tmp->next = initnode(grammarStringToTokenID(tempstr));
			tmp = tmp->next;
		}
 
		char ch;
		ch=fgetc(file_pointer);
		if(ch=='\r') st=-1;
		else if(ch=='\n'){
			rule_count++;
			st=0;
		}
	}
	rule_count++;
	fclose(file_pointer);

 
 	int id ;
	for (id = program ; id <= TK_RNUM ; id++)
	{
		if(isTerminal(id))
			firsts[id] = initnode (id) ;	
		else 
			firsts[id] = NULL;
	}



	for (id = program ; id <= TK_RNUM ; id++)
	{
		if(isNonTerminal(id) && firsts[id] == NULL)
			populateFirst((tokenID)id);	
	}
	
	
	follows[program] = initnode (TK_EOF) ;
	for (id = program + 1 ; id <= idL ; id++)
		follows[id] = NULL ;


	for (id = program + 1 ; id <= idL ; id++)
	{
		if (isNonTerminal(id))
			populateFollow ((tokenID)id) ;
	}

	traverse_grammar() ;
}

// Assembly:
andl $0x0, 0x2488f(%rip)     # 0x2e6f0
pushq $0x8
popq %rax
leaq 0xc505(%rip), %rcx      # 0x16370
cmpq $0xc88, %rax            # imm = 0xC88
je 0x9e83
orl $-0x1, -0x8(%rax,%rcx)
andq $0x0, (%rax,%rcx)
addq $0x10, %rax
jmp 0x9e6b
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
leaq 0x66e1(%rip), %rsi      # 0x1057f
leaq -0x250(%rbp), %rbx
pushq $0x47
popq %rdx
movq %rbx, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x10a0
incl %eax
leal -0x2(%rax), %ecx
movslq %ecx, %rcx
decl %eax
cmpb $0x2f, -0x250(%rbp,%rcx)
jne 0x9eba
cltq
movb $0x0, -0x250(%rbp,%rax)
leaq -0x250(%rbp), %rbx
movq %rbx, %rdi
callq 0x10a0
movabsq $0x2e72616d6d617267, %rcx # imm = 0x2E72616D6D617267
movq %rcx, -0x250(%rbp,%rax)
movl $0x747874, -0x248(%rbp,%rax) # imm = 0x747874
leaq 0x64ec(%rip), %rsi      # 0x103f5
movq %rbx, %rdi
callq 0x1120
movq %rax, %rbx
leaq -0x50(%rbp), %r15
xorl %r13d, %r13d
movq %rbx, %rdi
leaq 0x66ad(%rip), %rsi      # 0x105d2
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
cmpl $-0x1, %eax
je 0x9fed
cmpl $-0x1, %r13d
je 0x9fa0
movq %r15, %rdi
cmpl $0x1, %r13d
je 0x9f74
testl %r13d, %r13d
jne 0x9fa5
callq 0x823d
movslq 0x24798(%rip), %rdi     # 0x2e6f0
movq %rdi, %rcx
shlq $0x4, %rcx
leaq 0x2479a(%rip), %rdx     # 0x2e700
movl %eax, (%rcx,%rdx)
movl %eax, %esi
callq 0x81f7
pushq $0x1
jmp 0x9f9c
callq 0x823d
movl %eax, %edi
callq 0x8f00
movq %rax, %r12
movslq 0x24766(%rip), %rax     # 0x2e6f0
shlq $0x4, %rax
leaq 0x2476b(%rip), %rcx     # 0x2e700
movq %r12, 0x8(%rax,%rcx)
pushq $0x2
popq %r14
jmp 0x9fbc
movl %r13d, %r14d
jmp 0x9fbc
callq 0x823d
movl %eax, %edi
callq 0x8f00
movq %rax, 0x8(%r12)
movl %r13d, %r14d
movq %rax, %r12
movq %rbx, %rdi
callq 0x10c0
pushq $-0x1
popq %r13
cmpb $0xd, %al
je 0x9f1b
movzbl %al, %eax
movl %r14d, %r13d
cmpl $0xa, %eax
jne 0x9f1b
incl 0x2470b(%rip)           # 0x2e6f0
xorl %r13d, %r13d
jmp 0x9f1b
incl 0x246fd(%rip)           # 0x2e6f0
movq %rbx, %rdi
callq 0x1090
leaq 0x259be(%rip), %r14     # 0x2f9c0
xorl %ebx, %ebx
cmpq $0x73, %rbx
je 0xa027
cmpq $0x36, %rbx
jb 0xa019
movl %ebx, %edi
callq 0x8f00
jmp 0xa01b
xorl %eax, %eax
movq %rax, (%r14)
incq %rbx
addq $0x8, %r14
jmp 0xa004
leaq 0x25992(%rip), %r14     # 0x2f9c0
xorl %ebx, %ebx
cmpq $0x73, %rbx
je 0xa052
cmpq $0x35, %rbx
ja 0xa049
cmpq $0x0, (%r14)
jne 0xa049
movl %ebx, %edi
callq 0x9b27
incq %rbx
addq $0x8, %r14
jmp 0xa030
pushq $0x70
popq %rdi
callq 0x8f00
movq %rax, 0x2531f(%rip)     # 0x2f380
pushq $0x1
popq %rax
leaq 0x25315(%rip), %rcx     # 0x2f380
cmpq $0x36, %rax
je 0xa07b
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0xa06b
pushq $0x1
popq %rbx
cmpl $0x36, %ebx
je 0xa08e
movl %ebx, %edi
callq 0x9c1a
incl %ebx
jmp 0xa07e
callq 0x98df
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq