static void pr_dec(FILE *out, A_dec v, int d) { indent(out, d); switch (v->kind) { case A_functionDec: fprintf(out, "functionDec(%s,\n", S_name(v->u.function.func)); pr_tyfieldList(out, v->u.function.tyfieldList, d + 1);fprintf(out, ",\n"); fprintf(out, "%s,\n", S_name(v->u.function.type)); pr_exp(out, v->u.function.exp, d + 1); fprintf(out, ")"); break; case A_varDec: fprintf(out, "varDec(%s,\n", S_name(v->u.var.id)); if (v->u.var.type) { indent(out, d + 1); fprintf(out, "%s,\n", S_name(v->u.var.type)); } pr_exp(out, v->u.var.exp, d + 1); fprintf(out, ",\n"); indent(out, d + 1); //fprintf(out, "%s", v->u.var.escape ? "TRUE)" : "FALSE)"); break; case A_typeDec: fprintf(out, "typeDec(\n"); fprintf(out, "%s,\n", S_name(v->u.type.type)); pr_ty(out, v->u.type.ty, d + 1); fprintf(out, ")"); break; default: assert(0); } }
static void pr_dec(FILE *out, A_dec v, int d) { indent(out, d); switch (v->kind) { case A_functionDec: fprintf(out, "functionDec(\n"); pr_fundecList(out, v->u.function, d+1); fprintf(out, ")"); break; case A_varDec: fprintf(out, "varDec(%s,\n", S_name(v->u.var.var)); if (v->u.var.typ) { indent(out, d+1); fprintf(out, "%s,\n", S_name(v->u.var.typ)); } pr_exp(out, v->u.var.init, d+1); fprintf(out, ",\n"); indent(out, d+1); fprintf(out, "%s", v->u.var.escape ? "TRUE)" : "FALSE)"); break; case A_typeDec: fprintf(out, "typeDec(\n"); pr_nametyList(out, v->u.type, d+1); fprintf(out, ")"); break; default: assert(0); } }
static void pr_assignfield(FILE *out, A_assignfield v, int d) { indent(out, d); if (v) { fprintf(out, "assignfield(%s,\n", S_name(v->id)); pr_exp(out, v->exp, d + 1); fprintf(out, ")"); } else fprintf(out, "assignfield()"); }
static void pr_fundec(FILE *out, A_fundec v, int d) { indent(out, d); fprintf(out, "fundec(%s,\n", S_name(v->name)); pr_fieldList(out, v->params, d+1); fprintf(out, ",\n"); if (v->result) { indent(out, d+1); fprintf(out, "%s,\n", S_name(v->result)); } pr_exp(out, v->body, d+1); fprintf(out, ")"); }
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: a.out filename\n"); exit(1); } if (parse(argv[1])) pr_exp(stdout, absyn_root, 2); return 0; }
static void pr_efield(FILE *out, A_efield v, int d) { indent(out, d); if (v) { fprintf(out, "efield(%s,\n", S_name(v->name)); pr_exp(out, v->exp, d+1); fprintf(out, ")"); } else { fprintf(out, "efield()"); } }
static void pr_expList(FILE *out, A_expList v, int d) { indent(out, d); if (v) { fprintf(out, "expList(\n"); pr_exp(out, v->head, d + 1); fprintf(out, ",\n"); pr_expList(out, v->tail, d + 1); fprintf(out, ")"); } else fprintf(out, "expList()"); }
int main(int argc, char **argv) { if (argc != 2){ fprintf(stderr, "usage: a.out filename\n"); exit(1); } A_exp temp = parse(argv[1]); if (temp) { pr_exp(stdout, temp, 4); printf("\n-------------------------\n"); SEM_transProg(temp); } return 0; }
int main(int argc, char **argv) { FILE* outfile; A_exp ast; yydebug = 1; if (argc!=3) {fprintf(stderr,"usage: parser tigfile outfile\n"); exit(1);} if (! (outfile=fopen(argv[2], "w"))) exit(1); EM_reset(argv[1]); ast = parse(argv[1]); if (ast) pr_exp(outfile, ast, 0); return 0; }
int main(int argc, char **argv) { extern int yydebug; yydebug = 1; if (argc < 2) { fprintf(stderr,"Utilize o comando da seguinte forma: compilador.tge filename -options\n"); exit(1); } char *filename = argv[1]; A_exp prg = parse(filename); int IS_ERRORS; if (prg != NULL) { printf("\n[ ANALISE SINTATICA ] Completada!! :) \n"); printf("[ ANALISE SEMANTICA ] Iniciando!\n"); SEM_transProg(prg, &IS_ERRORS); printf("[ ANALISE SEMANTICA ] Completada! :D \n"); }else{ printf("[ ANALISE SEMANTICA ] Não entrou! (ಠ_ಠ) \n"); } if(!IS_ERRORS){ char *treeFileName = malloc(500); sprintf(treeFileName, "%s",filename); treeFileName = remove_ext(treeFileName, '.', '/'); strcat(treeFileName, ".arv"); FILE* treefile = fopen(treeFileName, "w+"); printf("[ EMISSÃO DA ARVORE ] Iniciando!\n"); pr_exp(treefile, prg, 0); printf("[ EMISSÃO DA ARVORE ] Completada! XD \n"); char *emit = "-llvm-emit"; if(argc == 3 && (strcmp(emit, argv[2]) == 0)){ printf("[ GERACAO DE CODIGO ] Iniciando!\n"); geraCodigoIntermediario(filename, prg); printf("[ GERACAO DE CODGIO ] Completada! (͡° ͜ʖ ͡°)\n"); } } return 0; }
int main(int argc, char *argv[]) { A_exp absyn_root; F_fragList frags; char outfile[100]; FILE *out = NULL; if (argc == 2) { absyn_root = parse(argv[1]); if (!absyn_root) return 1; #if 0 pr_exp(out, absyn_root, 0); /* print absyn data structure */ fprintf(out, "\n"); #endif Esc_findEscape(absyn_root); /* set varDec's escape field */ frags = SEM_transProg(absyn_root); if (anyErrors) return 1; /* don't continue */ /* convert the filename */ sprintf(outfile, "%s.s", argv[1]); /* Chapter 8 */ for (;frags;frags=frags->tail) if (frags->head->kind == F_procFrag) doProc(out, frags->head->u.proc.frame, frags->head->u.proc.body); else if (frags->head->kind == F_stringFrag) { if (out == NULL) { out = fopen(outfile, "w"); if (out == NULL) assert(0 && "Cannot create file."); } fprintf(out, "%s\n", frags->head->u.stringg.str); } if (out) fclose(out); return 0; } EM_error(0,"usage: tiger file.tig"); return 1; }
/* Print A_var types. Indent d spaces. */ static void pr_var(FILE *out, A_var v, int d) { indent(out, d); switch (v->kind) { case A_simpleVar: fprintf(out, "simpleVar(%s)", S_name(v->u.simple)); break; case A_fieldVar: fprintf(out, "%s\n", "fieldVar("); pr_var(out, v->u.field.var, d + 1); fprintf(out, "%s\n", ","); indent(out, d + 1); fprintf(out, "%s)", S_name(v->u.field.id)); break; case A_subscriptVar: fprintf(out, "%s\n", "subscriptVar("); pr_var(out, v->u.subscript.var, d + 1); fprintf(out, "%s\n", ","); pr_exp(out, v->u.subscript.exp, d + 1); fprintf(out, "%s", ")"); break; default: assert(0); } }
int main(int argc, char **argv){ FILE *fp1, *fp2; if((fp1=fopen("prabsyn_result.txt","w"))==NULL) printf("cannot open file!\n"); if((fp2 = fopen("prtreeexp_result.txt", "w")) == NULL) printf("cannot open file!\n"); if (argc!=2) {fprintf(stderr,"usage: a.out filename\n"); exit(1);} A_exp exp = parse(argv[1]); if (exp) { printf("analyze abstract syntax successfully!\n"); pr_exp(fp1, exp, 0); Tr_exp e= SEM_TransProg(exp); printf("semant check end!\n"); pr_tree_exp(fp2, UnEx(e), 0);//暂时调用UnEx调试,本该不能被外部调用 } else printf("pass the syntax rule to begin semant check!\n"); return 0; }
/* Print A_var types. Indent d spaces. */ void pr_exp(FILE *out, A_exp v, int d) { indent(out, d); switch (v->kind) { case A_varExp: fprintf(out, "varExp(\n"); pr_var(out, v->u.var, d+1); fprintf(out, "%s", ")"); break; case A_nilExp: fprintf(out, "nilExp()"); break; case A_intExp: fprintf(out, "intExp(%d)", v->u.intt); break; case A_stringExp: fprintf(out, "stringExp(%s)", v->u.stringg); break; case A_callExp: fprintf(out, "callExp(%s,\n", S_name(v->u.call.func)); pr_expList(out, v->u.call.args, d+1); fprintf(out, ")"); break; case A_opExp: fprintf(out, "opExp(\n"); indent(out, d+1); pr_oper(out, v->u.op.oper); fprintf(out, ",\n"); pr_exp(out, v->u.op.left, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.op.right, d+1); fprintf(out, ")"); break; case A_recordExp: fprintf(out, "recordExp(%s,\n", S_name(v->u.record.typ)); pr_efieldList(out, v->u.record.fields, d+1); fprintf(out, ")"); break; case A_seqExp: fprintf(out, "seqExp(\n"); pr_expList(out, v->u.seq, d+1); fprintf(out, ")"); break; case A_assignExp: fprintf(out, "assignExp(\n"); pr_var(out, v->u.assign.var, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.assign.exp, d+1); fprintf(out, ")"); break; case A_ifExp: fprintf(out, "iffExp(\n"); pr_exp(out, v->u.iff.test, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.iff.then, d+1); if (v->u.iff.elsee) { /* else is optional */ fprintf(out, ",\n"); pr_exp(out, v->u.iff.elsee, d+1); } fprintf(out, ")"); break; case A_whileExp: fprintf(out, "whileExp(\n"); pr_exp(out, v->u.whilee.test, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.whilee.body, d+1); fprintf(out, ")\n"); break; case A_forExp: fprintf(out, "forExp(%s,\n", S_name(v->u.forr.var)); pr_exp(out, v->u.forr.lo, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.forr.hi, d+1); fprintf(out, "%s\n", ","); pr_exp(out, v->u.forr.body, d+1); fprintf(out, ",\n"); indent(out, d+1); fprintf(out, "%s", v->u.forr.escape ? "TRUE)" : "FALSE)"); break; case A_breakExp: fprintf(out, "breakExp()"); break; case A_letExp: fprintf(out, "letExp(\n"); pr_decList(out, v->u.let.decs, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.let.body, d+1); fprintf(out, ")"); break; case A_arrayExp: fprintf(out, "arrayExp(%s,\n", S_name(v->u.array.typ)); pr_exp(out, v->u.array.size, d+1); fprintf(out, ",\n"); pr_exp(out, v->u.array.init, d+1); fprintf(out, ")"); break; default: assert(0); } }
/* Print A_var types. Indent d spaces. */ void pr_exp(FILE *out, A_exp e, int d) { indent(out, d); switch (e->kind) { case A_varExp: fprintf(out, "varExp(\n"); pr_var(out, e->u.var, d + 1); fprintf(out, "%s", ")"); break; case A_nilExp: fprintf(out, "nilExp()"); break; case A_intExp: fprintf(out, "intExp(%d)", e->u.int_); break; case A_stringExp: fprintf(out, "stringExp(%s)", e->u.string_); break; case A_callExp: fprintf(out, "callExp(%s,\n", S_name(e->u.call.func)); pr_expList(out, e->u.call.args, d + 1); fprintf(out, ")"); break; case A_opExp: fprintf(out, "opExp(\n"); indent(out, d + 1); pr_binop(out, e->u.op.op); fprintf(out, ",\n"); pr_exp(out, e->u.op.exp1, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.op.exp2, d + 1); fprintf(out, ")"); break; case A_recordExp: fprintf(out, "recordExp(%s,\n", S_name(e->u.record.type)); pr_assignfieldList(out, e->u.record.assignfieldList, d + 1); fprintf(out, ")"); break; case A_seqExp: fprintf(out, "seqExp(\n"); pr_expList(out, e->u.seq, d + 1); fprintf(out, ")"); break; case A_assignExp: fprintf(out, "assignExp(\n"); pr_var(out, e->u.assign.var, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.assign.exp, d + 1); fprintf(out, ")"); break; case A_ifExp: fprintf(out, "ifExp(\n"); pr_exp(out, e->u.if_.exp1, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.if_.exp2, d + 1); if (e->u.if_.exp3) { /* else is optional */ fprintf(out, ",\n"); pr_exp(out, e->u.if_.exp3, d + 1); } fprintf(out, ")"); break; case A_whileExp: fprintf(out, "whileExp(\n"); pr_exp(out, e->u.while_.exp1, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.while_.exp2, d + 1); fprintf(out, ")\n"); break; case A_forExp: fprintf(out, "forExp(%s,\n", S_name(e->u.for_.id)); pr_exp(out, e->u.for_.exp1, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.for_.exp2, d + 1); fprintf(out, "%s\n", ","); pr_exp(out, e->u.for_.exp3, d + 1); fprintf(out, ",\n"); indent(out, d + 1); //fprintf(out, "%s", e->u.for_.escape ? "TRUE)" : "FALSE)"); break; case A_breakExp: fprintf(out, "breakExp()"); break; case A_letExp: fprintf(out, "letExp(\n"); pr_decList(out, e->u.let.decList, d + 1); fprintf(out, ",\n"); pr_expList(out, e->u.let.expList, d + 1); fprintf(out, ")"); break; case A_arrayExp: fprintf(out, "arrayExp(%s,\n", S_name(e->u.array.type)); pr_exp(out, e->u.array.exp1, d + 1); fprintf(out, ",\n"); pr_exp(out, e->u.array.exp2, d + 1); fprintf(out, ")"); break; default: assert(0); } }
int main(int argc, char **argv) { if (argc!=2) {fprintf(stderr,"usage: a.out filename\n"); exit(1);} pr_exp(stdout,parse(argv[1]),0); printf("\n"); return 0; }