// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl $0x40, %edi
callq 0x2130
movq %rax, -0x10(%rbp)
callq 0x22c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
leaq 0x66cb(%rip), %rsi      # 0xc405
callq 0x2050
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
leaq 0x6612(%rip), %rsi      # 0xc366
callq 0x2050
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movl $0x1, 0x3c(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x3c(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x8(%rbp), %rdi
leaq 0x7dce(%rip), %rsi      # 0xdb94
callq 0x2150
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x20a0
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2140
movq -0x18(%rbp), %rdi
callq 0x2590
movq %rax, -0x18(%rbp)
xorl %edi, %edi
callq 0x4d90
movl $0x1, %edi
callq 0x4d90
callq 0x2750
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x3c(%rax), %ecx
leaq 0x7a7c(%rip), %rdi      # 0xd8bb
movb $0x0, %al
callq 0x2090
movq -0x20(%rbp), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
leaq 0x7a5d(%rip), %rsi      # 0xd8b5
callq 0x20f0
cmpl $0x0, %eax
je 0x6175
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
leaq 0x6823(%rip), %rdi      # 0xc69f
movb $0x0, %al
callq 0x2090
movq -0x30(%rbp), %rdi
leaq 0x6826(%rip), %rsi      # 0xc6b4
callq 0x20f0
cmpl $0x0, %eax
jne 0x5edb
callq 0x2750
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movslq 0xcd8d(%rip), %rcx      # 0x12c40
leaq 0x17d26(%rip), %rax     # 0x1dbe0
cmpl $0x0, (%rax,%rcx,4)
je 0x5ed4
movslq 0xcd79(%rip), %rcx      # 0x12c40
leaq 0x17d12(%rip), %rax     # 0x1dbe0
cmpl $0x3, (%rax,%rcx,4)
jne 0x5ed9
jmp 0x6175
jmp 0x5edb
movslq 0xcd5e(%rip), %rcx      # 0x12c40
leaq 0x17cf7(%rip), %rax     # 0x1dbe0
cmpl $0x33, (%rax,%rcx,4)
jle 0x5f95
movslq 0xcd46(%rip), %rcx      # 0x12c40
leaq 0x17cdf(%rip), %rax     # 0x1dbe0
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4390
movl %eax, %ecx
movl -0x4c(%rbp), %eax
cmpl %ecx, %eax
jne 0x5f42
callq 0x4e00
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
callq 0x2750
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
callq 0x4e50
jmp 0x5f90
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x3c(%rax), %ecx
movslq 0xccdb(%rip), %rdi      # 0x12c40
leaq 0x17c74(%rip), %rax     # 0x1dbe0
movslq (%rax,%rdi,4), %rdi
leaq 0xccd9(%rip), %rax      # 0x12c50
movq (%rax,%rdi,8), %r8
leaq 0x673e(%rip), %rdi      # 0xc6c0
movb $0x0, %al
callq 0x2090
xorl %edi, %edi
callq 0x21a0
jmp 0x6170
movslq 0xcca4(%rip), %rcx      # 0x12c40
leaq 0x17c3d(%rip), %rax     # 0x1dbe0
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x17cfd(%rip), %rax     # 0x1dcb0
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4390
movl %eax, %ecx
movq -0x58(%rbp), %rax
subl $0x33, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0x60a8
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x3c(%rax), %ecx
movslq 0xcc37(%rip), %rdi      # 0x12c40
leaq 0x17bd0(%rip), %rax     # 0x1dbe0
movslq (%rax,%rdi,4), %rdi
leaq 0xcc35(%rip), %rax      # 0x12c50
movq (%rax,%rdi,8), %r8
leaq 0x669a(%rip), %rdi      # 0xc6c0
movb $0x0, %al
callq 0x2090
callq 0x2750
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movslq 0xcbf8(%rip), %rcx      # 0x12c40
leaq 0x17b91(%rip), %rax     # 0x1dbe0
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x17c51(%rip), %rax     # 0x1dcb0
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4390
movl %eax, %ecx
movq -0x60(%rbp), %rax
subl $0x33, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
jne 0x60a3
callq 0x2750
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x6041
jmp 0x616e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x60cc
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x20(%rbp), %rcx
leaq 0xb6eb(%rip), %rdx      # 0x117b0
callq 0x49d0
jmp 0x6101
movq -0x10(%rbp), %rdi
movslq 0xcb69(%rip), %rcx      # 0x12c40
leaq 0x17b02(%rip), %rax     # 0x1dbe0
movl (%rax,%rcx,4), %esi
callq 0x4910
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x20(%rbp), %rcx
leaq 0xb6b4(%rip), %rdx      # 0x117b0
callq 0x49d0
callq 0x4e00
movl -0x34(%rbp), %eax
subl $0x1, %eax
cltq
leaq 0xb69b(%rip), %rdi      # 0x117b0
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x42f0
subl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jle 0x6169
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0xb673(%rip), %rax      # 0x117b0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
cmpl $0x6d, -0x48(%rbp)
je 0x615c
movl -0x48(%rbp), %edi
callq 0x4d90
jmp 0x615e
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6127
callq 0x4e50
jmp 0x6170
jmp 0x5e46
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movslq 0xcabb(%rip), %rcx      # 0x12c40
leaq 0x17a54(%rip), %rax     # 0x1dbe0
cmpl $0x3, (%rax,%rcx,4)
jne 0x6197
callq 0x4e00
movslq 0xcaa2(%rip), %rcx      # 0x12c40
leaq 0x17a3b(%rip), %rax     # 0x1dbe0
cmpl $0x0, (%rax,%rcx,4)
jne 0x61be
callq 0x4e00
leaq 0x6568(%rip), %rdi      # 0xc71f
movb $0x0, %al
callq 0x2090
movl -0x28(%rbp), %esi
leaq 0x6579(%rip), %rdi      # 0xc741
movb $0x0, %al
callq 0x2090
movq -0x10(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)