// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movl $0x40, %edi
callq 0x2150
movq %rax, %r15
movl $0x8, %edi
callq 0x2150
movq %rax, %r14
movl $0x40, %edi
callq 0x2150
movq %rax, (%r14)
movq %r14, (%r15)
movabsq $0x4d4152474f5250, %rcx # imm = 0x4D4152474F5250
movq %rcx, (%rax)
movabsq $0x6d6172676f7270, %rcx # imm = 0x6D6172676F7270
movq %rcx, 0x1e(%rax)
movl $0x1, %r14d
movl %r14d, 0x3c(%rax)
movl %r14d, 0x8(%r15)
leaq 0x18(%r15), %rax
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x28(%r15)
movq %r15, -0x38(%rbp)
movq $0x0, 0x38(%r15)
leaq 0x655c(%rip), %rsi      # 0xa6f6
movq %rbx, %rdi
callq 0x2170
movq %rax, %rbx
movq %rax, %rdi
callq 0x20c0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %rbx, %rdi
callq 0x243e
movslq 0xba98(%rip), %rax      # 0xfc60
leaq 0x166c1(%rip), %rcx     # 0x1a890
movl $0x0, 0x4(%rcx,%rax,4)
movl %r14d, 0x8(%rcx,%rax,4)
addq $0x2, %rax
movl %eax, 0xba7a(%rip)      # 0xfc60
callq 0x2558
movq %rax, %rbx
movq (%rax), %r15
leaq 0x1e(%r15), %rdx
movl 0x3c(%r15), %ecx
leaq 0x62ef(%rip), %rdi      # 0xa4ef
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
movl $0x0, -0x2c(%rbp)
cmpb $0x24, 0x1e(%r15)
jne 0x4223
cmpb $0x0, 0x1f(%r15)
je 0x44c3
leaq 0x535f(%rip), %rdi      # 0x9589
movq %r15, %rsi
xorl %eax, %eax
callq 0x20b0
movq %r15, %rdi
leaq 0x5360(%rip), %rsi      # 0x959e
callq 0x2120
testl %eax, %eax
je 0x425d
movl 0xba13(%rip), %edx      # 0xfc60
movslq %edx, %rsi
leaq 0x16639(%rip), %rax     # 0x1a890
movl (%rax,%rsi,4), %r12d
jmp 0x428f
callq 0x2558
movq %rax, %rbx
movl 0xb9f5(%rip), %edx      # 0xfc60
movslq %edx, %rsi
leaq 0x1661b(%rip), %rax     # 0x1a890
movl (%rax,%rsi,4), %r12d
testl %r12d, %r12d
je 0x4501
cmpl $0x3, %r12d
je 0x44db
movq (%rbx), %r15
cmpl $0x34, %r12d
jl 0x4306
movq %rsi, -0x50(%rbp)
movl %edx, -0x40(%rbp)
leaq 0x87dd(%rip), %r14      # 0xca80
xorl %r13d, %r13d
movq (%r14), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x42c8
incq %r13
addq $0x8, %r14
cmpq $0x6e, %r13
jne 0x42a6
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %r13d, %r12d
jne 0x4549
leaq 0x165b8(%rip), %rax     # 0x1a890
movq -0x50(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x40(%rbp), %eax
decl %eax
movl %eax, 0xb972(%rip)      # 0xfc60
incl -0x2c(%rbp)
callq 0x2558
movq %rax, %rbx
callq 0x3877
movq (%rbx), %r15
jmp 0x4211
leal -0x1(%r12), %eax
cltq
movq %rax, -0x40(%rbp)
movq $-0x33, %r14
leaq 0x8761(%rip), %r13      # 0xca80
movq (%r13), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x4343
incq %r14
addq $0x8, %r13
cmpq $0x3b, %r14
jne 0x431f
movq $-0x34, %r14
imulq $0xe8, -0x40(%rbp), %rax
leaq 0x1660e(%rip), %rcx     # 0x1a960
addq %rcx, %rax
movslq (%rax,%r14,4), %r15
cmpq $-0x1, %r15
je 0x4388
movq -0x48(%rbp), %rax
cmpq $0x0, (%rax)
leaq 0xa462(%rip), %r14      # 0xe7d0
je 0x441d
movq -0x38(%rbp), %rdi
movl %r12d, %esi
callq 0x34d1
movq %rax, %rdi
jmp 0x4421
movq (%rbx), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
movslq %r12d, %rax
leaq 0x86e4(%rip), %rdi      # 0xca80
movq (%rdi,%rax,8), %r8
leaq 0x5203(%rip), %rdi      # 0x95aa
xorl %eax, %eax
callq 0x20b0
callq 0x2558
movq %rax, %rbx
movq (%rax), %r15
movslq 0xb8a0(%rip), %rax      # 0xfc60
leaq 0x164c9(%rip), %rcx     # 0x1a890
movslq (%rcx,%rax,4), %r14
decq %r14
movq $-0x33, %r12
leaq 0x86a4(%rip), %r13      # 0xca80
movq (%r13), %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
je 0x4400
incq %r12
addq $0x8, %r13
cmpq $0x3b, %r12
jne 0x43dc
movq $-0x34, %r12
imulq $0xe8, %r14, %rax
leaq 0x16552(%rip), %rcx     # 0x1a960
addq %rcx, %rax
cmpl $-0x1, (%rax,%r12,4)
je 0x43ae
jmp 0x42fe
movq -0x38(%rbp), %rdi
movl %r15d, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3521
movslq 0xb82a(%rip), %rax      # 0xfc60
leaq 0x16453(%rip), %rcx     # 0x1a890
movl $0xffffffff, (%rcx,%rax,4) # imm = 0xFFFFFFFF
decl %eax
movl %eax, 0xb814(%rip)      # 0xfc60
decq %r15
imulq $0x38, %r15, %rdx
cmpl $-0x1, (%rdx,%r14)
je 0x42f9
leaq (%r14,%rdx), %rcx
leaq (%rdx,%r14), %rsi
addq $0x4, %rsi
movl $0x1, %edi
xorl %r8d, %r8d
movl %edi, %edx
cmpl $-0x1, (%rsi,%r8,4)
leaq 0x1(%r8), %r8
leal 0x1(%rdx), %edi
jne 0x4472
cmpl $0x2, %r8d
jl 0x42f9
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
incq %rdx
movl -0x8(%rcx,%rdx,4), %esi
cmpl $0x6d, %esi
je 0x44b5
movslq %eax, %rdi
incl %eax
leaq 0x163e6(%rip), %r8      # 0x1a890
movl %esi, 0x4(%r8,%rdi,4)
movl %eax, 0xb7ab(%rip)      # 0xfc60
decq %rdx
cmpq $0x2, %rdx
ja 0x4495
jmp 0x42f9
movl 0xb797(%rip), %edx      # 0xfc60
movslq %edx, %rsi
leaq 0x163bd(%rip), %rax     # 0x1a890
movl (%rax,%rsi,4), %eax
cmpl $0x3, %eax
jne 0x44fd
leaq 0x163ae(%rip), %rcx     # 0x1a890
movl $0xffffffff, (%rcx,%rsi,4) # imm = 0xFFFFFFFF
movslq %edx, %rax
leaq -0x1(%rax), %rsi
leal -0x1(%rax), %edx
movl %edx, 0xb767(%rip)      # 0xfc60
movl -0x4(%rcx,%rax,4), %eax
testl %eax, %eax
jne 0x4523
leaq 0x16388(%rip), %rax     # 0x1a890
movl $0xffffffff, (%rax,%rsi,4) # imm = 0xFFFFFFFF
decl %edx
movl %edx, 0xb749(%rip)      # 0xfc60
leaq 0x6316(%rip), %rdi      # 0xa834
callq 0x2070
movl -0x2c(%rbp), %esi
incl %esi
leaq 0x50da(%rip), %rdi      # 0x9609
xorl %eax, %eax
callq 0x20b0
movq -0x38(%rbp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
leaq 0x1e(%rsi), %rdx
movl 0x3c(%rsi), %ecx
movl %r12d, %eax
leaq 0x8523(%rip), %rdi      # 0xca80
movq (%rdi,%rax,8), %r8
leaq 0x5042(%rip), %rdi      # 0x95aa
xorl %eax, %eax
callq 0x20b0
xorl %edi, %edi
callq 0x21c0