// File: /workspace/github_c_makefile_star2_O0/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
subq $0x160, %rsp            # imm = 0x160
leaq 0x30(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
xorl %edi, %edi
callq 0x7300
leaq -0x48(%rbp), %rdi
callq 0x7540
cmpq $0x0, -0x48(%rbp)
jne 0x7b38
cmpl $-0x1, -0x3c(%rbp)
je 0x7b38
movq -0x128(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x7b38
cmpq $0x0, -0x20(%rbp)
jne 0x7b6a
cmpq $0x0, -0x20(%rbp)
je 0x7b4f
leaq 0x3b02(%rip), %rdi      # 0xb648
movb $0x0, %al
callq 0x2070
jmp 0x7b5d
leaq 0x3b23(%rip), %rdi      # 0xb679
movb $0x0, %al
callq 0x2070
movq $0x0, -0x8(%rbp)
jmp 0x8081
movq -0x128(%rbp), %rax
movl -0x3c(%rbp), %edi
movq -0x20(%rbp), %rcx
movl (%rcx), %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x7200
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x1, -0x28(%rbp)
jne 0x7bd5
movq -0x20(%rbp), %rdi
callq 0x73c0
movl -0x24(%rbp), %edi
callq 0x7300
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x805a
cmpl $0x2, -0x28(%rbp)
jne 0x800a
cmpl $0x0, -0x24(%rbp)
jl 0x7bf4
movq -0x130(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x20(%rcx), %eax
jl 0x7c38
leaq 0x3aa5(%rip), %rdi      # 0xb6a0
movb $0x0, %al
callq 0x2070
movq -0x130(%rbp), %rax
movq -0x20(%rbp), %rdi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7780
movq $0x0, -0x8(%rbp)
jmp 0x8081
movq -0x130(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x68(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movl $0x30, %edi
callq 0x20e0
movq %rax, -0x58(%rbp)
movl -0x68(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x14(%rax)
movslq -0x64(%rbp), %rdi
shlq $0x3, %rdi
callq 0x20e0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x28(%rax)
movl -0x64(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x7ef6
movl -0x64(%rbp), %eax
subl -0x6c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x94(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x75b0
leaq -0xd8(%rbp), %rdi
callq 0x75b0
movq -0xd8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x7d7c
leaq 0x3995(%rip), %rdi      # 0xb6d4
movb $0x0, %al
callq 0x2070
movq -0x130(%rbp), %rax
movq -0x20(%rbp), %rdi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7780
movq $0x0, -0x8(%rbp)
jmp 0x8081
cmpl $0x1, -0x88(%rbp)
jne 0x7e88
movq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0x30, %edi
callq 0x20e0
movq %rax, %rdx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xe0(%rbp), %rax
movl (%rax), %ecx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x0, 0x4(%rax)
movq -0xe0(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x8(%rax)
movq -0xe0(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x10(%rax)
movq -0xe0(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x14(%rax)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x28(%rax)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x0, 0x20(%rax)
jmp 0x7ee6
cmpl $0x2, -0x88(%rbp)
jne 0x7ead
movq -0x90(%rbp), %rdx
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x94(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x7ee4
leaq 0x383e(%rip), %rdi      # 0xb6f2
movb $0x0, %al
callq 0x2070
movq -0x130(%rbp), %rax
movq -0x20(%rbp), %rdi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7780
jmp 0x7ee6
jmp 0x7ee8
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x7cc9
cmpl $0x0, -0x64(%rbp)
je 0x7f11
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl 0x14(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x14(%rax)
leaq -0x100(%rbp), %rdi
callq 0x7540
movq -0x100(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x7f65
cmpl $-0x1, -0x3c(%rbp)
je 0x7f65
movq -0x128(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jl 0x7fae
leaq 0x37ab(%rip), %rdi      # 0xb717
movb $0x0, %al
callq 0x2070
movq -0x130(%rbp), %rax
movq -0x20(%rbp), %rdi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7780
callq 0x7690
movq $0x0, -0x8(%rbp)
jmp 0x8081
movl -0x68(%rbp), %eax
leaq 0x70e8(%rip), %rcx      # 0xf0a0
cmpl (%rcx), %eax
jne 0x7fc4
movq -0x58(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x58(%rbp), %rdi
callq 0x7480
movq -0x128(%rbp), %rax
movl -0x3c(%rbp), %edi
movl -0x68(%rbp), %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x7280
movl %eax, %edi
callq 0x7300
jmp 0x8058
cmpl $0x3, -0x28(%rbp)
jne 0x8012
jmp 0x805f
leaq 0x372c(%rip), %rdi      # 0xb745
movb $0x0, %al
callq 0x2070
movq -0x130(%rbp), %rax
movq -0x20(%rbp), %rdi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7780
callq 0x7690
movq $0x0, -0x8(%rbp)
jmp 0x8081
jmp 0x805a
jmp 0x7b0c
jmp 0x8061
cmpq $0x0, 0x142cf(%rip)     # 0x1c338
je 0x8079
leaq -0x120(%rbp), %rdi
callq 0x75b0
jmp 0x8061
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nop