// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, %rbx
pushq $0x40
popq %rdi
callq 0x2150
movq %rax, %r15
callq 0x22fc
movq %rax, (%r15)
movq (%rax), %rcx
movabsq $0x4d4152474f5250, %rdx # imm = 0x4D4152474F5250
movq %rdx, (%rcx)
movq (%rax), %rcx
movabsq $0x6d6172676f7270, %rdx # imm = 0x6D6172676F7270
movq %rdx, 0x1e(%rcx)
movq (%rax), %rax
pushq $0x1
popq %r14
movl %r14d, 0x3c(%rax)
movl %r14d, 0x8(%r15)
leaq 0x18(%r15), %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x28(%r15)
movq %r15, -0x38(%rbp)
andq $0x0, 0x38(%r15)
leaq 0x57f1(%rip), %rsi      # 0x96f6
movq %rbx, %rdi
callq 0x2180
movq %rax, %rbx
movq %rax, %rdi
callq 0x20c0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq %rbx, %rdi
callq 0x2451
movslq 0xad2d(%rip), %rax      # 0xec60
leaq 0x15956(%rip), %r15     # 0x19890
andl $0x0, 0x4(%r15,%rax,4)
movl %r14d, 0x8(%r15,%rax,4)
addq $0x2, %rax
movl %eax, 0xad11(%rip)      # 0xec60
callq 0x2559
movq %rax, %r13
movq (%rax), %rbx
leaq 0x1e(%rbx), %rdx
movl 0x3c(%rbx), %ecx
leaq 0x5587(%rip), %rdi      # 0x94ef
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20b0
movl $0x0, -0x2c(%rbp)
cmpb $0x24, 0x1e(%rbx)
jne 0x3f89
cmpb $0x0, 0x1f(%rbx)
je 0x4153
leaq 0x45f9(%rip), %rdi      # 0x8589
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20b0
movq %rbx, %rdi
leaq 0x45fa(%rip), %rsi      # 0x859e
callq 0x2120
testl %eax, %eax
je 0x3fbd
movl 0xacac(%rip), %r14d     # 0xec60
movslq %r14d, %r12
movl (%r15,%r12,4), %r15d
jmp 0x3fe9
callq 0x2559
movl 0xac97(%rip), %r14d     # 0xec60
movslq %r14d, %r12
movl (%r15,%r12,4), %r15d
testl %r15d, %r15d
je 0x4168
cmpl $0x3, %r15d
je 0x4171
movq %rax, %r13
movq (%rax), %rbx
cmpl $0x34, %r15d
jl 0x402f
movq %rbx, %rdi
callq 0x2f9b
cmpl %eax, %r15d
jne 0x41d9
leaq 0x15889(%rip), %r15     # 0x19890
orl $-0x1, (%r15,%r12,4)
decl %r14d
movl %r14d, 0xac4a(%rip)     # 0xec60
incl -0x2c(%rbp)
callq 0x2559
movq %rax, %r13
callq 0x3665
movq (%r13), %rbx
jmp 0x3f79
leal -0x1(%r15), %eax
movslq %eax, %r14
movq %rbx, %rdi
callq 0x2f9b
cltq
imulq $0xe8, %r14, %rcx
leaq 0x15912(%rip), %r14     # 0x19960
addq %r14, %rcx
movslq -0xcc(%rcx,%rax,4), %rbx
cmpq $-0x1, %rbx
je 0x4081
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
leaq 0x9762(%rip), %r12      # 0xd7d0
je 0x40e5
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x32f7
movq %rax, %rdi
jmp 0x40e9
movq (%r13), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
movslq %r15d, %rax
leaq 0x79ea(%rip), %rdi      # 0xba80
movq (%rdi,%rax,8), %r8
leaq 0x4509(%rip), %rdi      # 0x85aa
xorl %eax, %eax
callq 0x20b0
leaq 0x157e1(%rip), %r15     # 0x19890
callq 0x2559
movq %rax, %r13
movq (%rax), %rdi
movslq 0xab9f(%rip), %rax      # 0xec60
movslq (%r15,%rax,4), %rax
imulq $0xe8, %rax, %rbx
callq 0x2f9b
cltq
addq %r14, %rbx
cmpl $-0x1, -0x1b4(%rbx,%rax,4)
je 0x40af
jmp 0x4026
movq -0x38(%rbp), %rdi
movl %ebx, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0x3344
movslq 0xab63(%rip), %rax      # 0xec60
leaq 0x1578c(%rip), %r15     # 0x19890
orl $-0x1, (%r15,%rax,4)
leal -0x1(%rax), %r14d
movl %r14d, 0xab4c(%rip)     # 0xec60
imulq $0x38, %rbx, %rax
leaq (%rax,%r12), %rbx
addq $-0x38, %rbx
movq %rbx, %rdi
callq 0x2f67
movl %eax, %eax
cmpl $0x2, %eax
jl 0x4021
movl -0x4(%rbx,%rax,4), %ecx
decq %rax
cmpl $0x6d, %ecx
je 0x412a
movslq %r14d, %rdx
incl %r14d
movl %ecx, 0x4(%r15,%rdx,4)
movl %r14d, 0xab0f(%rip)     # 0xec60
jmp 0x412a
movl 0xab06(%rip), %r14d     # 0xec60
movslq %r14d, %r12
movl (%r15,%r12,4), %eax
cmpl $0x3, %eax
je 0x4178
jmp 0x4194
leaq 0x15721(%rip), %r15     # 0x19890
jmp 0x4198
leaq 0x15718(%rip), %r15     # 0x19890
orl $-0x1, (%r15,%r12,4)
movslq %r14d, %rax
leaq -0x1(%rax), %r12
leal -0x1(%rax), %r14d
movl %r14d, 0xaad1(%rip)     # 0xec60
movl -0x4(%r15,%rax,4), %eax
testl %eax, %eax
jne 0x41b3
orl $-0x1, (%r15,%r12,4)
decl %r14d
movl %r14d, 0xaab9(%rip)     # 0xec60
leaq 0x5686(%rip), %rdi      # 0x9834
callq 0x2070
movl -0x2c(%rbp), %esi
incl %esi
leaq 0x444a(%rip), %rdi      # 0x8609
xorl %eax, %eax
callq 0x20b0
movq -0x38(%rbp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
movl %r15d, %eax
leaq 0x7892(%rip), %rdi      # 0xba80
movq (%rdi,%rax,8), %r8
leaq 0x43b1(%rip), %rdi      # 0x85aa
xorl %eax, %eax
callq 0x20b0
xorl %edi, %edi
callq 0x21d0