// File: /workspace/github_c_makefile_star2_O1/gregtour[P]minimal-compiler/parser.c
// Function: ParseSource
SYNTAX_TREE* ParseSource(L_TOKEN*      input,
                         LR_TABLE      parser,
                         GRAMMAR_TABLE grammar)
{
    // the program's syntax tree
    SYNTAX_TREE* ast = NULL;

    // set ip and init stack
    L_TOKEN* ip = input;
    StackPushState(0);
    // printf("Parsing source...\n");

    // loop forever
    for (;;)
    {
        ACTION action;
        // get the state off the top of the stack
        PARSE_STACK s = StackPeek();
        if (s.token || s.state == -1
                || s.state >= parser.numStates
                || ip == NULL)
        {
            if (ip)
                printf("Parse error: invalid state on top of the stack.\n");
            else
                printf("Parse error: unexpected end of input.\n");
            return 0;
        }
        
        // find the action table entry for the state and input
        action = ActionTable(parser, s.state, ip->token);

        // perform the parse action
        if (action.type == ACTION_SHIFT)
        {
            // push a then s' on top of the stack;
            StackPushToken(ip);
            StackPushState(action.value);
            // advance ip to the next input symbol
            ip = ip->next;
        }
        else if (action.type == ACTION_REDUCE)
        {
            SYNTAX_TREE* node;
            RULE r;
            int rhs;
            
            // output the production A -> B
            if (action.value < 0 || action.value >= grammar.numRules) {
                printf("Parse error: invalid production for reduce action.\n");
                PrintErrorInput(ip, grammar);
                return 0;
            }
            r = grammar.rules[action.value];
            //printf("%s\n", GetElement(r.lhs, grammar));

            // pop 2*|B| symbols off the stack;
            node = (SYNTAX_TREE*)malloc(sizeof(SYNTAX_TREE));
            node->token = r.lhs;
            node->production = action.value;
            node->string = NULL;
            node->length = 0;
            node->line = 0;
            node->children = (SYNTAX_TREE**)malloc(r.rhsLength * sizeof(SYNTAX_TREE*));
            node->numChildren = r.rhsLength;
            for (rhs = 0; rhs < r.rhsLength; rhs++)
            {
                PARSE_STACK symbol;
                int child = r.rhsLength - rhs - 1;

                StackPop(); // state
                symbol = StackPop();
                if (symbol.token == NULL)
                {
                    printf("Parse error: expected token.\n");
                    PrintErrorInput(ip, grammar);
                    return 0;
                }
                
                if (symbol.type == TOKEN_L_TOKEN)
                {
                    L_TOKEN* token = (L_TOKEN*)symbol.token;
                    node->children[child] = (SYNTAX_TREE*)malloc(sizeof(SYNTAX_TREE));
                    node->children[child]->token = token->token;
                    node->children[child]->production = 0;
                    node->children[child]->string = token->string;
                    node->children[child]->length = token->length;
                    node->children[child]->line = token->line;
                    node->children[child]->children = NULL;
                    node->children[child]->numChildren = 0;
                }
                else if (symbol.type == TOKEN_SYNTAX_TREE)
                {
                    node->children[child] = (SYNTAX_TREE*)symbol.token;
                }
                else
                {
                    printf("Parse error: expected token object.\n");
                    PrintErrorInput(ip, grammar);
                }
            }
            if (r.rhsLength) node->line = node->children[0]->line;
                
            // let s' be the state now on top of the stack;
            s = StackPeek();
            if (s.token || s.state == -1 || s.state >= parser.numStates)
            {
                printf("Parse error: expected state on top of stack.\n");
                PrintErrorInput(ip, grammar);

                FreeParseStack();

                return 0;
            }
            
            // push A then goto[s', A] on top of the stack;
            if (r.lhs == gSymbolGoal)
            {
                ast = node;
            }
            StackPushTree(node);
            StackPushState(GotoTable(parser, s.state, r.lhs));
        }
        else if (action.type == ACTION_ACCEPT)
        {
            // accept
            // printf("Parse accepted!\n");
            break;
        }
        else
        {
            // error
            printf("Parse error: illegal action type.\n");
            PrintErrorInput(ip, grammar);

            FreeParseStack();

            return 0;
        }
    }
    
    // free the stack
    while (gParseStack)
    {
        StackPop();
    }

    // no errors
    return ast;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r14
leaq 0x30(%rbp), %rbx
leaq 0x10(%rbp), %r15
xorl %edi, %edi
callq 0x4df8
xorl %r12d, %r12d
movq 0x13fc9(%rip), %rcx     # 0x19350
testq %rcx, %rcx
je 0x5398
movl 0xc(%rcx), %eax
cmpq $0x0, (%rcx)
setne %cl
jmp 0x539f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %dl
orb %cl, %dl
jne 0x53c0
movl 0x8(%r15), %r13d
cmpl %r13d, %eax
setge %cl
testq %r14, %r14
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x53e8
testq %r14, %r14
je 0x53ce
leaq 0x3370(%rip), %rdi      # 0x873c
jmp 0x53d5
leaq 0x3341(%rip), %rdi      # 0x8716
callq 0x2050
movl $0x1, %eax
testl %eax, %eax
je 0x5380
jmp 0x5704
testl %eax, %eax
js 0x5411
xorl %ecx, %ecx
cmpl %eax, %r13d
jle 0x5413
movslq (%r14), %rsi
andq $-0x2001, %rsi          # imm = 0xDFFF
movl $0x0, %edx
testl %esi, %esi
jle 0x5415
movl (%r15), %ecx
cmpl %ecx, %esi
jle 0x560e
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x3, %ecx
je 0x55d0
movq %rdx, %r15
shrq $0x20, %r15
cmpl $0x2, %ecx
je 0x544e
cmpl $0x1, %ecx
jne 0x55da
movq %r14, %rdi
callq 0x4e64
movl %r15d, %edi
callq 0x4df8
movq 0x18(%r14), %r14
xorl %eax, %eax
jmp 0x565e
testq %rdx, %rdx
js 0x562c
cmpl %r15d, 0x20(%rbx)
jle 0x562c
movq %r12, -0x38(%rbp)
movq 0x28(%rbx), %rax
movq %r15, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edx
movl %edx, -0x2c(%rbp)
movslq 0x4(%rax,%rcx), %r12
movl $0x30, %edi
callq 0x2100
movq %rax, %rbx
movl -0x2c(%rbp), %eax
movl %eax, (%rbx)
movl %r15d, 0x4(%rbx)
leaq (,%r12,8), %rdi
callq 0x2100
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq %rax, 0x28(%rbx)
movl %r12d, 0x20(%rbx)
movq %r12, %r15
movq %r12, -0x40(%rbp)
testq %r12, %r12
jle 0x5599
leaq -0x60(%rbp), %r12
movq %r12, %rdi
callq 0x4f89
movq %r12, %rdi
callq 0x4f89
movq -0x60(%rbp), %r12
testq %r12, %r12
je 0x5542
movl -0x58(%rbp), %eax
cmpl $0x2, %eax
je 0x554b
cmpl $0x1, %eax
jne 0x5556
movl $0x30, %edi
callq 0x2100
movq 0x28(%rbx), %rcx
movq %rax, -0x8(%rcx,%r15,8)
movl (%r12), %eax
movq 0x28(%rbx), %rcx
movq -0x8(%rcx,%r15,8), %rdx
movl %eax, (%rdx)
xorl %esi, %esi
movl %esi, 0x4(%rdx)
movq 0x8(%r12), %rax
movq %rax, 0x8(%rdx)
movl 0x10(%r12), %eax
movq -0x8(%rcx,%r15,8), %rcx
movl %eax, 0x10(%rcx)
movl 0x14(%r12), %eax
movl %eax, 0x14(%rcx)
movq $0x0, 0x28(%rcx)
movq 0x28(%rbx), %rax
movq -0x8(%rax,%r15,8), %rax
movl %esi, 0x20(%rax)
jmp 0x5587
leaq 0x317d(%rip), %rdi      # 0x86c6
jmp 0x555d
movq 0x28(%rbx), %rax
movq %r12, -0x8(%rax,%r15,8)
jmp 0x5587
leaq 0x3145(%rip), %rdi      # 0x86a2
callq 0x2050
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x50a3
testq %r12, %r12
je 0x5667
decq %r15
jne 0x54bd
cmpl $0x0, -0x40(%rbp)
je 0x55ac
movq 0x28(%rbx), %rax
movq (%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0x14(%rbx)
movq 0x13d9d(%rip), %rax     # 0x19350
testq %rax, %rax
movq -0x38(%rbp), %r12
je 0x5676
movl 0xc(%rax), %r15d
cmpq $0x0, (%rax)
sete %al
jmp 0x567e
movl $0x2, %eax
jmp 0x53df
leaq 0x3072(%rip), %rdi      # 0x8653
callq 0x2050
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x50a3
callq 0x5016
jmp 0x5659
movq 0x10(%r15), %rdx
imull %eax, %ecx
movslq %ecx, %rax
addq %rsi, %rax
movl -0x8(%rdx,%rax,8), %ecx
movl -0x4(%rdx,%rax,8), %edx
shlq $0x20, %rdx
jmp 0x5415
leaq 0x30b0(%rip), %rdi      # 0x86e3
callq 0x2050
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x50a3
movl $0x1, %eax
leaq 0x10(%rbp), %r15
jmp 0x53df
movl $0x1, %eax
movq -0x38(%rbp), %r12
leaq 0x30(%rbp), %rbx
jmp 0x565e
movb $0x1, %al
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb %al, %al
je 0x56e1
cmpl $-0x1, %r15d
je 0x56e1
cmpl %r13d, %r15d
jge 0x56e1
leaq 0x6a1c(%rip), %rax      # 0xc0b0
movl -0x2c(%rbp), %r13d
cmpl (%rax), %r13d
cmoveq %rbx, %r12
movq %rbx, %rdi
callq 0x4ed7
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
testl %r15d, %r15d
js 0x56f6
andl $0xffffefff, %r13d      # imm = 0xFFFFEFFF
jle 0x56f6
leaq 0x10(%rbp), %rax
movl 0x4(%rax), %eax
cmpl %eax, %r13d
leaq 0x30(%rbp), %rbx
jg 0x56fa
leaq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
imull %r15d, %eax
addl %r13d, %eax
cltq
movl -0x4(%rcx,%rax,4), %edi
jmp 0x56fa
leaq 0x2f8d(%rip), %rdi      # 0x8675
callq 0x2050
leaq 0x30(%rbp), %rbx
jmp 0x55e6
leaq 0x30(%rbp), %rbx
callq 0x4df8
jmp 0x5447
cmpl $0x2, %eax
jne 0x572b
cmpq $0x0, 0x13c37(%rip)     # 0x19348
je 0x572e
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f89
cmpq $0x0, 0x13c21(%rip)     # 0x19348
jne 0x5717
jmp 0x572e
xorl %r12d, %r12d
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq