From d13657a411c549b9bcb647478be01249ed74ee71 Mon Sep 17 00:00:00 2001 From: Mikael Vejdemo-Johansson <michiexile@gmail.com> Date: Fri, 10 Apr 2015 10:05:19 +0200 Subject: [PATCH] Changed generating functions printouts to be copy-paste-able into SageMath. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we can do things like: # copy-paste from output ring.<t,L,tie,Cn,M,Ln,I,D,J,Rn,A,K,F,G> = QQ[] ID = ring.ideal(L - (1*Cn*t),tie - (1*Ln*t),Cn - (1*I + 1*J),M - (1*t^2),Ln - (1*D + 1*L + 1*M),I - (1*Rn*t),D - (1*Rn*t),J - (1*Ln*t),Rn - (1*F + 1*G + 1*K),A - (1*tie),K - (1*t^2),F - (1*Ln*t),G - (1*Cn*t)) #Â we are interested in tie in terms of t; so we want to remove anything not these two: ID.elimination_ideal([L,Cn,M,Ln,I,D,J,Rn,A,K,F,G]) # output from this SageMath command is # Ideal (t^3 + 2*t^2*tie + t*tie - tie) of Multivariate Polynomial Ring in t, L, tie, Cn, M, Ln, I, D, J, Rn, A, K, F, G over Rational Field # which we can solve for tie to get tie = t^3/(1-t-2*t^2) just as expected --- examples/grammar.c | 45 +++++++++++++++++++++++++++++------ examples/ties.c | 59 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/examples/grammar.c b/examples/grammar.c index f722edf0..7638fe99 100644 --- a/examples/grammar.c +++ b/examples/grammar.c @@ -20,7 +20,8 @@ #include <stdio.h> const char *nonterminal_name(const HCFGrammar *g, const HCFChoice *nt) { - if(nt->user_data != NULL) { + // if user_data exists and is printable: + if(nt->user_data != NULL && *(char*)(nt->user_data) > ' ' && *(char*)(nt->user_data) < 127) { if(*(char*)(nt->user_data) != '0') { // user_data is a non-empty string return nt->user_data; @@ -80,7 +81,7 @@ void readsequence(FILE *file, uint32_t *count, uint32_t *length, *count *= cscount; break; default: // HCF_CHOICE, non-terminal symbol - fprintf(file, "*%s(t)", nonterminal_name(g, *x)); + fprintf(file, "*%s", nonterminal_name(g, *x)); break; } } @@ -101,19 +102,48 @@ void h_pprint_gfeqns(FILE *file, const HCFGrammar *g) { // determine maximum string length of symbol names int len; size_t s; - for(len=1, s=26; s < g->nts->used; len++, s*=26); + for(len=1, s=26; s < g->nts->used; len++, s*=26); - // iterate over g->nts + // emit the SageMath ring init string + // iterate over g->nts, output symbols size_t i; - HHashTableEntry *hte; + HHashTableEntry *hte; + fprintf(file, "ring.<t"); + for(i=0; i < g->nts->capacity; i++) { + for(hte = &g->nts->contents[i]; hte; hte = hte->next) { + if (hte->key == NULL) { + continue; + } + const HCFChoice *nt = hte->key; + fprintf(file, ","); + + fprintf(file, "%s", nonterminal_name(g, nt)); + } + } + fprintf(file, "> = QQ[]\n"); + + + // iterate over g->nts + // emit a Sage ideal definition + int j=0; + fprintf(file, "ID = ring.ideal("); for(i=0; i < g->nts->capacity; i++) { for(hte = &g->nts->contents[i]; hte; hte = hte->next) { if (hte->key == NULL) { continue; } + if(j>0) { + fprintf(file, ","); + } + j++; + const HCFChoice *nt = hte->key; - fprintf(file, "%s(t) = ", nonterminal_name(g, nt)); + const char *ntn = nonterminal_name(g, nt); + if(*ntn == 0) { + continue; + } + fprintf(file, "%s - (", ntn); for(HCFSequence **seq = nt->seq; *seq; seq++) { @@ -142,7 +172,8 @@ void h_pprint_gfeqns(FILE *file, const HCFGrammar *g) { } } - fprintf(file, "\n"); + fprintf(file, ")"); } } + fprintf(file, ")\n"); } diff --git a/examples/ties.c b/examples/ties.c index 09f6b707..77d08212 100644 --- a/examples/ties.c +++ b/examples/ties.c @@ -47,6 +47,18 @@ HParser* finkmao() { h_bind_indirect(Rnext, R_); h_bind_indirect(Cnext, C_); HParser *tie = h_sequence(L, Lnext, NULL); + + h_desugar_augmented(mm__, tie); + + L->desugared->user_data = "L"; + R->desugared->user_data = "R"; + C->desugared->user_data = "C"; + Lnext->desugared->user_data = "Ln"; + Rnext->desugared->user_data = "Rn"; + Cnext->desugared->user_data = "Cn"; + tie->desugared->user_data = "tie"; + U->desugared->user_data = "0U"; + return tie; } @@ -103,7 +115,20 @@ HParser* depth1TW() { h_epsilon_p(), NULL); h_bind_indirect(tuckpairstar, tpstar_); - return h_choice(h_sequence(prefix, tuckpairstar, tuck, NULL), NULL); + HParser *tie = h_choice(h_sequence(prefix, tuckpairstar, tuck, NULL), NULL); + + h_desugar_augmented(mm__, tie); + + T->desugared->user_data = "T"; + W->desugared->user_data = "W"; + U->desugared->user_data = "0U"; + prefix->desugared->user_data = "prefix"; + pair->desugared->user_data = "pair"; + tuck->desugared->user_data = "tuck"; + tpstar_->desugared->user_data = "tuckpairstar"; + tie->desugared->user_data = "tie"; + + return tie; } HParser* depth1() { @@ -144,10 +169,23 @@ HParser* depth1() { h_bind_indirect(lastR, R_); h_bind_indirect(lastL, L_); h_bind_indirect(lastC, C_); - return h_choice(h_sequence(L, lastL, NULL), + HParser* tie = h_choice(h_sequence(L, lastL, NULL), h_sequence(R, lastR, NULL), h_sequence(C, lastC, NULL), NULL); + + h_desugar_augmented(mm__, tie); + + L->desugared->user_data = "L"; + R->desugared->user_data = "R"; + C->desugared->user_data = "C"; + U->desugared->user_data = "0U"; + lastL ->desugared->user_data = "Ln"; + lastR->desugared->user_data = "Rn"; + lastC->desugared->user_data = "Cn"; + tie->desugared->user_data = "tie"; + + return tie; } HParser* depthNTW() { @@ -237,14 +275,27 @@ HParser* depthNTW() { NULL); h_bind_indirect(tuckpairstar, tpstar_); - return h_choice(h_sequence(prefix, tuckpairstar, tuck, NULL), NULL); + HParser *tie = h_choice(h_sequence(prefix, tuckpairstar, tuck, NULL), NULL); + + h_desugar_augmented(mm__, tie); + + T->desugared->user_data = "T"; + W->desugared->user_data = "W"; + U->desugared->user_data = "0U"; + prefix->desugared->user_data = "prefix"; + pair->desugared->user_data = "pair"; + tuck->desugared->user_data = "tuck"; + tpstar_->desugared->user_data = "tuckpairstar"; + tie->desugared->user_data = "tie"; + + return tie; } int main(int argc, char **argv) { mm__ = &system_allocator; - HParser *p = finkmaoTW(); + HParser *p = finkmao(); HCFGrammar *g = h_cfgrammar_(mm__, h_desugar_augmented(mm__, p)); if (g == NULL) { fprintf(stderr, "h_cfgrammar failed\n"); -- GitLab