// File: /workspace/github_c_makefile_star2_O1/Gaurang18[P]Compiler-Project-BITS-Pilani/./parser.c
// Function: parse_Source_Code
parsetree parse_Source_Code(char *filename){   //to Parse Source Code

    parsetree head;

    head = (parsetree) malloc (sizeof(struct Parse_Tree));
    head->Lex = createnode();
    strcpy(head->Lex->tk->token, "PROGRAM");
    strcpy(head->Lex->tk->value, "program");
    head->Lex->tk->line = 1;
    head->value = 1;
    head->child = NULL;
    head->left = NULL;
    head->right = NULL;
    head->isleaf = 0;
    head->isVisited = 0;
    head->parent = NULL;


    FILE* fp = fopen(filename,"r");
    //char B[4096];
    //int bufflen =4096;
    rewind(fp);
    fseek( fp, 0, SEEK_SET);
    fp = getstream(fp);
    push(0);
    push(1);
     
    node* Lex = getNextToken();
    //assert(0);
    int po=0;
    int count = 0;
    printf("token = %s, value = %s, line = %d\n", Lex->tk->token, Lex->tk->value, Lex->tk->line);
    while(strcmp(Lex->tk->value,"$")!=0){
        char *token = Lex->tk->token;
        printf("Parsing Token -> %s\n",token);
        if(strcmp(token,"COMMENTMARK") == 0){
            Lex = getNextToken();
            token = Lex->tk->token;
            if(stack[top] == 0 || stack[top] == 3){
                break;
            }
        }
        if(stack[top] > NON_TERMINALS){
            if(stack[top] == (find_location(token))){
                
                pop();
                count++;
                Lex = getNextToken();
                token = Lex->tk->token;
                printstack();
            }else{
                printf("\nERROR_5: The token %s for lexeme %s does not match at line %d. The expected token here is %s\n",Lex->tk->token,Lex->tk->value,Lex->tk->line,terms[stack[top]]); //Terminal is not a match
                exit(0);
            }
        }else{
            int rule = parse_table[stack[top] - 1][(find_location(token)-NON_TERMINALS)];
            
            if(rule == -1){
                printf("\nERROR_5: The token %s for lexeme %s does not match at line %d. The expected token here is %s\n",Lex->tk->token,Lex->tk->value,Lex->tk->line,terms[stack[top]]); //Rule not found
                //exit(0);
                Lex = getNextToken();
                token = Lex->tk->token;
                while((rule = parse_table[stack[top] - 1][(find_location(token)-NON_TERMINALS)]) == -1){
                    Lex = getNextToken();
                    token = Lex->tk->token;
                }
            }else{
                if(head->child==NULL)
                    insert (head, rule, G, Lex);    //populate parse tree 
                else{
                  parsetree tmp = search(head, stack[top]);
                  insert(tmp, rule, G, Lex);
                }
                int right;
                pop();  
                for(right = size_of(G[rule-1])-1; right > 0; right--){
                    int alpha = G[rule-1][right];
                       
                    if(alpha != EPLS)               
                        push(alpha);
                }
                printstack();
            }
        }
    }
    count++;
    if(stack[top] == 3){
            pop();
    }
    //printstack();
    if(stack[top] == 0){                //Stack is empty - Successful Parsing
        pop();
        printf("\nSUCCESFUL PARSING.... THANK YOU\n");
    }
    printf("Count %d\n",count);

    //populateLexemes(head, filename);

    return head;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movl $0x40, %edi
callq 0x2150
movq %rax, %r12
movl $0x8, %edi
callq 0x2150
movq %rax, %r15
movl $0x40, %edi
callq 0x2150
movq %rax, (%r15)
movq %r15, (%r12)
movabsq $0x4d4152474f5250, %rcx # imm = 0x4D4152474F5250
movq %rcx, (%rax)
movabsq $0x6d6172676f7270, %rcx # imm = 0x6D6172676F7270
movq %rcx, 0x1e(%rax)
movl $0x1, %ebx
movl %ebx, 0x3c(%rax)
movl %ebx, 0x8(%r12)
leaq 0x18(%r12), %rax
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r12)
movups %xmm0, 0x28(%r12)
movq %r12, -0x40(%rbp)
movq $0x0, 0x38(%r12)
leaq 0x6480(%rip), %rsi      # 0xa6f6
movq %r14, %rdi
callq 0x2170
movq %rax, %r14
movq %rax, %rdi
callq 0x20c0
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r14, %rdi
callq 0x2441
movslq 0xb9bc(%rip), %rax      # 0xfc60
leaq 0x165e5(%rip), %r14     # 0x1a890
movl $0x0, 0x4(%r14,%rax,4)
movl %ebx, 0x8(%r14,%rax,4)
addq $0x2, %rax
movl %eax, 0xb99d(%rip)      # 0xfc60
callq 0x255b
movq %rax, %r12
movq (%rax), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
leaq 0x6213(%rip), %rdi      # 0xa4ef
xorl %eax, %eax
callq 0x20b0
movl $0x0, -0x2c(%rbp)
movq (%r12), %r15
leaq 0x1e(%r15), %rdi
leaq 0x61f0(%rip), %rsi      # 0xa4e9
callq 0x2120
testl %eax, %eax
je 0x45a7
leaq 0x527c(%rip), %rdi      # 0x9589
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
movq %r15, %rdi
leaq 0x527d(%rip), %rsi      # 0x959e
callq 0x2120
testl %eax, %eax
je 0x43a1
movq %r12, -0x38(%rbp)
movslq 0xb92b(%rip), %rbx      # 0xfc60
movslq (%r14,%rbx,4), %r12
cmpq $0x34, %r12
jl 0x43c8
leaq 0x8736(%rip), %r13      # 0xca80
xorl %r14d, %r14d
movq (%r13), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x4370
incq %r14
addq $0x8, %r13
cmpq $0x6e, %r14
jne 0x434d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl %r14d, %r12d
jne 0x4615
leaq 0x16510(%rip), %r14     # 0x1a890
movl $0xffffffff, (%r14,%rbx,4) # imm = 0xFFFFFFFF
leal -0x1(%rbx), %eax
movl %eax, 0xb8cf(%rip)      # 0xfc60
incl -0x2c(%rbp)
callq 0x255b
movq %rax, %r12
jmp 0x4598
callq 0x255b
movq %rax, %r12
movslq 0xb8b0(%rip), %rax      # 0xfc60
movl (%r14,%rax,4), %eax
testl %eax, %eax
je 0x43c1
cmpl $0x3, %eax
jne 0x4508
xorl %eax, %eax
jmp 0x459f
leaq -0x1(%r12), %r14
leaq 0x86ac(%rip), %r13      # 0xca80
xorl %ebx, %ebx
movq (%r13), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x43fa
incq %rbx
addq $0x8, %r13
cmpq $0x6e, %rbx
jne 0x43d6
movq $-0x1, %rbx
shlq $0x20, %rbx
movabsq $-0x3300000000, %rax    # imm = 0xFFFFFFCD00000000
addq %rax, %rbx
imulq $0xe8, %r14, %rax
leaq 0x16547(%rip), %rcx     # 0x1a960
addq %rcx, %rax
sarq $0x1e, %rbx
movslq (%rbx,%rax), %r15
cmpq $-0x1, %r15
je 0x445a
movq -0x48(%rbp), %rax
cmpq $0x0, (%rax)
leaq 0x16457(%rip), %r14     # 0x1a890
leaq 0xa390(%rip), %rbx      # 0xe7d0
je 0x4511
movq -0x40(%rbp), %rdi
movl %r12d, %esi
callq 0x3534
movq %rax, %rdi
jmp 0x4515
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
leaq 0x8611(%rip), %rax      # 0xca80
movq (%rax,%r12,8), %r8
leaq 0x5130(%rip), %rdi      # 0x95aa
xorl %eax, %eax
callq 0x20b0
leaq 0x16408(%rip), %r14     # 0x1a890
callq 0x255b
movq %rax, %r12
movq (%rax), %r15
movslq 0xb7c6(%rip), %rax      # 0xfc60
movslq (%r14,%rax,4), %rbx
decq %rbx
leaq 0x85d8(%rip), %r14      # 0xca80
xorl %r13d, %r13d
movq (%r14), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x44ce
incq %r13
addq $0x8, %r14
cmpq $0x6e, %r13
jne 0x44ab
movq $-0x1, %r13
shlq $0x20, %r13
movabsq $-0x3300000000, %rax    # imm = 0xFFFFFFCD00000000
addq %rax, %r13
imulq $0xe8, %rbx, %rax
leaq 0x16473(%rip), %rcx     # 0x1a960
addq %rcx, %rax
sarq $0x1e, %r13
cmpl $-0x1, (%r13,%rax)
leaq 0x1638f(%rip), %r14     # 0x1a890
je 0x4488
jmp 0x459d
movq (%r12), %r15
jmp 0x432a
movq -0x40(%rbp), %rdi
movl %r15d, %esi
movq %rbx, %rdx
movq -0x38(%rbp), %r12
movq %r12, %rcx
callq 0x3581
movslq 0xb732(%rip), %rax      # 0xfc60
movl $0xffffffff, (%r14,%rax,4) # imm = 0xFFFFFFFF
decl %eax
movl %eax, 0xb722(%rip)      # 0xfc60
decq %r15
imulq $0x38, %r15, %rax
cmpl $-0x1, (%rax,%rbx)
je 0x4561
leaq (%rax,%rbx), %rcx
addq $0x4, %rcx
xorl %edx, %edx
cmpl $-0x1, (%rcx,%rdx,4)
leaq 0x1(%rdx), %rdx
jne 0x4555
jmp 0x4563
xorl %edx, %edx
cmpl $0x2, %edx
jl 0x4598
addq %rbx, %rax
movl 0xb6ef(%rip), %ecx      # 0xfc60
movl %edx, %edx
incq %rdx
movl -0x8(%rax,%rdx,4), %esi
cmpl $0x6d, %esi
je 0x458f
movslq %ecx, %rdi
incl %ecx
movl %esi, 0x4(%r14,%rdi,4)
movl %ecx, 0xb6d1(%rip)      # 0xfc60
decq %rdx
cmpq $0x2, %rdx
jg 0x4576
callq 0x38e7
movb $0x1, %al
testb %al, %al
jne 0x42ea
movslq 0xb6b2(%rip), %rax      # 0xfc60
cmpl $0x3, (%r14,%rax,4)
jne 0x45c5
movl $0xffffffff, (%r14,%rax,4) # imm = 0xFFFFFFFF
decl %eax
movl %eax, 0xb69b(%rip)      # 0xfc60
movslq 0xb694(%rip), %rax      # 0xfc60
cmpl $0x0, (%r14,%rax,4)
jne 0x45ef
movl $0xffffffff, (%r14,%rax,4) # imm = 0xFFFFFFFF
decl %eax
movl %eax, 0xb67d(%rip)      # 0xfc60
leaq 0x624a(%rip), %rdi      # 0xa834
callq 0x2070
movl -0x2c(%rbp), %esi
incl %esi
leaq 0x500e(%rip), %rdi      # 0x9609
xorl %eax, %eax
callq 0x20b0
movq -0x40(%rbp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
leaq 0x8456(%rip), %rax      # 0xca80
movq (%rax,%r12,8), %r8
leaq 0x4f75(%rip), %rdi      # 0x95aa
xorl %eax, %eax
callq 0x20b0
xorl %edi, %edi
callq 0x21c0