static struct exp *parseOrExp() /* Parse lowest level of precedent expressions - or and xor. */ { struct exp *left; struct exp *right, *exp; struct kxTok *tok; enum kxTokType type; if ((left = parseAndExp()) == NULL) return NULL; if ((tok = token) == NULL) return left; type = token->type; if (type == kxtOr || type == kxtXor) { advanceToken(); right = nextExp(); if (right == NULL) errAbort("Expecting expression on the other side of %s", tok->string); AllocVar(exp); exp->left = left; exp->right = right; if (type == kxtOr) exp->type = kxOr; else exp->type = kxXor; return exp; } else return left; }
static struct exp *parseAndExp() /* Parse and level expressions. */ { struct exp *left; struct exp *right, *exp; struct kxTok *tok; enum kxTokType type; if ((left = parseNot()) == NULL) return NULL; if ((tok = token) == NULL) return left; type = token->type; if (type == kxtAnd) { advanceToken(); right = nextExp(); if (right == NULL) errAbort("Expecting expression on the other side of %s", tok->string); AllocVar(exp); exp->left = left; exp->right = right; exp->type = kxAnd; return exp; } else return left; }
static struct exp *parseExp(struct kxTok *tokList) /* Convert key expression from token stream to parse tree. */ { struct exp *exp; token = tokList; exp = nextExp(); if (token->type != kxtEnd) { errAbort("Extra tokens past end of expression. Missing &?"); } return exp; }
static struct exp *parseNot() /* Parse not */ { struct exp *exp; struct exp *right; if (token == NULL) return NULL; if (token->type == kxtNot) { advanceToken(); right = nextExp(); AllocVar(exp); exp->right = right; exp->type = kxNot; return exp; } else return parseParenthesized(); }
static struct exp *parseParenthesized() /* Parse parenthesized expressions. */ { struct exp *exp; if (token == NULL) return NULL; if (token->type == kxtOpenParen) { advanceToken(); exp = nextExp(); if (token->type != kxtCloseParen) errAbort("Unmatched parenthesis"); advanceToken(); return exp; } else { return parseRelation(); } }
long ZeckendorfLogarithm(mpz_class n, mpz_class a) { /* Check that the input is valid. */ /* Construct our "Fibonacci iterator" to keep track of where we are in the * Fibonacci sequence, including both the base and the logarithm. */ //std::pair<T, T> fibExponentIterator(T(1), a); //std::pair<Integer, Integer> fibBaseIterator(0u, 1u); mpz_class first(1); mpz_class second(a); mpz_class b_first(0); mpz_class b_second(1); /* Keep marching the iterator forward until we arrive at n. */ while (second<n ) { /* Compute the next value in the generalized Fibonacci sequence as * * (F(k), F(k+1)) -> (F(k+1), F(k+2)) */ mpz_class nextExp(first * second); cout<<nextExp<<endl; first = second; second = nextExp; mpz_class nextBase (b_first + b_second); cout<<nextBase<<endl; b_first = b_second; b_second = nextBase; } mpz_class result(0); /* Keep dividing out terms from the Zeckendorf representation of the number * until it reaches one. */ while (b_first != 0) { /* If the first of the Fibonacci numbers is less than the current number, * divide it out and add the corresponding Fibonacci number to the * total. */ if (b_first <= n) { n /= b_first; result += b_first; } /* Walk both iterators backward a step: * * (F(k), F(k+1)) -> (F(k-1), F(k)) */ mpz_class prevExp (second / first); second = first; first = prevExp; mpz_class prevBase (b_second - b_first); b_second = b_first; b_first = prevBase; } return result.get_si(); }